2

I'm making a Snakes and Ladders game and I have one small problem. I can't get the pieces to move.

Basically I have a 64-tile grid with 64 pictures in an array. I also have 2 pictures of the same size as the tiles which I use as pieces that move.

The pictures are supposed to update when I click the JButton to do so.

Here is my code and what I have tried:

public class SnakesandLadders extends Applet implements ActionListener
{

//Part of the grid
int row = 8;
JLabel a[] = new JLabel [(row*row) + 1];
Panel g = new Panel (new GridLayout (row, row));
JLabel grid;
JButton roll;
int playerNum = 1;
int p1space = 0;
int p2space = 0;

public void init ()
{       
    //The Roll Button
    roll = new JButton ("ROLL");
    roll.addActionListener (this);
    roll.setActionCommand ("roll");

    //The Grid Displayed using a for loop
    for (int rownum = 7 ; rownum >= 0 ; rownum--)
    {
    int number = rownum*8;
        if (rownum % 2 != 0)
        {
            for (int i = 7 ; i >= 0 ; i--)
            {
                a [i+number] = new JLabel (createImageIcon ((i+number) + ".jpg"));
                g.add (a [i+number]);
            }
        }
        else
        {
            for (int i = 0; i < 8; i++)
            {   
                a [i+number] = new JLabel (createImageIcon ((i+number) + ".jpg"));
                g.add (a [i+number]);
            }
        }
    }
        add (g);
        add (roll);
    }

    public void actionPerformed (ActionEvent e)
    {

        if (e.getActionCommand ().equals ("roll"))
        {//NEED TO FIX THIS PART
            {
                int n = (int) ((Math.random () * 6) + 1);
                playerNum++;
                //To choose which picture to update
                if (playerNum % 2 != 0) {
                    turn.setText ("It is Player 1's Turn");
                    p1space = p1space + n;
                    a[p1space] = new JLabel (createImageIcon ("p1.jpg"));
                    }

                else {
                    turn.setText ("It is Player 2's Turn");
                    p2space = p2space + n;
                    a[p2space] = new JLabel (createImageIcon("p2.jpg")); 
                    }
           }
    }

    //The picture update method                
    protected static ImageIcon createImageIcon (String path)
    {
        java.net.URL imgURL = SnakesandLadders.class.getResource (path);
        if (imgURL != null)
        {
            return new ImageIcon (imgURL);
        }
        else
        {
            System.err.println ("Couldn't find file: " + path);
            return null;
        }
    }
}

The code runs but none of the picture update. I'm sure I made a mistake in the logic, and I can't figure out how to change the pictures in the array to the player1 and player2 pieces. Thanks for any help.

EDIT: I ALSO WANT TO FIND OUT HOW TO REMOVE THE PREVIOUS ICON WHEN I CLICK THE ROLL BUTTON AGAIN. THANKS

  • *I ALSO WANT .."* FFS Find your caps-lock key & deisengage it.. Once you've done that I might come back and read the rest of it. – Andrew Thompson Jun 12 '16 at 09:16
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Jun 12 '16 at 09:17
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 12 '16 at 09:17

2 Answers2

0

You are not adding the new label you create to g, so g can not call the paint method to render the label.

Instead of creating a new JLabel, you could use JLabel::setIcon to change the image:

// a[p1space] = new JLabel (createImageIcon ("p1.jpg"));
a[p1space].setIcon(createImageIcon ("p1.jpg"));
Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
0

You need to call revalidate() and repaint() methods after performing your changes which will reload your changes in your applet. Add these 2 methods at end of your method as following

 public void actionPerformed (ActionEvent e)
 {
   .....
   g.revalidate();
   g.repaint();
 }