JButton在3秒内改变图像

时间:2014-04-13 12:41:36

标签: java jbutton imageicon

我试图创造一个像旧宝石一样的游戏。几乎一切都结束了。但我有一点问题。当图像并排时,它们会消失,上面的照片会下降。但是这个事件是如此之快。我希望JButton的图像在此事件发生前三秒钟内是X图像。

在这个课程中,位移,繁荣,就像检查一样。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class ButtonActionListener implements ActionListener {
  public JButton previousButton = null; //previousButton is first clicked button
  public int numP, numC; // It's Check the next button
public static int score=0; // Create score
public static JButton[][] buttons; // Create the buttons
    //isBomb is check the crushing the pictures if pictures don't crush, pictures returning the old place. control check the pictures twice.
private boolean isBomb=false , control;
    //isPlay is pausing the game
public static boolean isPlay=true;

    //This method sending the button to levels
public static void sendButton(JButton[][] j){
    buttons=j;
}    

    //This method changing the pictures and check the next button
@Override
public void actionPerformed(ActionEvent e){ 
    if(isPlay==true){
    isBomb=false;

        // currentButton is second clicked button
    JButton currentButton = (JButton)e.getSource();

    if (previousButton == null) {
        previousButton = currentButton;
        return;
    }
        //Chancing the pictures and check the next button
    int numP=Integer.parseInt(((JButton)previousButton).getActionCommand());
    int numC=Integer.parseInt(((JButton)currentButton).getActionCommand());
    if (numP==(numC+1) || numP==(numC-1) || numP==(numC+10) || numP==(numC-10) ){
        Icon previousIcon1 = previousButton.getIcon();
        Icon currentIcon1 = currentButton.getIcon();
        Icon previousIcon = previousButton.getIcon();
        Icon currentIcon = currentButton.getIcon();         
        currentButton.setIcon(previousIcon);
        previousButton.setIcon(currentIcon);


        if(isBomb()==true){
            previousButton = null;              
        }
        else{
            JOptionPane.showMessageDialog(null, "Bad Choice");
            currentButton.setIcon(currentIcon1);
            previousButton.setIcon(previousIcon1);
            previousButton = null; 
        }
    }   

    else
        previousButton=null;    
}
}
    //Crushing event is happening in this method
public boolean isBomb(){
  do{
     control=false;

        //This loop crush the horizontal buttons
    for(int i=1; i<9; i++){
        for(int j=1; j<7; j++){             
            //if(j<4){
            String s0, s1, s2, s3 = null, s4 = null;

                s0 = buttons[i][j].getIcon().toString();
                s1 = buttons[i][j+1].getIcon().toString();
                s2 = buttons[i][j+2].getIcon().toString();
                if(j < 5){
                    s3 = buttons[i][j+3].getIcon().toString();
                    s4 = buttons[i][j+4].getIcon().toString();
                }
                for(int k=i; k>=1 ;k--){                    
                    if(k>1){
                        if(s0.equals(s1) & s1.equals(s2)){
                            if(s2.equals(s3)){
                                if(s3.equals(s4)){
                                    buttons[k][j].setIcon(buttons[k-1][j].getIcon());
                                    buttons[k][j+1].setIcon(buttons[k-1][j+1].getIcon());
                                    buttons[k][j+2].setIcon(buttons[k-1][j+2].getIcon());
                                    buttons[k][j+3].setIcon(buttons[k-1][j+3].getIcon());
                                    buttons[k][j+4].setIcon(buttons[k-1][j+4].getIcon());
                                    score+=500;                                     
                                    refreshScore(String.valueOf(score));
                                    isBomb=true;
                                    control=true;
                                }
                                buttons[k][j].setIcon(buttons[k-1][j].getIcon());
                                buttons[k][j+1].setIcon(buttons[k-1][j+1].getIcon());
                                buttons[k][j+2].setIcon(buttons[k-1][j+2].getIcon());
                                buttons[k][j+3].setIcon(buttons[k-1][j+3].getIcon());
                                score+=400;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;

                            }
                            buttons[k][j].setIcon(buttons[k-1][j].getIcon());
                            buttons[k][j+1].setIcon(buttons[k-1][j+1].getIcon());
                            buttons[k][j+2].setIcon(buttons[k-1][j+2].getIcon());
                            score+=300;                                     
                            refreshScore(String.valueOf(score));
                            isBomb =true;
                            control=true;
                        }                       
                    }
                    //en üst satıra random resim için
                    else{
                        if(s0.equals(s1) & s1.equals(s2)){
                            if(s2.equals(s3)){
                                if(s3.equals(s4)){
                                    buttons[k][j].setIcon(randomIcon.r());
                                    buttons[k][j+1].setIcon(randomIcon.r());
                                    buttons[k][j+2].setIcon(randomIcon.r());
                                    buttons[k][j+3].setIcon(randomIcon.r());
                                    buttons[k][j+4].setIcon(randomIcon.r());
                                    score+=500;                                     
                                    refreshScore(String.valueOf(score));
                                    isBomb=true;
                                    control=true;
                                }
                                buttons[k][j].setIcon(randomIcon.r());
                                buttons[k][j+1].setIcon(randomIcon.r());
                                buttons[k][j+2].setIcon(randomIcon.r());
                                buttons[k][j+3].setIcon(randomIcon.r());
                                score+=400;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;
                            }
                            buttons[k][j].setIcon(randomIcon.r());
                            buttons[k][j+1].setIcon(randomIcon.r());
                            buttons[k][j+2].setIcon(randomIcon.r());
                            score+=300;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;                       
                    }                           
                }
            }           

        }

    }
        //This loop crush vertical buttons
    for(int i=1; i<7; i++){
        for(int j=1; j<9; j++){ 

            String s0, s1, s2, s3=null, s4=null;

                s0 = buttons[i][j].getIcon().toString();
                s1 = buttons[i+1][j].getIcon().toString();
                s2 = buttons[i+2][j].getIcon().toString();
                if(i<5){
                    s3 = buttons[i+3][j].getIcon().toString();
                    s4 = buttons[i+4][j].getIcon().toString();
                }


            for(int k=i; k>=1 ;k--){ 

                if(k == 1){


                    if(s0.equals(s1) & s1.equals(s2)){
                        if(s2.equals(s3)){
                            if(s3.equals(s4)){
                                buttons[k][j].setIcon(randomIcon.r());
                                buttons[k+1][j].setIcon(randomIcon.r());
                                buttons[k+2][j].setIcon(randomIcon.r());
                                buttons[k+3][j].setIcon(randomIcon.r());
                                buttons[k+4][j].setIcon(randomIcon.r());
                                score+=500;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;
                            }
                            buttons[k][j].setIcon(randomIcon.r());
                            buttons[k+1][j].setIcon(randomIcon.r());
                            buttons[k+2][j].setIcon(randomIcon.r());
                            buttons[k+3][j].setIcon(randomIcon.r());
                            score+=400;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;
                        }
                        buttons[k][j].setIcon(randomIcon.r());
                        buttons[k+1][j].setIcon(randomIcon.r());
                        buttons[k+2][j].setIcon(randomIcon.r());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }       
                }
                if(k == 2){

                    if(s0.equals(s1) & s1.equals(s2)){
                        if(s2.equals(s3)){
                            if(s3.equals(s4)){
                                buttons[k][j].setIcon(randomIcon.r());
                                buttons[k+1][j].setIcon(randomIcon.r());
                                buttons[k+2][j].setIcon(randomIcon.r());
                                buttons[k+3][j].setIcon(randomIcon.r());
                                buttons[k+4][j].setIcon(buttons[k-1][j].getIcon());
                                score+=500;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;
                            }
                            buttons[k][j].setIcon(randomIcon.r());
                            buttons[k+1][j].setIcon(randomIcon.r());
                            buttons[k+2][j].setIcon(randomIcon.r());
                            buttons[k+3][j].setIcon(buttons[k-1][j].getIcon());
                            score+=400;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;
                        }
                        buttons[k][j].setIcon(randomIcon.r());
                        buttons[k+1][j].setIcon(randomIcon.r());
                        buttons[k+2][j].setIcon(buttons[k-1][j].getIcon());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }       
                }
                if(k == 3){

                    if(s0.equals(s1) & s1.equals(s2)){
                        if(s2.equals(s3)){
                            if(s3.equals(s4)){
                                buttons[k][j].setIcon(randomIcon.r());
                                buttons[k+1][j].setIcon(randomIcon.r());
                                buttons[k+2][j].setIcon(randomIcon.r());
                                buttons[k+3][j].setIcon(buttons[k-2][j].getIcon());
                                buttons[k+4][j].setIcon(buttons[k-1][j].getIcon());
                                score+=500;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;
                            }
                            buttons[k][j].setIcon(randomIcon.r());
                            buttons[k+1][j].setIcon(randomIcon.r());
                            buttons[k+2][j].setIcon(buttons[k-2][j].getIcon());
                            buttons[k+3][j].setIcon(buttons[k-1][j].getIcon());
                            score+=400;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;
                        }
                        buttons[k][j].setIcon(randomIcon.r());
                        buttons[k+1][j].setIcon(buttons[k-2][j].getIcon());
                        buttons[k+2][j].setIcon(buttons[k-1][j].getIcon());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }
                }
                if(k == 4){

                    if(s0.equals(s1) & s1.equals(s2)){
                        if(s2.equals(s3)){
                            if(s3.equals(s4)){
                                buttons[k][j].setIcon(randomIcon.r());
                                buttons[k+1][j].setIcon(randomIcon.r());
                                buttons[k+2][j].setIcon(buttons[k-3][j].getIcon());
                                buttons[k+3][j].setIcon(buttons[k-2][j].getIcon());
                                score+=400;                                     
                                refreshScore(String.valueOf(score));
                                isBomb=true;
                                control=true;
                            }
                            buttons[k][j].setIcon(randomIcon.r());
                            buttons[k+1][j].setIcon(buttons[k-3][j].getIcon());
                            buttons[k+2][j].setIcon(buttons[k-2][j].getIcon());
                            buttons[k+3][j].setIcon(buttons[k-1][j].getIcon());
                            score+=400;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;
                        }
                        buttons[k][j].setIcon(buttons[k-3][j].getIcon());
                        buttons[k+1][j].setIcon(buttons[k-2][j].getIcon());
                        buttons[k+2][j].setIcon(buttons[k-1][j].getIcon());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }
                }
                if(k == 5){

                    if(s0.equals(s1) & s1.equals(s2)){
                        if(s2.equals(s3)){
                            buttons[k][j].setIcon(buttons[k-4][j].getIcon());
                            buttons[k+1][j].setIcon(buttons[k-3][j].getIcon());
                            buttons[k+2][j].setIcon(buttons[k-2][j].getIcon());
                            buttons[k+3][j].setIcon(buttons[k-1][j].getIcon());
                            score+=400;                                     
                            refreshScore(String.valueOf(score));
                            isBomb=true;
                            control=true;
                        }
                        buttons[k][j].setIcon(buttons[k-3][j].getIcon());
                        buttons[k+1][j].setIcon(buttons[k-2][j].getIcon());
                        buttons[k+2][j].setIcon(buttons[k-1][j].getIcon());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }
                }
                if(k == 6){
                    if(s0.equals(s1) & s1.equals(s2)){
                        buttons[k][j].setIcon(buttons[k-3][j].getIcon());
                        buttons[k+1][j].setIcon(buttons[k-2][j].getIcon());
                        buttons[k+2][j].setIcon(buttons[k-1][j].getIcon());
                        score+=300;                                     
                        refreshScore(String.valueOf(score));
                        isBomb=true;
                        control=true;
                    }
                }
            }
        }     
    }



 }while(control==true);

    return isBomb;
}

    //This method send the score to levels
public static void refreshScore(String score){
    if(randomIcon.LevelName.equals("Level1"))
        Level1.scoreTable.setText(score);
    if(randomIcon.LevelName.equals("Level2"))
        Level2.scoreTable.setText(score);
    if(randomIcon.LevelName.equals("Level3"))
        Level3.scoreTable.setText(score);
    if(randomIcon.LevelName.equals("Level4"))
        Level4.scoreTable.setText(score);
    if(randomIcon.LevelName.equals("Level5"))
        Level5.scoreTable.setText(score);
    if(randomIcon.LevelName.equals("Level6"))
        Level6.scoreTable.setText(score);

}
}

1 个答案:

答案 0 :(得分:0)

ImageIcon[3]数组并使用javax.swing.Timer

         ImageIcon m[];
         Timer timer;
         int i=0;
         m = new ImageIcon[3];
         m[0] = new ImageIcon("m.jpg");
         m[1] = new ImageIcon("m1.jpg");
         m[2] = new ImageIcon("m2.jpg");
         JButton b=new JButton();
         timer=new Timer(300,new ActionListener(){        //Here The Icon is Changed every 3 min .
     public void actionPerformed(ActionEvent e)
    {
        if(i<3)
        {
             b.setIcon(m[i]);
             i=i+1;
        }
        else
        {
         i=0;
         b.setIcon(m[i]);
        }
    }
  });
     timer.start();