In: Computer Science
Need to read a PPM image using a 3D array and display it in a swing Jpanel.
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++;
}
}
}