Java Reference
In-Depth Information
Example 11•1: GraphicsSampler.java (continued)
// Load some Image objects for use in the paint() method.
image = this.getImage(this.getDocumentBase(), "tiger.gif");
background = this.getImage(this.getDocumentBase(), "background.gif");
// Set a property that tells the applet its background color
this.setBackground(Color.lightGray);
}
// This method is called whenever the applet needs to be drawn or redrawn
public void paint(Graphics g) {
g.setFont(font); // Specify the font we'll be using throughout
// Draw a background by tiling an image tile() is defined below
tile(g, this, background);
// Draw a line
g.setColor(outline); // Specify the drawing color
g.drawLine(25, 10, 150, 80); // Draw a line from (25,10) to (150,80)
// Draw some text. See the centerText() method below.
centerText("drawLine()", null, g, textcolor, 25, 10, 150, 80);
// Draw and fill an arc
g.setColor(fill);
g.fillArc(225, 10, 150, 80, 90, 135);
g.setColor(outline);
g.drawArc(225, 10, 150, 80, 90, 135);
centerText("fillArc()", "drawArc()", g, textcolor,225,10,150,80);
// Draw and fill a rectangle
g.setColor(fill);
g.fillRect(25, 110, 150, 80);
g.setColor(outline);
g.drawRect(25, 110, 150, 80);
centerText("fillRect()", "drawRect()", g, textcolor, 25, 110, 150, 80);
// Draw and fill a rounded rectangle
g.setColor(fill);
g.fillRoundRect(225, 110, 150, 80, 20, 20);
g.setColor(outline);
g.drawRoundRect(225, 110, 150, 80, 20, 20);
centerText("fillRoundRect()", "drawRoundRect()", g, textcolor,
225, 110, 150, 80);
// Draw and fill an oval
g.setColor(fill);
g.fillOval(25, 210, 150, 80);
g.setColor(outline);
g.drawOval(25, 210, 150, 80);
centerText("fillOval()", "drawOval()", g, textcolor, 25, 210, 150, 80);
// Define an octagon using arrays of X and Y coordinates
int numpoints = 8;
int[] xpoints = new int[numpoints+1];
int[] ypoints = new int[numpoints+1];
for(int i=0; i < numpoints; i++) {
double angle = 2*Math.PI * i / numpoints;
xpoints[i] = (int)(300 + 75*Math.cos(angle));
ypoints[i] = (int)(250 - 40*Math.sin(angle));
Search WWH ::




Custom Search