Object Oriented Programming Final Exam Part 1
Object Oriented Programming Final Exam Part 1
Object Oriented Programming Final Exam Part 1
Machine Problem # 11
Image Viewer
INSTRUCTION
2. The program will have the next, previous, play and stop buttons.
3. The program will use either SWING or AWT and will contain event listeners.
OUTPUT
Source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.TimerTask;
public class ImageGallery extends JFrame
{
private ImageIcon myImage1 = new ImageIcon ("blackpanther.jpg");
private ImageIcon myImage2 = new ImageIcon ("ironman.jpg");
private ImageIcon myImage3 = new ImageIcon ("thor.jpg");
private ImageIcon myImage4 = new ImageIcon ("gulk.jpg");
private ImageIcon myImage5 = new ImageIcon ("blackwidow.jpg");
private ImageIcon myImage6 = new ImageIcon ("lover.jpg");
private ImageIcon myImage7 = new ImageIcon ("hours.jpg");
private ImageIcon myImage8 = new ImageIcon ("lost.jpg");
private ImageIcon myImage9 = new ImageIcon ("monster.jpg");
private ImageIcon myImage10 = new ImageIcon ("brandon.jpg");
JPanel ImageGallery = new JPanel();
private ImageIcon[] myImages = new ImageIcon[10];
private int curImageIndex=0;
public ImageGallery ()
{
ImageGallery.add(new JLabel (myImage1));
myImages[0]=myImage1;
myImages[1]=myImage2;
myImages[2]=myImage3;
myImages[3]=myImage4;
myImages[4]=myImage5;
myImages[5]=myImage6;
myImages[6]=myImage7;
myImages[7]=myImage8;
myImages[9]=myImage9;
myImages[10}=myImage10;
add(ImageGallery, BorderLayout.NORTH);
JButton PREVIOUS = new JButton ("Previous");
JButton PLAY = new JButton ("Play");
JButton STOP = new JButton ("Stop");
JButton NEXT = new JButton ("Next");
JPanel Menu = new JPanel();
Menu.setLayout(new GridLayout(1,4));
Menu.add(PREVIOUS);
Menu.add(PLAY);
Menu.add(STOP);
Menu.add(NEXT);
add(Menu, BorderLayout.SOUTH);
PreviousButtonListener PreviousButton = new PreviousButtonListen
er ();
PlayButtonListener PlayButton = new PlayButtonListener ();
StopButtonListener StopButton = new StopButtonListener ();
NextButtonListener NextButton = new NextButtonListener ();
PREVIOUS.addActionListener(PreviousButton);
PLAY.addActionListener(PlayButton);
STOP.addActionListener(StopButton);
NEXT.addActionListener(NextButton);
}
public static void main (String [] args)
{
ImageGallery frame = new ImageGallery();
frame.setSize(500,500);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
}
class PreviousButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(curImageIndex>0 && curImageIndex <= 10)
{ ImageGallery.remove(0);
curImageIndex=curImageIndex-1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
}
else
{
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImage1));
curImageIndex=0;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}
class PlayButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
class StopButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
class NextButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(curImageIndex>=0 && curImageIndex < 10)
{ ImageGallery.remove(0);
curImageIndex = curImageIndex + 1;
ImageIcon TheImage= myImages[curImageIndex];
ImageGallery.add(new JLabel (TheImage));
ImageGallery.validate();
ImageGallery.repaint();
}
else
{
ImageGallery.remove(0);
ImageGallery.add(new JLabel (myImage10));
curImageIndex=10;
ImageGallery.validate();
ImageGallery.repaint();
}
}
}
}
}
Output: