Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JButton的顶部绘制一个椭圆形_Java_Swing_Graphics_Reversi_Othello - Fatal编程技术网

Java 在JButton的顶部绘制一个椭圆形

Java 在JButton的顶部绘制一个椭圆形,java,swing,graphics,reversi,othello,Java,Swing,Graphics,Reversi,Othello,所以基本上我在尝试创建一个reversi游戏。首先,我创建了一个由按钮和附加ID填充的板,这样我可以在需要时访问它们。现在我试图在每个按钮上绘制一个游戏块,但是我无法获取按钮的graphics(),因为我读到这是一个坏主意,并且返回null。请记住,我希望将所有实体分开:板、单元和块,因为我使用MVC模式进行开发 board.java import java.awt.GridLayout; import javax.swing.JPanel; public class Board extend

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

board.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);
    }
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);   
        }
    }
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);
    }
}
disk.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);
    }
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);   
        }
    }
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我应该如何重写代码,使每个按钮上都有一个椭圆形


提前感谢。

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

例如:

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());
    }
}
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.GridLayout;
导入java.awt.RenderingHints;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.image.buffereImage;
导入javax.swing.*;
@抑制警告(“串行”)
公共类ReversiPanel扩展了JPanel{
专用静态最终内部侧=8;
私有静态最终整数图标长度=60;
专用静态最终颜色BG=Color.BLACK;
私有静态最终颜色标签_Color=Color.GREEN.darker();
专用JLabel[]labelGrid=新JLabel[SIDES][SIDES];
私人图标空白图标;
私人图标黑图标;
私人图标白色图标;
公共反向投资(){
blankIcon=createIcon(新颜色(0,0,0,0));
blackIcon=createIcon(Color.BLACK);
whiteIcon=createIcon(Color.WHITE);
退避地(BG);
setBorder(BorderFactory.createEmptyByOrder(1,1,1,1));
setLayout(新网格布局(边、边、1、1));
MyMouse MyMouse=新建MyMouse();
对于(int i=0;icreateAndShowGui());
}
}

@RMS:例如,编译并运行上面的代码。它使用JLabels,因为我觉得它更干净,但是JButton也可以。非常感谢。这种方法看起来比我一直在做的任何事情都要好。非常感谢。