Polygons and window size
Lab5d

We can draw all kinds of shapes with drawLine but if we want to "fill in" with color that we have not been able to do that. For example a filled triangle or a trapezoid or any polygon.

Create Lab5d1, remove the drawString and add these line and see what happens

// isosceles triangle
g.setColor(Color.blue);
Polygon tri = new Polygon();
  tri.addPoint(300,100);
  tri.addPoint(250,200);
  tri.addPoint(350, 200);
g.fillPolygon(tri);


Now add this

// right triangle
g.setColor(Color.red);
Polygon tri2 = new Polygon();
tri2.addPoint(50,100);
tri2.addPoint(150,100);
tri2.addPoint(50, 200);
g.fillPolygon(tri2);

with 4 sides or more sides you must be very careful - try these

// trapezoid
Polygon trap = new Polygon();
  trap.addPoint(100,170);
  trap.addPoint(200,170);
  trap.addPoint(225,230);
  trap.addPoint(75,230);
g.fillPolygon(trap);

Bad things can happen with 4 or more sides.
In the polygon trap, swatch the order of the 2nd and 3rd sides - what happened?

Now, this was supposed to be a pentagon -see what happened - can you re-arrange the addPoint lines so it is a pentagon

Polygon p5 = new Polygon();
  p5.addPoint( 440,100 );
  p5.addPoint(390,130 );
  p5.addPoint(420,160 );
  p5.addPoint( 490,130 );
  p5.addPoint( 460,160 );
g.drawPolygon(p5);


Syntax for Polygon

 

A better way to set the size of a window

We can size the window of the applet. You can/should do this in 2 places

First Place - Add this to the init method.       setSize(300, 200);
Second Place - open the HTML file and set the width and height to 300 and 200.. If you do not change the HTML size, your applet will not look correct on the webpage.

When you run it, you will see that this is not a very good size window for Lab5d1. The lesson learned here, is to set the window size before you start adding objects to the applet.

 

Assignments

Lab5d2    Set window size to 200 by 200, There must be symmetry
Chose one of these  

80-89 points 

90-100 points

 

Lab5d3 - Set window size to 300 by 200, Chose one of these

80-89 points

2 fillArcs, 2 polygons ( isosceles triangles), and 1 round rectangle

 

90-100 points,

2 fillArcs, 4 polygons( right triangles), 1 round rectangle