-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
61 lines (54 loc) · 2.16 KB
/
Copy pathTriangle.java
File metadata and controls
61 lines (54 loc) · 2.16 KB
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
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Polygon;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
public class Triangle extends Shape {
// class for creating the triangle shape, this class extends the abstract class Shape
public Triangle(int length, int i, int j) {
super(length, Type.TRIANGLE, i, j);
isConnected = new boolean[this.type.getValue()];
for (int ii = 0; ii < this.type.getValue(); ii++)
neighbors.add(null);
}
/*
drawing method:
checks the routes/edges connecting the different squares and
paint them with the corresponding visted color
*/
@Override
public void draw(Graphics g) {
if (g == null) return;
Graphics2D g2 = (Graphics2D) g.create();
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(this.explored.getColor());
double side = this.length * 3 / 4.0;
double height = Math.sqrt(3) * side;
int orientation = (this.i + this.j) % 2;
int cy = (this.i + 1);
int cx = (this.j + 2);
int[] xs = { (int) ((cx - orientation) * side), (int) ((cx - 1 + orientation) * side), (int) ((cx + 1) * side)};
int[] ys = { (int) ((cy + 1) * height), (int) (cy * height), (int) ((cy + orientation) * height) };
Polygon p = new Polygon(xs, ys, 3);
g2.fillPolygon(p);
g2.drawPolygon(p);
g2.setStroke(new BasicStroke(2));
for (int edge = 0; edge < 3; edge++) {
if (!isConnected[edge]) continue;
int next = (edge + 1) % 3;
g2.drawLine(xs[edge], ys[edge], xs[next], ys[next]);
}
g2.setColor(Color.BLACK);
for (int edge = 0; edge < 3; edge++) {
if (isConnected[edge]) continue;
int next = (edge + 1) % 3;
g2.drawLine(xs[edge], ys[edge], xs[next], ys[next]);
}
} finally {
g2.dispose();
}
}
}