Java 使用 Observer 重绘无效

Java repainting with Observer didn't work

嘿,我的掷骰子游戏有点问题。问题是什么时候 我按下按钮以获得新的骰子数字,我的面板没有重新绘制。我使用观察者模式来更新我的面板。当我想要更新标签颜色时,我的观察器工作正常,但当我想要更新 Graphics.filloval 时它不起作用。有人知道为什么我的 paintcomponent 没有重新绘制 ??!,

谢谢

骰子class

public class Dice {

  private  int value = 0;
  private java.util.List<DobbelObserver> observers;

  public Dice()
  {
    value = 0;
    observers = new ArrayList<>();
  }

  public int getValue()
  {
    return value;
  }

  public void ThrowDice()
  {
    value = new Random().nextInt(6) + 1;
    System.out.println(value);
    notifyObservers();
  }

  public void addObserver(DobbelObserver observer)
  {
    observers.add(observer);
  }

  public void notifyObservers()
  {
    for(DobbelObserver observer : observers)
    {
        observer.update();
    }
  }
}

帧数

public class DobbelFrame extends JFrame {

  public DobbelFrame()
  {
    Dice dice = new Dice();

    setTitle("Dobbelstenen");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
    setFocusable(true);
    setContentPane(new DobbelPanel(dice));
    pack();
  }
}

观察者

public interface DobbelObserver {

  void update();
}

面板

public class DobbelPanel extends JPanel implements DobbelObserver{

  private JButton btn1;
  private Dice wdice;

  public DobbelPanel(Dice dice)
  {
    this.wdice = dice;
    setPreferredSize(new Dimension(400,400));

    addComponents();

    addActionListeners();
    dice.addObserver(this);
  }

  private void addComponents()
  {
    btn1 = new JButton("Genereer");
    add(btn1);
  }

  public void addActionListeners()
  {
    btn1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            wdice.ThrowDice();
        }
    });
  }

  public void paintComponent(Graphics g)
  {
    if(wdice.getValue() == 0) {
        g.drawRect(0,0,100,100);

    } else if (wdice.getValue() == 1) {
        g.fillOval(40, 40, 15, 15);

    } else if (wdice.getValue() == 2) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
    } else if (wdice.getValue() == 3) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(40, 40, 15, 15);
        g.fillOval(10, 75, 15, 15);
    } else if (wdice.getValue() == 4) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
        g.fillOval(10, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
    } else if (wdice.getValue() == 5) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
       g.fillOval(10, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
        g.fillOval(40, 40, 15, 15);

    } else if (wdice.getValue() == 6) {
        g.fillOval(10, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
        g.fillOval(75, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
       g.fillOval(10, 40, 15, 15);
        g.fillOval(75, 40, 15, 15);

    }

  }

  @Override
  public void update()
  {
    repaint();
  }
}
public void paintComponent(Graphics g) {

   //This solved the problem
    super.paintComponent(g);



    g.setColor(Color.magenta);
    if (wdice.getValue() == 0) {
        g.drawRect(0, 0, 100, 100);


    } else if (wdice.getValue() == 1) {
        g.fillOval(40, 40, 15, 15);

    } else if (wdice.getValue() == 2) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
    } else if (wdice.getValue() == 3) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(40, 40, 15, 15);
        g.fillOval(10, 75, 15, 15);
    } else if (wdice.getValue() == 4) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
        g.fillOval(10, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
    } else if (wdice.getValue() == 5) {
        g.fillOval(75, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
        g.fillOval(10, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
        g.fillOval(40, 40, 15, 15);

    } else if (wdice.getValue() == 6) {
        g.fillOval(10, 10, 15, 15);
        g.fillOval(10, 75, 15, 15);
        g.fillOval(75, 10, 15, 15);
        g.fillOval(75, 75, 15, 15);
        g.fillOval(10, 40, 15, 15);
        g.fillOval(75, 40, 15, 15);

    }