This ASCII file can be obtained from the following URL:
/wwwdb/htdocs/faculty/tohline/PHYS2411/q2.dir/Chap1.2.ColorAndFonts
"Exploring Java" by Niemeyer & Peck ---- Hello Web! ---- Practice Exercises (Joel Tohline 9/13/98) Making only minor modifications in the earlier "Hello Web" example(s), try adding color AND different type Fonts to your applet. --------------------------------------------------------------- --------------------------------------------------------------- NOTE: Before compiling any one of the following examples, you MUST name the file containing your example code "HelloWeb.java". It's resulting (compiled) Java byte-code will then be called "HelloWeb.class". NOTE: When running on the UNIX machine (baton or mleesun), don't forget to change the permission (using "chmod") on your "HelloWeb.class" file so that the above HTML file can actually run the applet from any web browser! --------------------------------------------------------------- //Example #1.2.A: // Straight from the textbook, but with predefined color. public class HelloWeb extends java.applet.Applet { public void paint( java.awt.Graphics gc ) { gc.setColor( java.awt.Color.blue); gc.drawString("Hello Web!", 125, 95 ); } } --------------------------------------------------------------- //Example #1.2.B: Using a "static" (i.e., predefined) Color. import java.applet.*; import java.awt.*; public class HelloWeb extends Applet { Color myColorChoice = Color.red; public void paint( Graphics gc ) { gc.setColor(myColorChoice); gc.drawString("Hello Web!", 125, 95 ); /* NOTE: The available "static" colors (Java version 1.0) are: Color.black Color.green Color.red Color.blue Color.lightGray Color.white Color.cyan Color.magenta Color.yellow Color.darkGray Color.orange Color.gray Color.pink See, for example, p. 189 of "Java in a Nutshell (version 1.0)". */ } } --------------------------------------------------------------- //Example #1.2.C: Building your own Color. import java.applet.*; import java.awt.*; public class Cf extends Applet { float red = 0.5F; //Decimal numbers are type "double" unless float blue = 0.5F; // they are suffixed with an "F". float green = 0.0F; Color myBrightGreen = new Color( 0, 255, 0); Color myPurple = new Color(red, green, blue); public void paint( Graphics gc ) { gc.setColor(myBrightGreen); gc.drawString("Hello Web!", 125, 95 ); gc.setCol(myPurple); gc.drawString("I Prefer Purple", 25, 145 ); /* NOTE: The java.awt.Color class provides 3 public "Constructors". Here we have used 2 of them: public Color( int r, int g, int b); public Color(float r, float g, float b); The integers r,g,b may have values between 0 and 255; the float numbers r,g,b may have values between 0.0 and 1.0 . See, for example, p. 243 of "Java in a Nutshell (version 1.0)". */ } } --------------------------------------------------------------- //Example #1.2.D: // Changing to a differnt Font. public class Ex1_2_D extends java.applet.Applet { public void paint( java.awt.Graphics gc ) { gc.setColor( java.awt.Color.blue ); gc.setFont( new java.awt.Font("Serif",java.awt.Font.BOLD,36) ); gc.drawString("Hello Web!", 125, 95 ); } } --------------------------------------------------------------- //Example #1.2.E: Using two different Fonts and Colors. import java.applet.*; import java.awt.*; public class Ex1_2_E extends Applet { Color myBrightGreen = new Color( 0, 255, 0); Color myPurple = new Color(0.5F, 0.0F, 0.5F); Font bigTimesRoman = new Font("Serif",Font.BOLD,36); Font fBI = new Font("Monospaced", Font.ITALIC + Font.BOLD, 12); public void paint( Graphics gc ) { gc.setColor(myBrightGreen); gc.setFont( fBI ); gc.drawString("Hello Web!", 125, 95 ); gc.setColor(myPurple); gc.setFont( bigTimesRoman ); gc.drawString("I Prefer Purple", 25, 145 ); /* See, for example, pp. 183-188 of "Java in a Nutshell (version 1.0)". Also see pp. 479-483 of "Exploring Java." */ } } ---------------------------------------------------------------