I'm currently developing a Hidato Puzzle solver for a university project. I'm using Swing and Intellij IDE.
One of the requirements is to have Square, Hexagonal and Triangular shapes. Square shapes are easily, but to implement the HexagonalGrid i wrote a custom Hexagonal Button extending JButton.
However, when trying to render a Grid i get this result. I don't know what's wrong.
I got the Hexagonal Math from this website which is aparently regarded online as the HexGrid Bible.
Here's the code of the Hexagonal Button and the Grid renderer.
import javax.swing.*;
import java.awt.*;
public class HexagonButton extends JButton {
private Shape hexagon;
float size;
float width, height;
public HexagonButton(float centerX, float centerY, float size){
Polygon p = new Polygon();
this.size = size;
p.reset();
for(int i=0; i<6; i++){
float angleDegrees = (60 * i) - 30;
float angleRad = ((float)Math.PI / 180.0f) * angleDegrees;
float x = centerX + (size * (float)Math.cos(angleRad));
float y = centerY + (size * (float)Math.sin(angleRad));
p.addPoint((int)x,(int)y);
}
width = (float)Math.sqrt(3) * size;
height = 2.0f * size;
hexagon = p;
}
public void paintBorder(Graphics g){
((Graphics2D)g).draw(hexagon);
}
public void paintComponent(Graphics g){
((Graphics2D)g).draw(hexagon);
}
@Override
public Dimension getPreferredSize(){
return new Dimension((int)width, (int)height);
}
@Override
public Dimension getMinimumSize(){
return new Dimension((int)width, (int)height);
}
@Override
public Dimension getMaximumSize(){
return new Dimension((int)width, (int)height);
}
@Override
public boolean contains(int x, int y){
return hexagon.contains(x,y);
}
}
Public class Main extends JFrame implements MouseListener {
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
public static void main(String[] args) {
// write your code here
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
Main main = new Main();
main.pack();
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Main(){
super();
int rows = 3;
int cols = 3;
setLayout(new GridLayout(rows,cols,-1,-1));
//grid.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
this.setMinimumSize(new Dimension(173 * rows, 200 * cols+2));
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
float size = 25;
int width = (int)(size * Math.sqrt(3));
int height = (int)(size * 2.0f);
int xOffset = (width / 2);
if(i%2==1){
//Offset odd rows to the right
xOffset += (width/2);
}
int yOffset = height / 2;
int centerX = xOffset + j*width;
int centerY = yOffset + i*height;
HexagonButton hexagon = new HexagonButton(centerX, centerY, size);
hexagon.addMouseListener(this);
hexagon.setMinimumSize(hexagon.getMinimumSize());
hexagon.setMaximumSize(hexagon.getMaximumSize());
hexagon.setPreferredSize(hexagon.getPreferredSize());
//hexagon.setVerticalAlignment(SwingConstants.CENTER);
hexagon.setHorizontalAlignment(SwingConstants.CENTER);
add(hexagon);
}
}
}
}
Does anyone know where the problem might be? I'm still pretty new to Swing