img
The polygon's endpoints are specified by the coordinate pairs contained within the x and y
arrays. The number of points defined by x and y is specified by numPoints. There are alternative
forms of these methods in which the polygon is specified by a Polygon object.
The following applet draws an hourglass shape:
// Draw Polygon
import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass" width=230 height=210>
</applet>
*/
public class HourGlass extends Applet {
public void paint(Graphics g) {
int xpoints[] = {30, 200, 30, 200, 30};
int ypoints[] = {30, 30, 200, 200, 30};
int num = 5;
g.drawPolygon(xpoints, ypoints, num);
}
}
Sample output from this program is shown here:
Sizing Graphics
Often, you will want to size a graphics object to fit the current size of the window in which it
is drawn. To do so, first obtain the current dimensions of the window by calling getSize( ) on
the window object. It returns the dimensions of the window encapsulated within a Dimension
object. Once you have the current size of the window, you can scale your graphical output
accordingly.
To demonstrate this technique, here is an applet that will start as a 200×200-pixel square
and grow by 25 pixels in width and height with each mouse click until the applet gets larger
than 500×500. At that point, the next click will return it to 200×200, and the process starts over.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home