Last revised 4/25/03 at 5:00pm.

The Applet, Color, and Graphics Classes

 

The Applet Class

Methods

public void init()
Execute code that initializes the applet.

public void paint(Graphics g)
Executes code for redrawing the applet when a paint event occurs. (A paint event occurs when an applet is (1) first displayed, (2) restored after being minimized, (3) exposed after being covered by another window.)

The Graphics object g is passed in to the paint method when it the paint method is executed. The object g can then call its own drawing methods to determine what is actually "painted" on the applet when paint is executed.

public void setBackground(Color c)
Set the background color of the applet.

public void setSize(int width, int height)
Set the height and width the applet.

 

The Color Class

The Color class is used to set colors, either the background color or the current drawing color.

Variables and Constants

public final static Color black
public final static Color blue
public final static Color cyan
public final static Color darkGray
public final static Color gray
public final static Color green
public final static Color lightGray
public final static Color magenta
public final static Color orange
public final static Color pink
public final static Color red
public final static Color white
public final static Color yellow

Constructor

public Color(int r, int g, int b)

Create a Color object with red, green, and blue components of r, g, and b, respectively.

 

The Graphics Class

Many of the methods of the graphics class are for drawing various shapes on the applet. The graphics context is passed back by the operating system when the paint method gets executed.

Methods

public void drawLine(int xstart, int ystart, int xend, int yend)
Draw a line from (xstart, ystart) to (xend, yend). public void drawOval(int x, int y, int width, int height)
Fill the interior of an ellipse with the current color.

public void drawRect(int x, int y, int width, int height)
Draw boundary of a rectangle with the current color.

public void drawRoundRect(int x, int y, int width, int height, int aWidth, int aHeight)
Draw boundary of a rectangle with rounded corners.

public void fillOval(int x, int y, int width, int height)
Fill the interior of an ellipse with the current color.

public void fillRect(int x, int y, int width, int height)
Fill the interior of a rectangle with the current color.

public void fillRoundRect(int x, int y, int width, int height, int aWidth, int aHeight)
Fill the interior of a rectangle with rounded corners with the current color.

public void setColor(Color c)
Set the current color.