The paint() method is used to display text, shapes, and graphics within the applet window. Whenever something needs to be displayed or redisplayed on the applet window, the paint() method handles the task. You also can force paint() to be handled with the following statement in any method of an applet:
repaint();
Aside from the use of repaint(), the main time the paint() method is handled is when something changes in the browser or the operating system running the browser. For example, if a user minimizes a web page containing an applet, the paint() method will be called to redisplay everything that was onscreen in the applet when the applet is later restored to full size.Unlike the other methods you will be learning about during this hour, paint() takes an argument. The following is an example of a simple paint() method:
public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D)screen;
// display statements go here
}
The argument sent to the paint() method is a Graphics object. The Graphics class of objects represents an environment in which something can be displayed, such as an applet window. As you did with Swing programs, you may cast this to a Graphics2D object to employ more sophisticated graphical features.
Later this hour, you'll learn about drawString(), a method for the display of text that's available in both the Graphics and Graphics2D classes.
If you are using a Graphics or Graphics2D object in your applet, you have to add the following import statements before the class statement at the beginning of the source file:
import java.awt.Graphics; import java.awt.Graphics2D;


4:48 AM
mitraaa
0 comments:
Post a Comment