1

I'm a beginner. I'm making a kid's math game. All it has to do is add the 2 pictures given. The problem is, I don't know how the JTextField will know that the answer is correct. Here's the picture.

enter image description here

ImageIcon[] images = new ImageIcon[10];
            images[0] = new ImageIcon("0.jpg");
            images[1] = new ImageIcon("1.jpg");
            images[2] = new ImageIcon("2.jpg");
            images[3] = new ImageIcon("3.jpg");
            images[4] = new ImageIcon("4.jpg");
            images[5] = new ImageIcon("5.jpg");
            images[6] = new ImageIcon("6.jpg");
            images[7] = new ImageIcon("7.jpg");
            images[8] = new ImageIcon("8.jpg");
            images[9] = new ImageIcon("9.jpg");

            ImageIcon[] image = new ImageIcon[10];
            image[0] = new ImageIcon("0.jpg");
            image[1] = new ImageIcon("1.jpg");
            image[2] = new ImageIcon("2.jpg");
            image[3] = new ImageIcon("3.jpg");
            image[4] = new ImageIcon("4.jpg");
            image[5] = new ImageIcon("5.jpg");
            image[6] = new ImageIcon("6.jpg");
            image[7] = new ImageIcon("7.jpg");
            image[8] = new ImageIcon("8.jpg");
            image[9] = new ImageIcon("9.jpg");

            int image_number = (int) (Math.random() * 10);
            int image_number1 = (int) (Math.random() * 10);

            Pic1.setIcon(images[image_number]);
            Pic2.setIcon(image[image_number1]);
0

2 Answers 2

2

I'm not sure if I understood your question correctly, but you can store the value of a pic in some variable in Image.

It's been some time since I've coded in java, but I remember there are fields to store data.

After which all the JTextField has to do, is get the data associated with the 2 images currently being shown and see if the answer is correct or not.

Another good option, can be to name the images equal to their value i.e. an image named "7.png" is actually a diagram showing 7, that way all you have to do, is figure out what 2 images are being shown, parse their name and check for accuracy with the user input.

Update

Just did some quick research - http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html

ImageIcon contains a field description, you can simply set that field when creating instances of it & retrieve the one currently rendered.

ImageIcon[] images = new ImageIcon[10];
            images[0] = new ImageIcon("0.jpg");
            images[0].setDescription("0");
            //Other code

then somewhere at some other place, click of a button or keypress of JTextField, it can be retrieved like :

Integer x=Integer.parseInt(Pic1.getIcon().getDescription());
Sign up to request clarification or add additional context in comments.

Comments

0

One thing you might consider doing is creating a wrapper class around the image, which not only maintains a reference to the image, but the value of the image

public class NumberImage {
    private Image image;
    private int value;

    public NumberImage(int value, Image image) {
        this.value = value;
        this.image = image;
    }

    public int getValue() {
        return value;
    }

    public Image getImage() {
        return image;
    }
}

This way, you can calculate the answer by simply adding the two values properties together within your program

The other thing you already have is the two indexes of the images to be displayed, wch is the same numerical value of the image to be displayed...

That is, based on your example, image_number == 7 and image_number1 == 5, therefore, you already have the information you need to calculate the answer ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.