切换存储在数组中的ImageIcons

时间:2013-05-25 02:18:01

标签: java arrays swing jlabel imageicon

我正在创建一个切换游戏(CANDY CRUSH),其目标是在标签上切换图像图标,直到三个相同颜色的“糖果”并排。

遍历数组后,如何在一行中找到3个相同(或来自同一个.jpg)的ImageIcons,可能使用if语句? (例如,第2行,第3-5栏中有一个)。我后来希望那些3消失。

我只需要一个方法来连续检测同一个ImageIcons中的3个。我如何比较数组中的ImageIcons?

这是显示,它创建下面的图像:( SEE METHOD detectThree())

   import javax.swing.*;
   import java.awt.*;

   import java.awt.image.*;
    // public class MouseEventDemo ... implements MouseListener {
//         //where initialization occurs:
//         //Register for mouse events on blankArea and the panel.
//         blankArea.addMouseListener(this);
//         addMouseListener(this);
//     ...
   public class Display extends JPanel 
   //implements MouseListener
   {
      private ImageIcon[][] candyBoard;
      private JPanel panel;
      public Display()
      {
         candyBoard = new ImageIcon[5][5];
         scramble();//fills candyBoard with random candies
         panel = new JPanel();

         panel.setLayout(new GridLayout(5, 5));

         for(int row = 0; row<candyBoard.length; row++)//for loop traverses array 
         {
            for(int col = 0; col < candyBoard[0].length; col++)
            {
               JLabel virginia = new JLabel();
               virginia.setIcon(candyBoard[row][col]); //and adds a JLabel called Virginia to the JPanel
               panel.add(virginia);
            }
         }
         setLayout(new BorderLayout());//what is BorderLayout?
         add(panel, BorderLayout.CENTER);

        //panel.addMouseListener();
        //addMouseListener();

      }     
      public void scramble()
      {
         for(int row = 0; row < candyBoard.length; row++)
         {
            for(int col = 0; col < candyBoard[0].length; col++)
            {
               int randnum = (int)(Math.random() * 4 + 1);
               if(randnum == 1)
               {
                  candyBoard[row][col] = new ImageIcon("blue.jpg");
               }
               if(randnum == 2)
               {
                  candyBoard[row][col] = new ImageIcon("green.jpg");
               }
               if(randnum == 3)
               {
                  candyBoard[row][col] = new ImageIcon("purple.jpg");
               }
               if(randnum == 4)
               {
                  candyBoard[row][col] = new ImageIcon("red.jpg");
               }
            }
         }
      }
      public static void detectThree(ImageIcon[][] candyBoard, JLabel virginia)
      {
         for(int row = 0; row < candyBoard.length; row++)
         {
            for(int col = 0; col < candyBoard[0].length; col++)
            {
               ImageIcon tempimageicon= candyBoard[row][col];
               Image tempimage = new Image(tempimageicon);
               tempimage.getImage();

            //                      


            }
         }
      }
   }

0 个答案:

没有答案