0

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");
        }
        }
    } 
Osiris93
  • 296
  • 2
  • 18
  • 1
    You can set the Icon of a JLabel using [JLabel.setIcon](https://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html#setIcon(javax.swing.Icon)) – copeg Jul 06 '16 at 19:50
  • I know how to do that, but how would I distinguish between the images when the user guesses what the image is if it's still on the same label? – Osiris93 Jul 06 '16 at 19:51
  • 2
    I recommend rephrasing your question then, preferably with a lot more context (eg [MCVE](http://stackoverflow.com/help/mcve) ). – copeg Jul 06 '16 at 19:55
  • A little thinking suggests to use an int index to the array -- create an int index variable, increment it on mouse click using a MouseListener, mod it to the size of the array or List that holds the icons, and then get and set the next icon using this index, .... but surely you've already thought of this and tried it, no? – Hovercraft Full Of Eels Jul 06 '16 at 20:20
  • @HovercraftFullOfEels No I haven't tried it. I'm still new to arrays so I'm trying to learn by doing applications like this – Osiris93 Jul 06 '16 at 20:22
  • Osiris, you get information from arrays by using an index, so this is the natural solution with any use of an array. I have to be honest to be a little disappointed in not seeing a code attempt at trying to solve this with your question. In the future, please do this, as it will show us what you've tried, but more importantly, it will show us specifically what you may be doing wrong and what assumptions you may have that are incorrect. Please look at the [mcve] link for future reference. – Hovercraft Full Of Eels Jul 06 '16 at 20:54
  • @HovercraftFullOfEels Well while waiting for more responses I have managed to produce some code. So I have now edited my question and entered the code I currently have. – Osiris93 Jul 06 '16 at 21:08
  • OK, thanks for posting this -- now what problems are you having with it? What errors if any? What misbehaviors? One sub-divide the problem and test each portion in isolation. Test getting and displaying images in a simple small program. Test swapping images in a JLabel in another program. Test MouseListener,... Also, read in the images all at once, place into an ImageIcon array (or better, an `ArrayList`), once at program start, and then swap from this array or ArrayList. – Hovercraft Full Of Eels Jul 06 '16 at 21:20
  • @HovercraftFullOfEels I tried using an ImageIcon array. I declared it correctly but when I tried specifying the image address (`imageList[0] = "/carGuessPackage/2010 Ferrari 458 Italia.jpg";`), Java says I'm missing a sqaure bracket "]". I tried various solutions from Stackoverflow regarding it and the problem persists. I don't think a MouseListener will help in this scenario as a button is used to check the answer nd move on. Basically I just need the right code for an if statement that checks which image is set as an icon on the label and therefore what answer is needed in the `txtAnswer`. – Osiris93 Jul 06 '16 at 21:47
  • Why are you comparing a String with a boolean? `if(imageList[0].equals(true))`. Also you might want to remove the spaces on your images – Frakcool Jul 06 '16 at 22:32
  • @Frakcool I know what I put there was not correct but I just gave it a try. Hence why I'm now asking what would be the correct way to go about this, because I cannot find anything online specifically for what I want to do. What difference would removing the spaces in the images do as a matter of interest? – Osiris93 Jul 07 '16 at 10:55

1 Answers1

1

Check out the JLabel.setIcon() method. You would need and array of Icon objects, and they would be passed to the existing JLabel.

m_kinsey
  • 194
  • 10
  • Okay how would I do that? Sorry but Arrays are a big weak point for me (still trying to understand them). Could you maybe show a very basic example please? – Osiris93 Jul 06 '16 at 19:54
  • 3
    `Sorry but Arrays are a big weak point for me` Then learn them before trying to go into GUI applications which add more complexity to the code. `Could you maybe show a very basic example please?` For better help sooner please post a [mcve] – Frakcool Jul 06 '16 at 19:56
  • One way to get image(s) for an example (as mentioned by @Frakcool) is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jul 07 '16 at 01:30