效果如何添加到JButton的图标?

时间:2014-03-03 19:36:12

标签: java swing jbutton effects

我正在努力制作像宝石迷阵或candycrush这样的游戏作业。它快要结束了。但我有一点问题。现在,当我点击时,会弹出具有相同图标的按钮,上面的图片将会出现,而不是爆炸。但它太快了。我怎么能减慢这个事件?或者我可以添加效果吗?

public class ButtonActionListener implements ActionListener {
public JButton previousButton = null;
public int numP, numC;
public JButton[] buttons=butondeneme.getButton();

@Override
public void actionPerformed(ActionEvent e){


    // TODO Auto-generated method stub
    JButton currentButton = (JButton)e.getSource();

    if (previousButton == null) {
        previousButton = currentButton;
        return;
    }

    int numP=Integer.parseInt(((JButton)previousButton).getActionCommand());
    int numC=Integer.parseInt(((JButton)currentButton).getActionCommand());

    //change picture of icons that clicked
    if (numP==(numC+1) || numP==(numC-1) || numP==(numC+8) || numP==(numC-8) ){
        Icon previousIcon = previousButton.getIcon();
        Icon currentIcon = currentButton.getIcon();
        currentButton.setIcon(previousIcon);
        previousButton.setIcon(currentIcon);
        previousButton = null;

    }
    else
        previousButton=null;

    Random r = new Random();
    int a = r.nextInt(64);
    int b = r.nextInt(64);
    int c = r.nextInt(64);

    //buttons that have same picture are explode.
    for(int i=0; i<63; i++){
        if(buttons[i].getIcon().toString()==buttons[i+1].getIcon().toString() && 
                buttons[i+1].getIcon().toString()== buttons[i+2].getIcon().toString() ){                                            
            //System.out.println("slm");
            if(i > 7){
                buttons[i].setIcon(buttons[i-8].getIcon());
                buttons[i+1].setIcon(buttons[i-7].getIcon());
                buttons[i+2].setIcon(buttons[i-6].getIcon());

                for(int j = i; j > 0; j=j-8){
                    if(j > 7){
                        buttons[j].setIcon(buttons[j-8].getIcon());
                        buttons[j+1].setIcon(buttons[j-7].getIcon());
                        buttons[j+2].setIcon(buttons[j-6].getIcon());
                    }
                    else{
                        buttons[j].setIcon(buttons[a].getIcon());
                        buttons[j+1].setIcon(buttons[b].getIcon());
                        buttons[j+2].setIcon(buttons[c].getIcon());
                    }

                }
            }
            else{
                buttons[i].setIcon(buttons[a].getIcon());
                buttons[i+1].setIcon(buttons[b].getIcon());
                buttons[i+2].setIcon(buttons[c].getIcon());
            }
        }
    }

}

}    

在本课程中,我创建了框架,按钮和随机图标。

public class butondeneme extends JFrame{

private JPanel grid;
public String comand;
public static JButton[] buttons;
public String imgName;

public butondeneme(){

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 640);
grid=new JPanel();
grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
grid.setLayout(new GridLayout(8,8,5,5));    
buttons = new JButton[64];


