hi there i am working on a project(java memory game) and first of all i am trying to understand that how swing timer works. firstly, i my main class implements ActionListener
and ItemListener
. and i use timer in actionPerformed(ActionEvent e)
if two cards which user selected different pictures then i use timer.start()
to give him a couple of seconds to see pictures and then they will be closed again. but if user selects two different pictures they suddenly closed, so i can't see the second picture. i read some tutorials about swing timer but i guess i understood wrongly.by the way i created my to SSCCE, and i will be appreciated if you can help me. thanks anwyway...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Menu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Random;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.border.*;
public class ConcentrationGame4 extends JFrame implements ActionListener, ItemListener{
private static final long serialVersionUID = 1L;
private int buttoncounter=0;
private int counter = 0;
private JFrame frame;
private JPanel mainPanel;
private JPanel buttonPanel;
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private int[] arr = new int[16];
private int i,j;
private int random;
private int size = 4;
private Icon hidden;
private GameButton buttonFirst;
private GameButton buttonSecond;
private Timer timer;
private Icon img[] = {UIManager.getIcon("OptionPane.errorIcon"),
UIManager.getIcon("OptionPane.informationIcon"),
UIManager.getIcon("OptionPane.warningIcon")};
private Icon iconList[] = new ImageIcon[size];
public ConcentrationGame4(){
createArray();
initComponents();
}
private void initComponents(){
frame = new JFrame("Concentration Game");
menuBar = new JMenuBar();
menu = new JMenu("Menu");
frame.setJMenuBar(menuBar);
menuBar.add(menu);
menuItem = new JMenuItem("New Game");
menu.add(menuItem);
menuItem = new JMenuItem("Solve");
menu.add(menuItem);
menuItem = new JMenuItem("Exit");
menu.add(menuItem);
mainPanel = new JPanel(new BorderLayout(5, 5));
mainPanel.setBorder(new EmptyBorder(4,4,4,4));
frame.setContentPane(mainPanel);
buttonPanel = new JPanel(new GridLayout(4,4,5,5));
buttonPanel.setBackground(Color.green);
timer = new Timer(2000,this);
for(i=0; i<4; i++){
final GameButton button = new GameButton(iconList[i]);
button.addItemListener(this);
button.addActionListener(this);
buttonPanel.add(button);
}
mainPanel.add(buttonPanel, BorderLayout.CENTER);
frame.setSize(300, 300);
//frame.pack();
frame.setLocation(300, 300);
frame.setVisible(true);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void itemStateChanged(ItemEvent e){
GameButton button = (GameButton) e.getItem();
button.setState();
}
public void actionPerformed(ActionEvent e){
GameButton button = (GameButton) e.getSource();
if(button.isSelected()){
buttoncounter++;
if(buttoncounter==1){
buttonFirst = (GameButton) e.getSource();
}
else if(buttoncounter==2){
buttonSecond = (GameButton) e.getSource();
buttoncounter=0;
if( checkPairs(buttonFirst,buttonSecond) ) {
retirePair(buttonFirst,buttonSecond);
}
else{
timer.start();
falsePair(buttonFirst, buttonSecond);
}
}
}
}
class GameButton extends JToggleButton{
private static final long serialVersionUID = 1L;
private Icon icon;
public GameButton(Icon icon){
this.icon = icon;
}
public void setState() {
if (this.isSelected() || !this.isEnabled()) {
this.setIcon(icon);
}
else {
this.setIcon(hidden);
}
}
}
private void retirePair(GameButton a, GameButton b){
a.setSelected(true);
a.setEnabled(false);
b.setSelected(true);
b.setEnabled(false);
}
private void falsePair(GameButton buttonFirst, GameButton buttonSecond){
buttonFirst.setIcon(hidden);
buttonFirst.setSelected(false);
buttonSecond.setIcon(hidden);
buttonSecond.setSelected(false);
}
private boolean checkPairs(GameButton first, GameButton second){
if(first.getIcon().equals(second.getIcon()))
return true;
else
return false;
}
private void createArray(){
Random rnd = new Random();
while(i<4){
random = rnd.nextInt(3)+1;
if(!includes(random)){
arr[i]=random;
iconList[i] = img[random-1];
i++;
}
}
}
public boolean includes(int rnd){
counter=0;
for(j=0; j<arr.length; j++){
if(arr[j] == rnd){
counter++;
if(counter>1)
return true;
}
}
return false;
}
/**
* @param args
*/
public static void main(String[] args) {
new ConcentrationGame4();
}
}
I don't know what you want to do, but you pass
this
as argument to the Timer constructor. This means that every 2 seconds, theactionPerformed
method ofthis
(instance of ConcentrationGame4) will be called. And the first thing that this method does isObviously, this will throw an exception, since the origin of the event won't be a game button, but the timer.
To understand what a Timer does and how it works, it's very simple. You just have to read its api doc : http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html. This doc will even lead you to a tutorial explaining how they work, with examples.
As an aside, you might want to look at using
List<Icon>
andList<GameButton>
. SuchCollections
are easy to shuffle in a reliable way. Also, note the difference between an individualGameButton
and a collection of them, external to theGameButton
class. It's the collection that you'll need to examine when any one button is clicked.An example of using a Swing Timer to pause action for xxx seconds: