I am working on a simple image guessing game. Instead of having a jLabel for each image, I'm using only one jLabel and it changes its icon using arrays when a button is clicked after the image has been guessed correctly.
But how can I code it so that the answer required in the jTextField (txtAnswer
) is determined by the icon set in the jLabel?
So for instance, if the current icon is of the Aston Martin DBS and the user guesses correctly, the icon will then change to the Ferrari 458. What must I do then to ensure that the txtAnswer
will now require the user to enter "Ferrari 458" to guess correctly based on the image icon that is set?
Here is the code I currently have:
private static String[] imageList = {"/carGuessPackage/2010 Aston Martin DBS.jpg", "/carGuessPackage/2010 Ferrari 458 Italia.jpg"};
//This method is called in the constructor to set the first image on startup
public void firstIcon()
{
ImageIcon image;
image = new javax.swing.ImageIcon(getClass().getResource(imageList[0]));
lblImage.setIcon(image);
}
private void btnCheckActionPerformed(java.awt.event.ActionEvent evt) {
ImageIcon image;
if(imageList[0].equals(true))
{
if("Aston Martin DBS".equals(txtAnswer.getText()))
{
image = new javax.swing.ImageIcon(getClass().getResource(imageList[1]));
lblImage.setIcon(image);
}
else
{
JOptionPane.showMessageDialog(this, "Incorrect");
}
}
}