|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Using Javas Polygon Class The Java Polygon class provides features that often make it the most convenient way to define polygons. The Polygon class provides the two constructors, defined as follows: public Polygon() public Polygon(int xpoints[], int ypoints[], int npoints) These constructors enable you to either instantiate an empty polygon or instantiate a polygon by initially passing an array of x and an array of y numbers and the number of points made up of the x and y pairs. If you do the latter, the parameters are saved in the following Polygon class fields: public int xpoints[] public int ypoints[] public int npoints Regardless of whether you started with an empty polygon, you can add points to it dynamically by using the Polygon class addPoint() method, defined as follows: public void addPoint(int x, int y) The addPoint() method automatically increments the Polygon class number of points field, named npoints. The Polygon class includes two other methods: the getBoundingBox() and inside() methods, defined as follows: public Rectangle getBoundingBox() public boolean inside(int x, int y) You can use the getBoundingBox() method to determine the minimum-sized box that can completely surround the polygon in screen coordinates. The Rectangle class returned by getBoundingBox() contains variables indicating the x , y coordinates of the rectangle along with the rectangles width and height. You determine whether a point is contained within the polygon or is outside it by calling the inside methods with the x,y coordinate of the point. Use the Polygon class in place of the x and y arrays for either the drawPolygon() or fillPolygon() method as indicated in their definitions, shown here: public void drawPolygon(Polygon p) public void fillPolygon(Polygon p) The Polygon class is used for both the drawPolygon() and the fillPolygon() methods in Listing 39.8. This applets output is identical to Figure 39.10. Listing 39.8 Polygons.javaThis Applet Draws Polygons Based on a Polygon Object import java.awt.Graphics; import java.awt.Polygon; public class Polygons extends java.applet.Applet { int xCoords[] = { 50, 200, 300, 150, 50, 50 }; int yCoords[] = { 100, 0, 50, 300, 200, 100 }; int xFillCoords[] = { 450, 600, 700, 550, 450, 450 }; public void paint(Graphics g) { Polygon myPolygon = new Polygon(xCoords, yCoords, 6); Polygon myFilledPolygon = new Polygon(xFillCoords, yCoords, 6); // Draw the left polygon. g.drawPolygon(myPolygon); // Draw the right filled polygon. g.fillPolygon(myFilledPolygon); } } Displaying ColorsRemember when you were in elementary school, and the teacher showed you how to make green by combining yellow and blue paint? Forget all thatthose techniques applied to pigments on paper, in which colors are made by subtracting colors from the white light falling on the paper. Most of the graphics you make in Java need to look good on a computer screenan additive process based on the primary colors red, green, and blue. Common combinations are
Black is formed by the absence of all light, and white is formed by the combination of all the additive primary colors. In other words, red, blue, and green, transmitted in equal amounts, result in white.
Java uses the RGB (Red, Green, and Blue) color model. You define the colors you want by indicating the amount of red, green, and blue light that you want to transmit to the viewer. You can do this either by using integers between 0 and 255 or by using floating-point numbers between 0.0 and 1.0. Table 39.1 indicates the red, green, and blue amounts for some common colors. Note that you dont always have to specify the RGB valuesthe Color class includes some named colors, such as red, blue, and white, as shown in the first column of Table 39.1.
|
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. |