   //Creating random image for buttons
   ActionListener buttonActionListener = new ButtonActionListener();
     for (int i = 0; i<buttons.length; i++) {

            Random r = new Random();
            int a = r.nextInt(9)+1;
            switch(a){
            case 1 : buttons[i]=new JButton(new ImageIcon("img//Cakal.png"));
                    break;
            case 2 : buttons[i]=new JButton(new ImageIcon("img//BugsBunny.png"));
                    break;
            case 3 : buttons[i]=new JButton(new ImageIcon("img//Pig.png"));
                    break;
            case 4 : buttons[i]=new JButton(new ImageIcon("img//Taz.png"));
                    break;
            case 5 : buttons[i]=new JButton(new ImageIcon("img//Sam.png"));
                    break;
            case 6 : buttons[i]=new JButton(new ImageIcon("img//DuffyDuck.png"));
                    break;
            case 7 : buttons[i]=new JButton(new ImageIcon("img//Tweety.png"));
                    break;
            case 8 : buttons[i]=new JButton(new ImageIcon("img//Slyvester.png"));
                    break;
            case 9 : buttons[i]=new JButton(new ImageIcon("img//RoadRunner.png"));
                    break;
            }

            //Adding number to find easily
            comand=Integer.toString(i);

            //Get ImageIcon name
            imgName=((ImageIcon)buttons[i].getIcon()).toString();

            buttons[i].addActionListener(buttonActionListener);
            buttons[i].setActionCommand(comand);

            grid.add(buttons[i]);

      }


add(grid);


}

static JButton[] getButton(){
return buttons;
}

public static void main(String[] args){
    butondeneme erdem=new butondeneme();
    erdem.setVisible(true);
}

}

1 个答案:

答案 0 :(得分:0)

如果要为此类“动画”添加延迟,则必须使用Thread.sleep(ms)

但是此代码包含在actionPerformed方法中,因此您无法直接使用Thread.sleep(ms),因为这会阻止Event-Dispatch-Thread(EDT) GUI会变得反应迟钝。

所以你必须用自己的线程执行这个“动画”。

但是您不能在不同的主题上调用Button#setIcon(...)而不是EDT。

所以实际的解决方案在这里有点棘手 - 你必须“乒乓”两个线程之间的责任(或者,你可以使用javax.swing.Timer,但我认为这可能需要更多的重组你的原始代码......虽然我并不完全明白你在那里做了什么(也就是说,你为什么会改变那些图标)。)

然而,这种方法的一般结构大致如下:

@Override
public void actionPerformed(ActionEvent e){

    ...
    //buttons that have same picture are explode.
    startAnimation();
}


private void startAnimation()
{
    Thread t = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            runAnimation();
        }
    });
    t.start();
}

private int a;
private int b;
private int c;
private void runAnimation()
{
    Random r = new Random();
    a = r.nextInt(64);
    b = r.nextInt(64);
    c = r.nextInt(64);

    for(int i=0; i<63; i++)
    {
        final int iFinal = i;
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                doAnimationStep(iFinal);
            }
        });

        try
        {
            Thread.sleep(100);
        }
        catch (InterruptedException e)
        {
            Thread.currentThread().interrupt();
            return;
        }
    }
}

private void doAnimationStep(int i)
{
    String s0 = buttons[i].getIcon().toString();
    String s1 = buttons[i+1].getIcon().toString();
    String s2 = buttons[i+2].getIcon().toString();
    if(s0.equals(s1) && s1.equals(s2))
    {
        //System.out.println("slm");
        if(i > 7){
            buttons[i].setIcon(buttons[i-8].getIcon());
            buttons[i+1].setIcon(buttons[i-7].getIcon());
            buttons[i+2].setIcon(buttons[i-6].getIcon());

            for(int j = i; j > 0; j=j-8){
                if(j > 7){
                    buttons[j].setIcon(buttons[j-8].getIcon());
                    buttons[j+1].setIcon(buttons[j-7].getIcon());
                    buttons[j+2].setIcon(buttons[j-6].getIcon());
                }
                else{
                    buttons[j].setIcon(buttons[a].getIcon());
                    buttons[j+1].setIcon(buttons[b].getIcon());
                    buttons[j+2].setIcon(buttons[c].getIcon());
                }

            }
        }
        else{
            buttons[i].setIcon(buttons[a].getIcon());
            buttons[i+1].setIcon(buttons[b].getIcon());
            buttons[i+2].setIcon(buttons[c].getIcon());
        }
    }
}

顺便说一句:你不应该将String个对象与if (string0==string1) ...进行比较,而应始终与if (string0.equals(string1)) ...进行比较