import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import javax.swing.JButton;
import javax.swing.border.Border;
public class HexButton extends JButton {
// das Polygon beschreibt die Form
private Polygon shape;
private boolean showBorder;
private Color borderColor, foregroundColor, buttonPressedColor;
public HexButton(int sideLength) {
this.shape = new Polygon();
// initialisiere Form
this.initialize(sideLength);
this.setContentAreaFilled(false);
this.showBorder = true;
this.setOpaque(false);
this.foregroundColor = Color.LIGHT_GRAY;
this.borderColor = Color.BLACK;
this.buttonPressedColor = Color.GRAY;
this.setLayout(null);
}
public void showBorder(boolean b) {
this.showBorder = b;
this.repaint();
}
protected void initialize(int sideLength) {
Point p1, p2, p3, p4, p5, p6;
this.setSize(2 * sideLength + 1, (int) (Math.round(Math.sin(Math.PI / 3)
* sideLength) * 2));
// Berechnung der 6 Ecken des Hexagons mit Hilfe der Seitenlänge
p1 = new Point(Math.round(sideLength / 2), 0);
p2 = new Point(Math.round(sideLength / 2) + sideLength, 0);
// bei einer ungeraden Seitenlänge wird Punkt3 um einen Pixel nach links
// versetzt
if (sideLength % 2 != 0) {
p3 = new Point((2 * sideLength - 1), this.getHeight() / 2);
} else {
p3 = new Point(2 * sideLength, this.getHeight() / 2);
}
p4 = new Point(Math.round(sideLength / 2) + sideLength, (2 * this
.getHeight() / 2) - 1);
p5 = new Point(Math.round(sideLength / 2), (2 * this.getHeight() / 2) - 1);
p6 = new Point(0, this.getHeight() / 2);
this.shape.addPoint((int) Math.round(p1.getX()), (int) Math.round(p1
.getY()));
this.shape.addPoint((int) Math.round(p2.getX()), (int) Math.round(p2
.getY()));
this.shape.addPoint((int) Math.round(p3.getX()), (int) Math.round(p3
.getY()));
this.shape.addPoint((int) Math.round(p4.getX()), (int) Math.round(p4
.getY()));
this.shape.addPoint((int) Math.round(p5.getX()), (int) Math.round(p5
.getY()));
this.shape.addPoint((int) Math.round(p6.getX()), (int) Math.round(p6
.getY()));
this.setMinimumSize(this.getSize());
this.setMaximumSize(this.getSize());
this.setPreferredSize(this.getSize());
}
// Hit detection
public boolean contains(int x, int y) {
return this.shape.contains(x, y);
}
// Zeichne den Button
protected void paintComponent(Graphics g) {
Graphics2D gCopy = (Graphics2D) g.create();
if (getModel().isArmed()) {
gCopy.setColor(this.buttonPressedColor);
} else {
gCopy.setColor(this.foregroundColor);
}
gCopy.fillPolygon(this.shape);
}
// zeichne die Border
protected void paintBorder(Graphics g) {
if (this.showBorder) {
Graphics2D gCopy = (Graphics2D) g.create();
gCopy.setColor(this.borderColor);
gCopy.drawPolygon(this.shape);
}
}
public Color getBorderColor() {
return borderColor;
}
public void setBorderColor(Color borderColor) {
this.borderColor = borderColor;
}
public Color getButtonPressedColor() {
return buttonPressedColor;
}
public void setButtonPressedColor(Color buttonPressedColor) {
this.buttonPressedColor = buttonPressedColor;
}
public Color getForegroundColor() {
return foregroundColor;
}
public void setForegroundColor(Color foregroundColor) {
this.foregroundColor = foregroundColor;
}
public Polygon getShape() {
return shape;
}
public void setShape(Polygon shape) {
this.shape = shape;
}
}