-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolygonDemo.java
66 lines (55 loc) · 1.72 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
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 trap = new Polygon();
trap.addPoint(600, 245);
trap.addPoint(700, 245);
trap.addPoint(775, 350);
trap.addPoint(550, 350);
g.drawPolygon(trap);
Polygon pent = new Polygon();
// moving around the order of the points can cause a different
// shape to be drawn, because the first and last point must
// always be directly linked
pent.addPoint(450, 200);
pent.addPoint(425, 350);
pent.addPoint(475, 350);
pent.addPoint(400, 250);
pent.addPoint(500, 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);
}
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);
}
}