Structures
Structures

Reputation: 111

GUI That displays each image of an Array one at a time

I am extremely new to GUI's (and Java, started less than 3 months ago) and need some help with a GUI I am doing for a homework assignment. I have come here as a last resort as I cannot figure this out and have been working on it for hours.

I need to make a GUI that will go through and Array of ImageIcons and will display each ImageIcon one at a time (erasing the previous one displayed). I have gotten it to where it displays my first image, but then my JButton does absolutely nothing and I have no clue how to get the thing to work. I have looked through my textbook and numerous online sources and examples my teacher has given and still nothing. I know once I see a solution I'll feel stupid but right now, I'm tired and am starting to not think straight because I have been doing this for so long. PLEASE HELP =D!!!

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

public class HangmanPanel extends JPanel {
    private JLabel imageLabel;
    private ImageIcon[] images;
    private JButton nextImage;
    private int imageNumber;


    public HangmanPanel() {

        nextImage = new JButton("Next Image");
        nextImage.setEnabled(true);
        nextImage.setToolTipText("Press for next image.");
        nextImage.addActionListener(new ButtonListener());

        images = new ImageIcon[8];
        // Populating the array
        {
            images[0] = new ImageIcon("hangman0.png");
            images[1] = new ImageIcon("hangman1.png");
            images[2] = new ImageIcon("hangman2.png");
            images[3] = new ImageIcon("hangman3.png");
            images[4] = new ImageIcon("hangman4.png");
            images[5] = new ImageIcon("hangman5.png");
            images[6] = new ImageIcon("hangman6.png");
            images[7] = new ImageIcon("hangman7.png");
        }

        setBackground(Color.white);

        add(nextImage);
        int count = 0;
        while (images.length > count)
        imageLabel = new JLabel (images[imageNumber]);
        count++;
        imageNumber++;
        add (imageLabel);
    }


    public void paint(Graphics page) {
        super.paint(page);


    }
    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event) {
            imageNumber++;
            }

        }
    }

Upvotes: 0

Views: 1674

Answers (3)

John Snow
John Snow

Reputation: 5334

Assuming that you want to change the picture when you press the button, you want to put your logic inside the ActionListener. This will make the program fire the changes when the button is clicked.

In the constructor you only want to initiate the first icon, and you want to save a global count refrence so you know which image is next. The "image changing logic" will be inside your listener, so you want to do something like this:

//construcor
   imageLabel = new JLabel (images[imageNumber]);
   imageNumber++;
   add (imageLabel);

//listener
private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event) {
           if(imageNumber<images.length){  //add a check so you don't get outofbounds exception
              imageLabel.setIcon(images[imageNumber]); //this will set the image next in line
              imageNumber++;
              imageLabel.repaint();
            }
          else
            System.out.println("Whole array has been looped thru, no more images to show");
          }

        }

Upvotes: 2

ControlAltDel
ControlAltDel

Reputation: 35011

You just need a few more lines in your ButtonListener


    private class ButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent event) {
            imageNumber++;
            imageNumber %= images.length;
            imageLabel.setIcon(images[imageNumber]);
            imageLabel.repaint();


        }
    }

Upvotes: 1

mKorbel
mKorbel

Reputation: 109815

  • these methods are decribed in the tutorial avout Icon/ImageIcon and JLabel, use method JLabel#setIcon()

  • don't forget to check if imageNumber++; isn't out of Array of ImageIcon[] images;, otherwise Array Index Out of Bounds Exception will freeze your GUI,

Upvotes: 2

Related Questions