112 Introduction to Java Applets Chapter 3 Try

112 Introduction to Java Applets Chapter 3 Try changing the shape to draw from Lines to Points by clicking the down arrow to the right of the word Lines at the bottom of the applet. A list drops down from the GUI component containing the two choices Lines and Points. To select Points, click the word Points in the list. The GUI component closes the list and the current shape type is now Points. This GUI component is commonly known as a choice, combo box or drop- down list. To start a new drawing, select Reload from the appletviewer s Applet menu. To terminate the applet, select Quit from the appletviewer s Applet menu. 3.2.3 The Java2D Applet The last applet we demonstrate before defining applets of our own shows many of the complex new two-dimensional drawing capabilities built into Java 2 known as the Java2D API. For this example, change directories to the jfc directory in the J2SDK s demo directory, then change to the Java2D directory (you can move up the directory tree toward demo using the command cd .. in both Windows and UNIX/Linux). In that directory is an HTML file (Java2Demo.html) that is used to execute the applet. In the command window, type the command appletviewer Java2Demo.html and press the Enter key. This executes the appletviewer. The appletviewer loads the HTML file specified as its command-line argument (Java2Demo.html), determines from the file which applet to load and begins execution of the applet. This particular demo takes some time to load as it is quite large. Figure 3.5 shows a screen capture of one of this applet s many demonstrations of Java s two-dimensional graphics capabilities. At the top of this demo you see tabs that look like file folders in a filing cabinet. This demo provides 12 different tabs with several different features on each tab. To change to a different part of the demo, simply click one of the tabs. Also, try changing the options in the upper-right corner of the applet. Some of these affect the speed with which the applet draws the graphics. For example, click the small box with a check in it (a GUI component known as a checkbox) to the left of the word Anti-Aliasing to turn off anti-aliasing (a graphics technique for producing smoother on-screen graphics in which the edges of the graphic are blurred). When this feature is turned off (i.e., its checkbox is unchecked), the animation speed increases for the animated shapes at the bottom of the demo shown in Fig. 3.5. This occurs because an animated shape displayed with anti-aliasing takes longer to draw than an animated shape without anti-aliasing. 3.3 A Simple Java Applet: Drawing a String Now, let s get started with some applets of our own. Remember, we are just getting started we have many more topics to learn before we can write applets similar to those demonstrated in Section 3.2. However, we will cover many of the same techniques in this book. We begin by considering a simple applet that mimics the program of Fig. 2.1 by displaying the string “Welcome to Java Programming!”. The applet and its screen output are shown in Fig. 3.6. The HTML document to load the applet into the applet- viewer is shown and discussed in Fig. 3.7. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01

Leave a Reply