forked from qVira/Gemblo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hexagon.java
96 lines (75 loc) · 2.94 KB
/
Hexagon.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package Gemblo;
import java.awt.*;
import java.awt.geom.Path2D;
/**
* Helping to draw a single hexagon on the board with the given center coordinates, size, color and text
*/
public class Hexagon {
private final double x, y; // center coordinates
private final int size; // size of the hexagon side
private final Color color;
private String text;
private int angle;
public Hexagon(double x, double y, int size, Color color, int r, int c, int angle) {
this.x = x;
this.y = y;
this.size = size;
this.color = color;
this.text = r + "," + c;
this.angle = angle;
}
public void draw(Graphics2D g2) {
double[] xPoints = new double[6];
double[] yPoints = new double[6];
for (int i = 0; i < 6; i++) {
xPoints[i] = x + (size * Math.cos(i * Math.PI / 3 + Math.PI / 6)); // Rotate by 60 degrees
yPoints[i] = y + (size * Math.sin(i * Math.PI / 3 + Math.PI / 6));
}
Path2D hexagon = new Path2D.Double();
hexagon.moveTo(xPoints[0], yPoints[0]);
for (int i = 1; i < 6; i++) {
hexagon.lineTo(xPoints[i], yPoints[i]);
}
hexagon.closePath();
g2.setColor(color);
g2.fill(hexagon);
g2.setColor(Color.BLACK);
g2.draw(hexagon);
}
public void drawText(Graphics2D g2) {
// Draw the text in the center of the hexagon
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(text);
int textHeight = fm.getAscent();
int textX = (int) (x - textWidth / 2);
int textY = (int) (y + textHeight / 4); // adjust this to center vertically
g2.drawString(text, textX, textY);
}
public void drawIndex(Graphics2D g2, String index) {
if (index == "-1") {
return;
}
// Draw the text in the center of the hexagon
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(index);
int textHeight = fm.getAscent();
int textX = (int) (x - textWidth / 2);
int textY = (int) (y + textHeight / 4); // adjust this to center vertically
g2.drawString(index, textX, textY);
}
public void drawDot(Graphics2D g2d) {
int dotRadius = 2; // Radius of the dot
g2d.fillOval((int) (x - dotRadius), (int) (y - dotRadius), 2 * dotRadius, 2 * dotRadius);
}
public void drawAngle(Graphics2D g2) {
FontMetrics fm = g2.getFontMetrics();
int textWidth = fm.stringWidth(Integer.toString(angle));
int textHeight = fm.getAscent();
int textX = (int) (x - textWidth / 2);
int textY = (int) (y + textHeight / 4); // adjust this to center vertically
g2.drawString(Integer.toString(angle), textX, textY);
}
public void setAngle(int newAngle) {
this.angle = newAngle;
}
}