Hi Folks!
I got to game (know as concentration or memory)code for my assignment.
I have found many similar tasks, but mine is a bit different.
Our teacher asked us to implement comboBox that we could switch
between images and text version.
So far I have made image one, and works fine.
Problem occurs while I'm trying to switch between them.
I mean switching isn't working so bad - it displays(resets)
image version and while switching to text shows in the command line 'choice2'
(that's what I set, as text version isn't ready yet)
It all would be great(as actually program works fine to me), but since I have
implemented comboBox it shows lots of errors in command line window.
I have taken screen shot of those errors - image file is in the attachments.
Code goes here:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import static java.util.Collections.*;
public class Assignment extends JFrame implements ActionListener
{
private JButton[] gameButton = new JButton[16];
private ArrayList<ImageIcon> gameList = new ArrayList<ImageIcon>();
private JComboBox combo1;
private String choices[] = {"Images version", "Font version"};
private int counter = 0;
private int score = 0;
private int[] buttonID = new int[2];
private ImageIcon[] buttonValue = new ImageIcon[16];
//Icon bugIcon = new ImageIcon("bug1.gif");
ImageIcon image1 = new ImageIcon(".\\img\\image1.jpg");
ImageIcon image2 = new ImageIcon(".\\img\\image2.jpg");
ImageIcon image3 = new ImageIcon(".\\img\\image3.jpg");
ImageIcon image4 = new ImageIcon(".\\img\\image4.jpg");
ImageIcon image5 = new ImageIcon(".\\img\\image5.jpg");
ImageIcon image6 = new ImageIcon(".\\img\\image6.jpg");
ImageIcon image7 = new ImageIcon(".\\img\\image7.jpg");
ImageIcon image8 = new ImageIcon(".\\img\\image8.jpg");
ImageIcon card = new ImageIcon(".\\img\\card.jpg");
JLabel plainLabel = new JLabel();
public JButton buttonC;
JPanel panel = new JPanel();
Container c = getContentPane();
public Assignment()
{
init();
panel();
setArrayList();
setTitle("Assignment 1: GUI Programming ");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public void setArrayList()
{
gameList.add(image1);
gameList.add(image2);
gameList.add(image3);
gameList.add(image4);
gameList.add(image5);
gameList.add(image6);
gameList.add(image7);
gameList.add(image8);
gameList.add(image1);
gameList.add(image2);
gameList.add(image3);
gameList.add(image4);
gameList.add(image5);
gameList.add(image6);
gameList.add(image7);
gameList.add(image8);
shuffle(gameList);
}
public void init()
{
for (int j = 0; j < gameButton.length; j++)
{
gameButton[j] = new JButton();
gameButton[j].addActionListener(this);
gameButton[j].setIcon(card);
}
buttonC = new JButton("Reset game");
buttonC.addActionListener(this);
panel.add(buttonC);
c.add(panel, BorderLayout.SOUTH);
panel.add(plainLabel);
Font plainFont = new Font("Arial", Font.ITALIC, 14);
plainLabel.setText("Score " +score);
plainLabel.setFont(plainFont);
plainLabel.setToolTipText("Score " + score);
combo1 = new JComboBox(choices);
combo1.addActionListener(this);
panel.add(combo1);
// c.add(panel);
}
public void panel()
{
JPanel gamePanel = new JPanel();
gamePanel.setLayout(new GridLayout(4, 4));
for (int j = 0; j < gameButton.length; j++)
{
gamePanel.add(gameButton[j]);
}
add(gamePanel, BorderLayout.CENTER);
}
public boolean sameValues()
{
if (buttonValue[0] == buttonValue[1])
{
return true;
}
return false;
}
public void actionPerformed(ActionEvent e)
{
for (int i = 0; i < gameButton.length; i++)
{
if (gameButton[i] == e.getSource())
{
gameButton[i].setIcon(gameList.get(i));
gameButton[i].setEnabled(true);
counter++;
if (counter == 3)
{
if (sameValues())
{
gameButton[buttonID[0]].setEnabled(false);
gameButton[buttonID[1]].setEnabled(false);
score = score + 1;
plainLabel.setText("Score "+score);
}
else
{
gameButton[buttonID[0]].setEnabled(true);
gameButton[buttonID[0]].setIcon(card);
gameButton[buttonID[1]].setEnabled(true);
gameButton[buttonID[1]].setIcon(card);
}
counter = 1;
}
if (counter == 1)
{
buttonID[0] = i;
buttonValue[0] = gameList.get(i);
}
if (counter == 2)
{
buttonID[1] = i;
buttonValue[1] = gameList.get(i);
}
}
}
if(e.getSource()== buttonC) //reset button ;)
{
new Assignment();
}
if (combo1 == (JComboBox)e.getSource());
int Selection;
Selection = combo1.getSelectedIndex();
if (Selection == 0)
{
new Assignment();
}
else if (Selection == 1)
{
System.out.println("choice2");
}
/*
if (combo1 == (JComboBox)e.getSource());
//String choices = (String)combo1.getSelectedItem();
//if(combo1.getSelectedItem() == "Images version")
{
System.out.println("choice1");
new Assignment();
}
*/
}
public static void main(String[] args)
{
new Assignment();
}
}
Can anyone advise, please?
Best regards
Tom