Honeycombs: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: added Java)
(→‎{{header|Java}}: improved code layout)
Line 1,128: Line 1,128:


class HoneycombsPanel extends JPanel {
class HoneycombsPanel extends JPanel {

class Hexagon extends Polygon {
final Color baseColor = Color.yellow;
final Color selectedColor = Color.magenta;
final char letter;

private boolean hasBeenSelected = false;

Hexagon(int x, int y, int halfWidth, char c) {
letter = c;
for (int i = 0; i < 6; i++)
addPoint((int) (x + halfWidth * Math.cos(i * Math.PI / 3)),
(int) (y + halfWidth * Math.sin(i * Math.PI / 3)));
getBounds();
}

void setSelected() {
hasBeenSelected = true;
}

void draw(Graphics2D g) {
g.setColor(hasBeenSelected ? selectedColor : baseColor);
g.fillPolygon(this);

g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawPolygon(this);

g.setColor(hasBeenSelected ? Color.black : Color.red);
drawCenteredString(String.valueOf(letter), g);
}

void drawCenteredString(String s, Graphics2D g) {
g.setFont(new Font("SansSerif", Font.BOLD, 30));

FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int dec = fm.getDescent();

int x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2;
int y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2);

g.drawString(s, x, y);
}
}


Hexagon[] comb;
Hexagon[] comb;
Line 1,210: Line 1,165:
int x1 = 150, y1 = 100, x2 = 225, y2 = 143, w = 150, h = 87;
int x1 = 150, y1 = 100, x2 = 225, y2 = 143, w = 150, h = 87;
for (int i = 0; i < comb.length; i++) {
for (int i = 0; i < comb.length; i++) {
int x = i < 12 ? x1 + (i % 3) * w : x2 + (i % 2) * w;
int x, y;
int y = i < 12 ? y1 + (i / 3) * h : y2 + ((i - 12) / 2) * h;
if (i < 12) {
x = x1 + (i % 3) * w;
y = y1 + (i / 3) * h;
} else {
x = x2 + (i % 2) * w;
y = y2 + ((i - 12) / 2) * h;
}
comb[i] = new Hexagon(x, y, w / 3, letters[i]);
comb[i] = new Hexagon(x, y, w / 3, letters[i]);
}
}
Line 1,227: Line 1,188:
for (Hexagon hex : comb)
for (Hexagon hex : comb)
hex.draw(g);
hex.draw(g);
}
}

class Hexagon extends Polygon {
final Color baseColor = Color.yellow;
final Color selectedColor = Color.magenta;
final char letter;

private boolean hasBeenSelected;

Hexagon(int x, int y, int halfWidth, char c) {
letter = c;
for (int i = 0; i < 6; i++)
addPoint((int) (x + halfWidth * Math.cos(i * Math.PI / 3)),
(int) (y + halfWidth * Math.sin(i * Math.PI / 3)));
getBounds();
}

void setSelected() {
hasBeenSelected = true;
}

void draw(Graphics2D g) {
g.setColor(hasBeenSelected ? selectedColor : baseColor);
g.fillPolygon(this);

g.setStroke(new BasicStroke(3));
g.setColor(Color.black);
g.drawPolygon(this);

g.setColor(hasBeenSelected ? Color.black : Color.red);
drawCenteredString(String.valueOf(letter), g);
}

void drawCenteredString(String s, Graphics2D g) {
g.setFont(new Font("SansSerif", Font.BOLD, 30));

FontMetrics fm = g.getFontMetrics();
int asc = fm.getAscent();
int dec = fm.getDescent();

int x = bounds.x + (bounds.width - fm.stringWidth(s)) / 2;
int y = bounds.y + (asc + (bounds.height - (asc + dec)) / 2);

g.drawString(s, x, y);
}
}
}</lang>
}</lang>