java - 如何制作六角形 JButton

标签 java swing jbutton hexagonal-tiles

我写了一个小游戏。在现有的实现中,我有一个带有按钮的 GridBagLayout 作为棋盘。每个按钮占据整个网格。游戏运行良好。我的下一个任务是将面板更改为由六角形按钮组成,而不是像现在这样的矩形。我完全不知道该怎么做。按钮在图片上应如下所示: target

最佳答案

这不是最漂亮的方式,但它至少会给你一个想法:

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;
        }
    }
}

测试一下!

这个程序包含两个类:

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

  2. HexagonPattern,这是主类。它通过使用 setBounds(x, y, width, height) 对它们进行布局来制作图案。它使用 ActionListener 通过调用 getRowgetCol 打印所点击六边形的坐标。

就像我说的,这不是最好的程序。如果要使六边形变小,则必须更改许多变量。

关于java - 如何制作六角形 JButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44012684/

相关文章:

java - 我的 Java GUI 中已突出显示按钮

java - 在java中单击按钮时自动单击特定选项卡

java - H.225、h.245 和 iax2 消息格式是什么?想要进行签名分析,但我的数据包已加密

java - 如何在PLAY框架中导入JAVA自定义包

java - 为什么我导入的 containsString 方法不起作用?

java - 如何阻止 ActionListener 停止所有其他代码?

java - 为 JButton 设置键盘快捷键?

java - 测试容器在等待容器端口打开时超时,使用 Elasticsearch docker 镜像

java - 我怎样才能 "erase"我在 Java Graphics2D Canvas 上绘制的东西而不删除它后面的东西?

java - 如何解决JTree中的显示问题?