// HelloSwingApplet.java (c) 2006 Kari Laitinen // http://www.naturalprogramming.com/ // 2006-01-21 File created. // 2006-09-28 Latest modification. // This prorgram is a 'swing' version of HelloApplet.java // This means that swing classes like JPanel and JApplet are // in use. Swing classes represent newer Java technology and thus // they should be favored in Java programming. When swing classes are // in use, Java programs should always provide the same Graphical User // Interface (GUI) regardless of the environment (Windows, UNIX, Linux, // Macintosh, etc.) in which the programs are executed. // See the notes at the end of this file. import java.awt.* ; import javax.swing.* ; class HelloPanel extends JPanel { public void paintComponent( Graphics graphics ) { // The superclass version of paintComponent() clears the // component area and does other necessary things. super.paintComponent( graphics ) ; graphics.drawString( "Hello. I am a Java applet.", 80, 100 ) ; graphics.drawString( "The coordinates of this line are (80,150).", 80, 150 ) ; } } public class HelloSwingApplet extends JApplet { public void init() { Container content_pane_of_this_applet = getContentPane() ; HelloPanel main_panel_of_this_applet = new HelloPanel() ; content_pane_of_this_applet.add( main_panel_of_this_applet ) ; } } /* NOTES: // The above init() method can alternatively be written in the // following way: public void init() { getContentPane().add( new HelloPanel() ) ; } */ // If you compare the program above to HelloApplet.java, you // may think that swing applets are complicated programs // when compared to traditional Java applets. // The above program is constructed so that a panel object is // made to represent the contents of the applet. It is not absolutely // necessary to write swing applets in this way. A less complicated // version of the above program would be the following. /* public class HelloSwingApplet extends JApplet { public void paint( Graphics graphics ) { super.paint( graphics ) ; graphics.drawString( "Hello. I am a Java applet.", 80, 100 ) ; graphics.drawString( "The coordinates of this line are (80,150).", 80, 150 ) ; } } */