簡體   English   中英

如何在Java Applet中更改數組中的特定圖片?

[英]How to change specific pictures in arrays in java applets?

我正在制作蛇棋和梯子游戲,但有一個小問題。 我無法動彈。

基本上我有一個64塊網格,其中有64張圖片。 我也有兩張圖片,它們的大小與我用來移動的瓷磚的大小相同。

當我單擊JButton時,圖片應該更新了。

這是我的代碼以及我嘗試過的內容:

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

該代碼運行,但沒有圖片更新。 我確定我在邏輯上犯了一個錯誤,而且我不知道如何將數組中的圖片更改為player1和player2。 謝謝你的幫助。

編輯:當我再次點擊滾動按鈕時,我也想找出如何刪除以前的圖標。 謝謝

您沒有將創建的新標簽添加到g ,因此g無法調用paint方法來渲染標簽。

可以使用JLabel::setIcon來更改圖像,而不是創建一個新的JLabel

// a[p1space] = new JLabel (createImageIcon ("p1.jpg"));
a[p1space].setIcon(createImageIcon ("p1.jpg"));

在執行更改后,您需要調用revalidate()和repaint()方法,這將在您的applet中重新加載您的更改。 在您的方法末尾添加以下兩個方法

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM