2

所以基本上我正在尝试创建一个黑白棋游戏。首先,我创建了一个由按钮填充的板,并为它们附加了 ID,因此我可以在需要时访问它们。现在我正在尝试在每个按钮上绘制一个游戏块,但是我无法获取按钮的 getGraphics(),因为我读到这是一个坏主意并且还返回 null。请记住,我希望将所有实体分开:板、单元和部件,因为我使用 MVC 模式开发它。

板子.java

import java.awt.GridLayout;
import javax.swing.JPanel;

public class Board extends JPanel {
    private static final int sizeOfBoard = 8;

    public Board() {
        int id =0;
        setLayout(new GridLayout(sizeOfBoard,sizeOfBoard));
        for (int i = 0; i < sizeOfBoard; i++) {
            for (int j = 0; j < sizeOfBoard; j++) {
                Cell cell = new Cell(id++);
                Disk disk = new Disk();
                cell.add(disk);
                add(cell);
            }
        }

        setSize(600,500);
        setVisible(true);
    }

细胞.java

import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.Painter;

    public class Cell extends JButton{
        private int id;
        private boolean taken;
        private String colour;
        private Painter painter;

        public Cell(int id){
            this.id = id;
        }

        public int getId(){
            return id;
        }

        @Override
        public void paintComponent(Graphics g){
            super.paintComponent(g);   
        }
    }

磁盘.java

import java.awt.Graphics;
import javax.swing.JComponent;


public class Disk extends JComponent{

    @Override
    public void paintComponent ( Graphics g ) {
        super.paintComponent(g);
        g.drawOval(50,50,50,50);
    }
}

TL;DR我应该如何重写我的代码,以便每个按钮上都有一个椭圆形。

提前致谢。

4

1 回答 1

3

最简单的解决方案:在 BufferedImage 中创建椭圆形或磁盘图像,将其放入 ImageIcon,然后通过其setIcon(myIcon)方法简单地交换 JButton 或 JLabel 上的图标。如果这是我的 GUI,我会创建 3 个 ImageIcon,一个用于初始状态的空白图标,然后为占用状态创建两个不同颜色的图标。

例如:

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;

@SuppressWarnings("serial")
public class ReversiPanel extends JPanel {
    private static final int SIDES = 8;
    private static final int ICON_LENGTH = 60;
    private static final Color BG = Color.BLACK;
    private static final Color LABEL_COLOR = Color.GREEN.darker();
    private JLabel[][] labelGrid = new JLabel[SIDES][SIDES];
    private Icon blankIcon;
    private Icon blackIcon;
    private Icon whiteIcon;

    public ReversiPanel() {
        blankIcon = createIcon(new Color(0, 0, 0, 0));
        blackIcon = createIcon(Color.BLACK);
        whiteIcon = createIcon(Color.WHITE);

        setBackground(BG);
        setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
        setLayout(new GridLayout(SIDES, SIDES, 1, 1));
        MyMouse myMouse = new MyMouse();
        for (int i = 0; i < labelGrid.length; i++) {
            for (int j = 0; j < labelGrid[i].length; j++) {
                JLabel label = new JLabel(blankIcon);
                label.setOpaque(true);
                label.setBackground(LABEL_COLOR);
                label.addMouseListener(myMouse);
                labelGrid[i][j] = label;
                add(label);
            }
        }
    }

    private Icon createIcon(Color color) {
        BufferedImage img = new BufferedImage(ICON_LENGTH, ICON_LENGTH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = img.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(color);
        int gap = 4;
        int w = ICON_LENGTH - 2 * gap;
        int h = w;
        g2.fillOval(gap, gap, w, h);
        g2.dispose();
        return new ImageIcon(img);
    }

    private class MyMouse extends MouseAdapter {
        @Override
        public void mousePressed(MouseEvent e) {
            JLabel label = (JLabel) e.getSource();
            Icon icon = label.getIcon();
            if (icon == blankIcon) {
                label.setIcon(blackIcon);
            } else if (icon == blackIcon) {
                label.setIcon(whiteIcon);
            } else {
                label.setIcon(blankIcon);
            }
        }
    }

    private static void createAndShowGui() {
        ReversiPanel mainPanel = new ReversiPanel();

        JFrame frame = new JFrame("ReversiPanel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}
于 2017-11-19T20:31:06.150 回答