如何制作六边形按钮

4
我写了一个简短的游戏。在现有的实现中,我使用GridBagLayout将按钮放置为棋盘。每个按钮占据整个网格。游戏运行良好。我的下一个任务是将棋盘更改为由六边形按钮组成,而不是像现在这样的矩形。我完全不知道如何做到这一点。按钮应该看起来像图片上的那样: target

2
那些不是六面体。 - Dawood ibn Kareem
2
实际上,关于JButton的形状有很多帖子,但我不想浏览它们以找到答案。这些可能会有所帮助 - user7627726
可能是除了矩形以外的形状中的按钮的重复问题。 - user7627726
2
你有两个问题,第一个是如何生成形状,第二个是如何对齐。即使您使用 JButton 并自己绘制形状、使用 BorderImage,它仍然是矩形,因此几乎所有的布局管理器都无法按照您想要的方式排列它们。这将需要完全自定义绘画路线或自定义布局管理器。 - MadProgrammer
2
请参见创建10,000个相连六边形页面; 使用Polygon::contains进行命中测试。 - trashgod
你能否请发一下你的代码?然后我会看看我能做些什么。 - user7627726
1个回答

2
这不是最美观的方法,但至少可以给你一个想法:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HexagonPattern extends JPanel {
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 7;
    private static final int COLUMNS = 7;
    private HexagonButton[][] hexButton = new HexagonButton[ROWS][COLUMNS];


    public HexagonPattern() {
        setLayout(null);
        initGUI();
    }


    public void initGUI() {
        int offsetX = -10;
        int offsetY = 0;

        for(int row = 0; row < ROWS; row++) {
            for(int col = 0; col < COLUMNS; col++){
                hexButton[row][col] = new HexagonButton(row, col);
                hexButton[row][col].addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        HexagonButton clickedButton = (HexagonButton) e.getSource();
                        System.out.println("Button clicked: [" + clickedButton.getRow() + "][" + clickedButton.getCol() + "]");
                    }
                });
                add(hexButton[row][col]);
                hexButton[row][col].setBounds(offsetY, offsetX, 105, 95);
                offsetX += 87;
            }
            if(row%2 == 0) {
                offsetX = -52;
            } else {
                offsetX = -10;
            }
            offsetY += 76;
        }
    }

    public static void main(String[] args) {
        HexagonPattern hexPattern = new HexagonPattern();
        JFrame frame = new JFrame();
        frame.setTitle("Hexagon Pattern");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(new Point(700, 300));
        frame.add(hexPattern);
        frame.setSize(550, 525);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    //Following class draws the Buttons
    class HexagonButton extends JButton {
        private static final long serialVersionUID = 1L;
        private static final int SIDES = 6;
        private static final int SIDE_LENGTH = 50;
        public static final int LENGTH = 95;
        public static final int WIDTH = 105;
        private int row = 0;
        private int col = 0;

        public HexagonButton(int row, int col) {
            setContentAreaFilled(false);
            setFocusPainted(true);
            setBorderPainted(false);
            setPreferredSize(new Dimension(WIDTH, LENGTH));
            this.row = row;
            this.col = col;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Polygon hex = new Polygon();
            for (int i = 0; i < SIDES; i++) {
                hex.addPoint((int) (50 + SIDE_LENGTH * Math.cos(i * 2 * Math.PI / SIDES)), //calculation for side
                        (int) (50 + SIDE_LENGTH * Math.sin(i * 2 * Math.PI / SIDES)));   //calculation for side
            }       
            g.drawPolygon(hex);
        }

        public int getRow() {
            return row;
        }

        public int getCol() {
            return col;
        }
    }
}

试一下吧!

这个程序由2个类组成:

  1. HexagonButton,使用Graphics在JButton中绘制六边形。当调用getRowgetCol时,它还会返回行和列的值。

  2. HexagonPattern是主类。它通过使用setBounds(x, y, width, height)来布置六边形图案。它使用ActionListener通过调用getRowgetCol来打印单击的六边形的坐标。

就像我说的那样,这不是最好的程序。如果你想让六边形变小,那么你将不得不改变很多变量。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接