This applet illustrates more of the drawing methods in the Graphics
class:
|
import
java.applet.Applet;
import java.awt.Graphics;
/** Demonstrate basic AWT drawing with
Graphics. **/
public class SimpleDraw1Applet
extends Applet
{
public void paint
(Graphics g) {
//
Get the drawing area
int
dY = getSize ().height;
int
dX = getSize ().width;
int
midY = dY/2;
int
midX =d X/2;
//
Set current drawing color
g.setColor
(Color.red);
//
Draw a rectangle centered at the mid-point
g.drawRect
(midX-22,midY-22,44,44);
//
Set a new drawing color
g.setColor
(Color.green);
//
Fill a rectangle centered at the mid-point
//
Put it within the previous rectangle so that
//
border shows.
g.fillRect
(midX-21,midY-21,42,42);
//
Set a new drawing color
g.setColor
(Color.blue);
//
Draw a circle around the mid-point
g.drawOval
(midX-20,midY-21,40,40);
} //
paint
} // SimpleDraw1Applet |
Note how we obtained the dimensions of the drawing
area. The getSize()
method returns an instance of the Dimension
class, which provides direct access to its height
and width
variables. (As of Java 1.2, component
includes the methods getHeight()
and getWidth().)
Latest update: Oct. 27, 2004
|