-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexagon.java
More file actions
64 lines (57 loc) · 2.19 KB
/
Copy pathHexagon.java
File metadata and controls
64 lines (57 loc) · 2.19 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
62
63
64
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 Hexagon extends Shape {
// class for creating the Hexagon shape, this class extends the abstract class Shape
public Hexagon(int length, int i, int j) {
super(length, Type.HEXAGON, 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());
int side = this.length;
double xOffset = (Math.sqrt(3) / 2.0) * (this.i % 2);
double cx = (this.j + 1) * Math.sqrt(3) + xOffset;
double cy = (this.i + 1) * 3 / 2.0;
int[] xs = new int[6];
int[] ys = new int[6];
for (int ii = 0; ii < 6; ii++) {
double angle = Math.toRadians(60 * ii + 150);
xs[ii] = (int) ((cx + Math.cos(angle)) * side);
ys[ii] = (int) ((cy + Math.sin(angle)) * side);
}
Polygon hex = new java.awt.Polygon(xs, ys, 6);
g2.fillPolygon(hex);
g2.setStroke(new BasicStroke(2));
for (int edge = 0; edge < 6; edge++) {
if (!isConnected[edge]) continue;
int next = (edge + 1) % 6;
g2.drawLine(xs[edge], ys[edge], xs[next], ys[next]);
}
g2.setColor(Color.BLACK);
for (int edge = 0; edge < 6; edge++) {
if (isConnected[edge]) continue;
int next = (edge + 1) % 6;
g2.drawLine(xs[edge], ys[edge], xs[next], ys[next]);
}
} finally {
g2.dispose();
}
}
}