home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next



FIGURE 39.10  Draw polygons using x and y arrays.

Using Java’s 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 rectangle’s 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 applet’s 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 Colors

Remember when you were in elementary school, and the teacher showed you how to make green by combining yellow and blue paint? Forget all that—those 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 screen—an additive process based on the primary colors red, green, and blue. Common combinations are

  Red and green, which results in yellow when the colors are bright and in brown when the colors are less intense
  Green and blue, resulting in cyan
  Red and blue, resulting in magenta

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.


NOTE:  The color effects of subtractive pigments and directly transmitted (additive) light are closely related. Each color pigment absorbs light, but not all of it. The color of a pigment is due to the wavelength of the light that the pigment does not absorb—and, therefore, the light that the pigment reflects. Because the absence of pigments results in all wavelengths of light being reflected, the result is white. This effect is the same as the transmission of all the additive primary colors of light. In contrast, all the colored pigments mixed together absorb all light. This effect is equivalent to the color black resulting from the absence of light.

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 don’t always have to specify the RGB values—the Color class includes some named colors, such as red, blue, and white, as shown in the first column of Table 39.1.


Previous Table of Contents Next


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.