-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolygonDemo.java
68 lines (54 loc) · 1.8 KB
/
PolygonDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.awt.*;
import javax.swing.JFrame;
import java.awt.Polygon;
public class PolygonDemo extends Canvas
{
public void paint( Graphics g )
{
g.setColor(Color.black);
g.drawString("Hey, a triangle!", 50, 50);
Polygon tri = new Polygon();
tri.addPoint(100, 100);
tri.addPoint(100, 300);
tri.addPoint(200, 300);
g.setColor(Color.blue);
g.fillPolygon(tri);
Polygon pent = new Polygon();
pent.addPoint(450, 200);
pent.addPoint(500, 250);
pent.addPoint(475, 350);
pent.addPoint(425, 350);
pent.addPoint(400, 250);
g.setColor(Color.green);
g.fillPolygon(pent);
Polygon hex = new Polygon();
// use trig to make a regular hexagon
int radius = 100; // pixels
int xCenter = 200;
int yCenter = 500;
for ( double ang = 0; ang<2*Math.PI; ang=ang+(2*Math.PI)/6.0)
{
double xDelta = radius * Math.cos(ang);
double yDelta = -radius * Math.sin(ang);
hex.addPoint(xCenter+(int)xDelta, yCenter+(int)yDelta);
}
g.setColor(Color.black);
g.fillPolygon(hex);
Polygon trapi = new Polygon();
trapi.addPoint(400,300);
trapi.addPoint(350,350);
trapi.addPoint(250,350);
trapi.addPoint(300,300);
g.setColor(Color.MAGENTA);
g.fillPolygon(trapi);
}
public static void main(String[] args)
{
JFrame win = new JFrame("Polygon Demo");
win.setSize(1024,768);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add( new PolygonDemo() );
win.setVisible(true);
}
}
//1. The Green Pentagon on the screen becomes deformed when the order of the points are being changed.