go to previous page   go to home page   go to next page

Answer:

No. Order matters.

When a filled figure is drawn it covers up any part of the picture that had previously been drawn.


Polygons

Irregular Pentagram

It would be nice to fill in the roof with color. Sadly, there is no quick way to do this. It would be nice if there were a fillTriangle method, but there is not. However, Graphics has methods that draw filled or outlined polygons and a triangle is a three sided polygon.

An object of the Polygon class represents a polygon figure. A polygon is a closed region bounded by line segments. For examples, triangles, squares, and rectangles are all polygons. Polygons can have any number of sides, and the sides can be of all different lengths.

To construct a Polygon object, do this:

Polygon poly = new Polygon();

So far, the polygon is empty (has no vertices).

A Polygon object contains a list of the vertices. A side of a polygon is a line that connects two vertices. The first and last vertices are connected to complete the figure.

Of course, since lines connected sequential pairs of vertices, you must define the vertices in order. Pick one to start with and then go around the polygon either clockwise or counterclockwise.

To add a vertex to a Polygon object do this:

poly.addPoint(int x, int y);

Once you have a polygon, you can draw it with

drawPoly( Polygon p )

or with

fillPoly( Polygon p)
.

Here is the code that creates the above polygon:


import javax.swing.*;
import java.awt.*;

class PentagramPanel extends JPanel
{
  final int width = 200;
  final int height = 200;  

  public PentagramPanel()
  {
    setPreferredSize( new Dimension( width, height) );
    setBackground( Color.YELLOW);   
  }
  
  public void paintComponent ( Graphics gr )
  { 
     super.paintComponent( gr );
     Polygon poly = new Polygon();   // construct an empty Polygon

     poly.addPoint(  20, 100 );      // add a vertex (the leftmost)
     poly.addPoint(  50, 180 );      // add another vertex, in  
     poly.addPoint( 150, 150 );      // counterclockwise order
     poly.addPoint( 180,  80 );
     poly.addPoint(  70,  30 );
     
     gr.setColor( Color.red);        // set the pen color to red
     gr.fillPolygon( poly );    
  }

}

public class DrawPentagram
{
   public static void main ( String[] args )
   {
      JFrame frame = new JFrame( "Pentagram" );
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      
      frame.getContentPane().add( new PentagramPanel() );
      
      frame.pack(); 
      frame.setVisible( true );
   }
}

QUESTION 14:

Can you start adding vertices with any vertex?