Java Graphics

Recall that when you include an Applet in HTML, you must specify the width and height. This determines the boundaries in pixels of the "Graphics" region in which the Applet may draw. This region is described by having an origin in the upper left hand corner as follows:



Methods to Resize

Note: getSize() is a method inheireted by the Applet class that you can call getSize().width, getSize().height to figure out the current width and height of the Applet.

Note: resize(int width, int height) is a method of the Applet class that requests that the applet be resized to width, height in size.


Graphics Class

You can use the Graphics class passed to the paint() method of an Applet class to draw items to the screen.

Some Examples to Do:
Hello World Applet
Hello World Applet 2
Message Applet
Squares Applet
Squares Applet 2
Squares Applet 3
Passing Parameters



Other Useful Supporting Classes

Color - create a object representing a specific color (use Graphics.setColor(Color))
(Also, standard colors: Color.white, Color.black, Color.red, .....)
  • Some related Applet methods
    • setBackground(Color) - sets background color of applet (default is grey).
    • setForeground(Color) - sets everything drawn in applet to this color regardless of what color it was drawn in.
  • SystemColor - a class that allows access to the System's color pallette.
    • SystemColor.activeCaption - the color used on the bar on top of an active window.
    • System.desktop - the desktop's background color (if one)
Polygon - represents a polygon by an array of x value, array of y values of the points in the polygon (use Graphics.fillPolygon(Polygon))
Font - create object representing a specific font (use Graphics.setFont(Font))