- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在制作一个游戏,每当我要按下新游戏按钮时,我的 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 中保持不变。
选项:
revalidate()
和 repaint()
。reset()
,它就会自行洗牌和更新。关于java - 使用 LinkedStack 中的值重新绘制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36822973/
我在这些问题上遇到了困难。我有 4 个问题要让它工作。我的其中两个是正确的,但另外两个是错误的。它是 java 的 toString(): LLNode node = top; Str
所以,我开始自己在 LinkedStack 上编码,我最终缩小了错误发生的范围,但我不知道如何修复它。我会在这里发布我的代码和错误,看看是否有人能弄清楚。谢谢 LinkedNode.h #pragma
所以我有这个链接节点堆栈(简称链接堆栈)的代码,它让我非常困惑!我明天要考试,这让我很困惑!所以看看: class Node { T item;
我正在制作一个游戏,每当我要按下新游戏按钮时,我的 Canvas 不会更新,但我的 LinkedStack 值已经更新。据说它已经打乱了 LinkedStack 值。 [编辑] -在我的代码中添加了一
我做了很多研究,但所有这些要么是递归,要么不是我目前正在寻找的。我正在尝试使用 LinkedStack 而不是递归创建 N-Queens 程序,LinkedStack 将采用对象 NQueen,而不仅
我对这项作业有一个小问题:我打算完成 LinkedStack 实现并确保 peek、isEmpty 和 size 方法按照 Stack 接口(interface)中的定义实现。我相信我的大部分工作都正
我有一个 LinkedStack.h 并且我已经定义了一些方法。我的问题是如何将该文件包含在 Qt creator 中以及如何编写它以便我可以在我的 GUI 程序中定义一个 LinkedStack 类
如何在 O(1) 中为 LinkedStack 编写一个 pop() 方法?我的 LinkedStack 类中有两个私有(private)数据成员:ListNode* head 和 ListNode*
我是一名优秀的程序员,十分优秀!