The "Hello World" Applet |
The bold lines of the following listing implement thepaint()
method.import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }Every applet must implement one or more of the
init()
,start()
, andpaint()
methods. You'll learn about these methods in the Overview of Applets lesson.Besides the
init()
,start()
, andpaint()
methods, applets can implement two more methods that the browser calls when a major event occurs (such as leaving the applet's page):stop()
anddestroy()
. Applets can implement any number of other methods, as well -- both custom methods and methods that override a superclass method implementation.Returning to the above code snippet, the Graphics object passed into the
paint()
method represents the applet's onscreen drawing context. The first argument to the GraphicsdrawString()
method is the string to draw onscreen. The second and third arguments are the (x,y) position of the lower left corner of the text onscreen. This applet draws the string "Hello world!" starting at location (50,25). The applet's coordinate system starts at (0,0), which is at the upper left corner of the applet's display area. You'll learn all about drawing to the screen in the Creating a User Interface trail.
The "Hello World" Applet |