medyormandy medyormandy - 7 months ago 20
Java Question

Repainting with values in LinkedStack not working

I am making a game, and whenever I'm going to press the new game button, my canvas doesn't update, but my LinkedStack values have already been updated. It's supposedly have shuffled the LinkedStack values.

[EDIT]



-added a few parts in my code, and the main problem in my code is that whenever I press new game, the nodes show the correct new value, but whenever it is passed to paint(g) (or repaint()), the nodes still houses the first values.

This is a part of my canvas code. (This class extends 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
}


This is part of my main class that implements JFrame. This block of code executes once I press "New Game".

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();
}
}

Answer

Here:

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

You're creating a new Solitaire object but not displaying it anywhere. The original Solitaire object sits in your GUI unchanged.

Options:

  • Remove the original Solitaire object and add the new one to the GUI
  • Then call revalidate() and repaint() on the container that holds it.
  • Even better, improve Solitaire so that you don't have to create a new one but rather simply call reset() on the original one and it shuffles and renews itself.
  • Again, don't use Canvas but rather JPanel, don't override paint but rather paintComponent, and call the super's painting method.
Comments