|
To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
Listing 1.1 The Java Source Code for the Hello1 Applet 1 // Hello1.java 2 3 import java.awt.*; 4 import java.applet.*; 5 6 /** 7 * This class reads PARAM tags from its HTML host page and sets 8 * the color and label properties of the applet. Program execution 9 * begins with the init() method. 10 */ 11 public class Hello1 extends Applet 12 { 13 /** 14 * The entry point for the applet. 15 */ 16 public void init() 17 { 18 initForm(); 19 20 usePageParams(); 21 22 // TODO: Add any constructor code after initForm call. 23 } 24 25 private final String labelParam = "label"; 26 private final String backgroundParam = "background"; 27 private final String foregroundParam = "foreground"; 28 29 /** 30 * Reads parameters from the applet's HTML host and sets applet 31 * properties. 32 */ 33 private void usePageParams() 34 { 35 final String defaultLabel = "Default label"; 36 final String defaultBackground = "C0C0C0"; 37 final String defaultForeground = "000000"; 38 String labelValue; 39 String backgroundValue; 40 String foregroundValue; 41 42 /** 43 * Read the <PARAM NAME="label" VALUE="some string">, 44 * <PARAM NAME="background" VALUE="rrggbb">, 45 * and <PARAM NAME="foreground" VALUE="rrggbb"> tags from 46 * the applet's HTML host. 47 */ 48 labelValue = getParameter(labelParam); 49 backgroundValue = getParameter(backgroundParam); 50 foregroundValue = getParameter(foregroundParam); 51 52 if ((labelValue == null) || (backgroundValue == null) || 53 (foregroundValue == null)) 54 { 55 /** 56 * There was something wrong with the HTML host tags. 57 * Generate default values. 58 */ 59 labelValue = defaultLabel; 60 backgroundValue = defaultBackground; 61 foregroundValue = defaultForeground; 62 } 63 64 /** 65 * Set the applet's string label, background color, and 66 * foreground colors. 67 */ 68 label1.setText(labelValue); 69 label1.setBackground(stringToColor(backgroundValue)); 70 label1.setForeground(stringToColor(foregroundValue)); 71 this.setBackground(stringToColor(backgroundValue)); 72 this.setForeground(stringToColor(foregroundValue)); 73 } 74 75 /** 76 * Converts a string formatted as "rrggbb" to an awt.Color object 77 */ 78 private Color stringToColor(String paramValue) 79 { 80 int red; 81 int green; 82 int blue; 83 84 red = (Integer.decode("0x" + ⇒paramValue.substring(0,2))).intValue(); 85 green = (Integer.decode("0x" + ⇒paramValue.substring(2,4))).intValue(); 86 blue = (Integer.decode("0x" + ⇒paramValue.substring(4,6))).intValue(); 87 88 return new Color(red,green,blue); 89 } 90 91 /** 92 * External interface used by design tools to show properties of ⇒an applet. 93 */ 94 public String[][] getParameterInfo() 95 { 96 String[][] info = 97 { 98 { labelParam, "String", "Label string to be displayed" }, 99 { backgroundParam, "String", "Background color, format ⇒\"rrggbb\"" }, 100 { foregroundParam, "String", "Foreground color, format ⇒\"rrggbb\"" }, 101 }; 102 return info; 103 } 104 105 Label label1 = new Label(); 106 107 /** 108 * Initializes values for the applet and its components 109 */ 110 void initForm() 111 { 112 this.setBackground(Color.lightGray); 113 this.setForeground(Color.black); 114 label1.setText("label1"); 115 this.setLayout(new BorderLayout()); 116 this.add("North",label1); 117 } 118 } Visual J++ creates HTML code for you whenever an applet project is created. You can see the HTML code for our program in Listing 1.2. Listing 1.2 The HTML Code for the Hello1 Project 1 <HTML> 2 <HEAD> 3 <TITLE>Hello1 Program</TITLE> 4 </HEAD> 5 <BODY> 6 7 <!-- Insert HTML here --> 8 <applet 9 code=Hello1.class 10 name=Hello1 11 width=320 12 height=200 > 13 <param name=label value="Hello Visual J++ World."> 14 <param name=background value="008080"> 15 <param name=foreground value="FFFFFF"> 16 </applet> 17 18 </BODY> 19 </HTML> What Comes FirstOne of the first things youll need to learn is in which order the common Java methods are executed. These common methods are ones that can be overridden in your applet classes. Its important because you might be expecting things to be initialized when you hit the constructor code, and theyre blowing up your applet with millions of divide-by-zero exceptions. Knowing the order will allow you to do everything in the proper sequence and avoid initialization and setup pitfalls. Overriding methods is a way in which classes that extend other classes can use their own method rather than the method thats part of the class from which theyre extended. This is explained in more detail in Day 5s chapter, Frame Windows. Member Variable DeclarationsThe first thing most applet classes have are variable and class declarations. When variable declarations come first, this makes it easier for others to understand the program. This is because they dont have to scan through the source code to find the variables. For this reason, its recommended that you place variables at the beginning of the applet class. Local variables are the obvious exception. They still have to be in the method that declares and uses them. Variable initialization can be done at the same time as the declaration. You can set integer values or allocate a class. Following are some examples of legal initializations: int x = 0; int y = 0; int horizontal = 0, vertical = 0; String str1 = new String(); String str2 = new String( "This is my string" ); If youre declaring variables at the beginning of the class, you cant then assign them with regular code. In fact, you cant just stick code in the appletcode must be within a method. Here are some examples of declarations with legal and illegal initialization code: public class ThreadedHelloWorld extends Applet implements Runnable { int x, y; x = 2; // ILLEGAL-not in a method y = x * 4; // ILLEGAL-not in a method public void InitXY() { x = 2; // LEGAL-in a method y = x * 4; // LEGAL-in a method } }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |