|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
You pass the coordinates of the upper-left corner of the imaginary surrounding rectangle and the width and height of the oval to drawOval() or fillOval(). The width and height are equal to the width and height of the imaginary surrounding rectangle. The Ovals applet in Listing 39.5 draws a circle and a filled oval. Figure 39.8 shows the output from the Ovals applet. Listing 39.5 Ovals.javaYou Can Use the Oval Methods to Draw a Circle import java.awt.Graphics; // This applet draws an unfilled circle and a filled oval public class Ovals extends java.applet.Applet { public void paint(Graphics g) { // Draw a circle with a diameter of 150 (width=150, height=150) // With the enclosing rectangles upper-left corner at (20, 20) g.drawOval(20, 20, 150, 150); // Fill an oval with a width of 150 and a height of 80 // The upper-left corner of the enclosing rectangle is at (200, 20) g.fillOval(200, 20, 150, 80); } }
Drawing ArcsAn arc is a segment of the line that forms the perimeter of an oval, as demonstrated in Figure 39.9. This output is generated by the ArcDemo1 applet shown in Listing 39.6. Listing 39.6 ArcDemo1.javaThink of an Arc as a Wedge of an Oval import java.awt.Graphics; import java.awt.Color; public class ArcDemo1 extends java.applet.Applet { public void paint(Graphics g) { // Draw a gray oval 200 pixels wide and 100 pixels // high with the upper left corner of its // enclosing rectangle at (20, 20). g.setColor(Color.gray); g.drawOval(20, 20, 200, 100); // Draw a black arc from 0 to 90 degrees inside // the oval created above. g.setColor(Color.black); g.drawArc(20, 20, 200, 100, 0, 90); // Draw the same arc as above but to the right // with the upper left corner of its enclosing // rectangle at (240, 20). g.drawArc(240, 20, 200, 100, 0, 90); } }
Two Graphics class methods are provided for drawing arcs: the drawArc() and fillArc() methods. Their complete definitions are as follows: public abstract void drawArc(int x, int y, int width, ⇒int height, int startAngle, int arcAngle) public abstract void fillArc(int x, int y, int width, ⇒int height, int startAngle, int arcAngle) Use the first four parameters just as you did with the oval methods. In fact, you are drawing an invisible oval and the arc is a segment of the ovals perimeter defined by startAngle and arcAngle, the last two parameters. The startAngle parameter defines where your arc starts along the invisible ovals perimeter. In Java, angles are set around a 360° circle as follows:
The arcAngle parameter defines the distance, in degrees, that your arc traverses along the invisible ovals perimeter. Angles are positive in the counterclockwise direction and negative in the clockwise direction. The arc you saw in Figure 39.9 began at 0°, or at 3 oclock, and traversed the invisible oval 90° in the positive, or counterclockwise, direction. The relevant line in Listing 39.6 is reproduced here: g.drawArc(20, 20, 200, 100, 0, 90); Notice that the last parameter is given in the angle traversed and not the angle at which the arc ends. Therefore, if you want an arc that starts at 45° and ends at 135°, you must provide a startAngle parameter value of 45° and an arcAngle parameter value of 90°.
Using the fillArc() method results in a filled, pie-shaped wedge defined by the center of the invisible oval and the perimeter segment traversed by the arc. Drawing PolygonsThe Java Graphics class provides four methods for building polygons: two versions of the drawPolygon() method and two versions of the fillPolygon() method. Each has two methods so that you can either pass two arrays containing the x , y coordinate of the points in the polygon, or you can pass an instance of a Polygon class. The Polygon class is defined in the java.awt package. Making a Polygon by Passing Coordinate Arrays First look at how to make a polygon by using two arrays. The full definitions of the drawPolygon() and fillPolygon() methods for using arrays are public abstract void drawPolygon(int xPoints[], int yPoints[], ⇒int nPoints) public abstract void fillPolygon(int xPoints[], int yPoints[], ⇒int nPoints) The DrawPoly applet in Listing 39.7 draws a polygon using an array of x coordinates (xCoords) and an array of y coordinates (yCoords). Each x,y pair, the first x (50) and the first y (100) pair, for instance, defines a point on a plane (50, 100). Use the drawPolygon method to connect each point to the following point in the list. The first pair is (50, 100) and connects by a line to the second pair (200, 0), and so on. The drawPolygon methods third parameter, nPoints, is the number of points in the polygon and should equal the number of pairs in the x and y arrays. Figure 39.10 shows the DrawPoly applets output. Listing 39.7 DrawPoly.javaThis Applet Draws Polygons Based on Arrays of Coordinates import java.awt.Graphics; public class DrawPoly extends java.applet.Applet { int xCoords[] = { 50, 200, 300, 150, 50 }; int yCoords[] = { 100, 0, 50, 300, 200 }; int xFillCoords[] = { 450, 600, 700, 550, 450 }; public void paint(Graphics g) { // Draw the left polygon. g.drawPolygon(xCoords, yCoords, 5); // Draw the right filled polygon. g.fillPolygon(xFillCoords, yCoords, 5); } }
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |