gpt4 book ai didi

java - 使用 LinkedStack 中的值重新绘制不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 12:41:06 25 4
gpt4 key购买 nike

我正在制作一个游戏,每当我要按下新游戏按钮时,我的 Canvas 不会更新,但我的 LinkedStack 值已经更新。据说它已经打乱了 LinkedStack 值。

[编辑]

-在我的代码中添加了一些部分,我的代码中的主要问题是每当我按下新游戏时,节点都会显示正确的新值,但每当它传递给paint(g)(或repaint())时,节点仍然包含第一个值。

这是我的 Canvas 代码的一部分。 (此类扩展了 Canvas)

public class Solitaire extends Canvas{
public Solitaire(){
setSize(width, height);
//initializes specific LinkedStacks
//creates "cards"
}
public void paint(Graphics g){
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.WHITE);
g.clearRect(0, 0, width, height);
g.setColor(Color.BLACK);
paintSolitaire(g2d);
}
public void paintSolitaire(Graphics2D g2d){
int i=0;
int move=0;
LinkedStack temp=null;

ypos=0;
xpos=30;
while(i!=7){
ypos=220;
move=30;
temp=new LinkedStack();
while(tableau[i].peek()!=null){
temp.push(tableau[i].pop());
}

if(temp.peek()==null){
g2d.drawRect(xpos, ypos, 70, 95);
}
else{
tableauNode=temp.checkTop();
while(tableauNode!=null){
Card card=(Card)tableauNode.data;
if(card.getFaceUp()==true){
ypos=ypos+move;
g2d.drawImage(getImage("Deck/"+card.getImage()+".png"), xpos, ypos, 70, 95, null);
}
else if(card.getFaceUp()==false){
ypos=ypos+move;
g2d.drawImage(getImage("Deck/155.png"), xpos, ypos, 70, 95, null);
}
else{
break;
}
tableauNode=tableauNode.link;
}
}
while(temp.peek()!=null){
tableau[i].push(temp.pop());
}
temp=null;
tableauNode=null;
i++;
xpos=xpos+100;
}
g2d.setFont(new Font("Dialog", Font.PLAIN, 30));
g2d.drawString(occurence, 800, 125);
}
public void update(){
repaint();
}
public void deal(){
//deals "cards" into specific LinkedStack
}
public void shuffle(){
//shuffles LinkedList nodes
}

这是我实现 JFrame 的主类的一部分。一旦我按下“新游戏”,该代码块就会执行。

public class Main extends JFrame implements ActionListener{
private Solitaire solitaire;
private JPanel panel;
public Main(){
panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(this.createMenuBar(), BorderLayout.NORTH);

solitaire=new Solitaire();

solitaire.shuffle();
solitaire.deal();
panel.add(solitaire, BorderLayout.CENTER);
add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setResizable(false);
}
private JMenuBar createMenuBar(){
JMenuBar menuBar;
JMenu menu;
JMenuItem newGame;
menu=new JMenu("Menu");
menuBar.add(menu);
newGame=new JMenuItem("New Game");
newGame.addActionListener(this);
menu.add(newGame);
return menuBar;
public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
switch (command){
case "New Game":
solitaire=new Solitaire();
solitaire.shuffle();
solitaire.deal();
solitaire.update();
break;
}
}
public static void main (String[] args){
Main main=new Main();
}
}

最佳答案

这里:

public void actionPerformed(ActionEvent e){
String command=e.getActionCommand();
switch (command){
case "New Game":
solitaire=new Solitaire();
solitaire.shuffle();
solitaire.deal();
solitaire.update();
break;
}
}

您正在创建一个新的 Solitaire 对象,但没有在任何地方显示它。原始的 Solitaire 对象在 GUI 中保持不变。

选项:

  • 删除原始 Solitaire 对象并将新对象添加到 GUI
  • 然后在容纳它的容器上调用 revalidate()repaint()
  • 更好的是,改进 Solitaire,这样您就不必创建新的 Solitaire,而只需对原始 Solitaire 调用 reset(),它就会自行洗牌和更新。
  • 再次强调,不要使用 Canvas 而应使用 JPanel,不要重写 Paint 而应重写 PaintComponent,并调用 super 的绘画方法。

关于java - 使用 LinkedStack 中的值重新绘制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com