使用Observer重新绘制的Java';行不通
嘿,我的掷骰子游戏有点小问题。问题是什么时候 我按下按钮换一个新的骰子号码,我的面板没有重新上漆。我正在使用观察者模式更新我的面板。当我想要更新标签collor时,我的观察者可以正常工作,但当我想要更新Graphics.Fillova时,它无法工作。有人知道为什么我的油漆组件没有重新喷漆 谢谢 骰子课使用Observer重新绘制的Java';行不通,java,repaint,Java,Repaint,嘿,我的掷骰子游戏有点小问题。问题是什么时候 我按下按钮换一个新的骰子号码,我的面板没有重新上漆。我正在使用观察者模式更新我的面板。当我想要更新标签collor时,我的观察者可以正常工作,但当我想要更新Graphics.Fillova时,它无法工作。有人知道为什么我的油漆组件没有重新喷漆 谢谢 骰子课 public class Dice { private int value = 0; private java.util.List<DobbelObserver> obse
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 interface DobbelObserver {
void update();
}
面板
您是否尝试调试并在代码中添加断点。如果是这样,您是否看到所有正确的值都已更改。根据你提供的代码,我猜可能是两件事。也许您没有正确的参考来重新绘制。或者重新绘制甚至可能没有触发。我认为问题在于重新绘制,而不是通知模式。我们已经解决了这个公共无效paintComponent(Graphics g){super.paintComponent(g);}的问题。请发布并接受自己的答案,以便我们在将来为孩子们保留记录。。。
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);
}