简体   繁体   中英

How to change specific pictures in arrays in java applets?

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

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"));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM