//HelloApplet.java //applet demo to show how easy it is to create an applet import java.awt.*; import javax.swing.*; public class HelloApplet extends JApplet { //*** //no main incantation... //no JFrame Container setVisible... //fields that need to be shared by init and paint methods: String name; //init() for setting up GUI, if any, and getting input, if any. //called automatically. public void init() { name = JOptionPane.showInputDialog( "Hello, what's your name?" ); } //paint draws in the applet. //called automatically after init() and whenever window is uncovered. public void paint( Graphics g ) { //this example can be made easier by using constants instead //of getWidth() and getHeight()... g.setColor( Color.GREEN ); g.fillOval( 0, 0, getWidth()/2, getHeight()/2 ); g.setColor( Color.MAGENTA ); g.fillOval( getWidth()/2, 0, getWidth()/2, getHeight()/2 ); g.setColor( Color.CYAN ); g.fillOval( 0, getHeight()/2, getWidth()/2, getHeight()/2 ); g.setColor( Color.YELLOW ); g.fillOval( getWidth()/2, getHeight()/2, getWidth()/2, getHeight()/2 ); g.setColor( Color.RED ); g.drawLine( 0, 0, getWidth(), getHeight() ); g.drawLine( 0, getWidth(), getHeight(), 0 ); g.drawRect( 10, 10, getWidth()-20, getHeight()-20 ); g.drawOval( 0, 0, getWidth()-1, getHeight()-1 ); g.setColor( Color.BLUE ); g.setFont( new Font( "Serif", Font.BOLD, 16 ) ); g.drawString( name, getWidth()/2-20, getHeight()/2 ); } } /* Need an .html file that contains: Run the applet either by appletviewer: command line: appletviewer HelloApplet.html JCreator: execute the .html file or by web browser: open the .html file */