在Square类中创建paint Graphics g破坏了我的GUI

我正在尝试登上游戏模拟器。我的方块是JButton,看起来不错,直到我使用fillOval函数添加一个椭圆形来指示玩家在棋盘上的位置。

我的面板是由方形按钮构建的,这里是Square类:

const {state,dispatch} = React.useContext(Store)

和GUI类

public class Square extends JButton{

    private boolean occupied;
    private Player occupant;    
    private int position,num;
    private Board board;

    public Square (int position) {
        this.setPreferredSize(new Dimension(85,85));
        this.setText(Integer.toString(position + 1));
        this.setfont(new Font("Tempus Sans ITC",Font.BOLD,20));
        this.position = position;
        occupied = false;
        occupant = null;
    }

    public int getPosition() {
        return position;
    }


    public boolean isOccupied() {
        return this.occupied;
    }


    public Player getOccupant() {
        if(this.isOccupied())
            return this.occupant;

        return null;
    }

    public void enter ( Player p) {
         this . enter (p);
         }
             public void leave ( Player p) {
         this . leave (p);
         }
    public void setOccupant(Player visitor) {
        this.occupant = visitor;
        this.occupied = true;

        }

public void paint(Graphics g) {

        if(this.isOccupied()) {

            g.setColor(occupant.getcolor());
            g.fillOval(30,30,50,50);


        }
}

这是添加fillOval之前的样子

在Square类中创建paint Graphics g破坏了我的GUI

这是添加fillOval后GUI的外观

在Square类中创建paint Graphics g破坏了我的GUI

我该如何解决? 预先谢谢你

lw9776535 回答:在Square类中创建paint Graphics g破坏了我的GUI

您必须致电super.paint()

public void paint(Graphics g) {
    super.paint(g);
    if(this.isOccupied()) {
        g.setColor(occupant.getColor());
        g.fillOval(30,30,50,50);
    }
}
本文链接:https://www.f2er.com/3154593.html

大家都在问