I'm trying to make a program which will which will allow me 2 switch between a number of pictures "opera.jpg", "nbeach.jpg", "bridge.jpg", when the Load button is pressed, however i don't know how to make it do that, not properly. Any ideas? I want to be able to tell which picture is currently shown and change it to the next in alphabetical order.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Photo{
publicstaticvoid main(String[] args){
MyFrame frame = new MyFrame();
frame.setSize(400,300);
frame.setVisible(true);
}
}
class MyFrame extends JFrame implements ActionListener{
ImgPanel panel;
public MyFrame(){
getContentPane().setLayout(new BorderLayout());
JButton button = new JButton("Load");
JPanel panel2 = new JPanel();
panel2.add(button);
getContentPane().add(panel2, BorderLayout.SOUTH);
panel = new ImgPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setVisible(true);
button.addActionListener(this);
}
publicvoid actionPerformed(ActionEvent evt){
panel.setVisible(true);
}
}
class ImgPanel extends JPanel{
private Image image;
public ImgPanel(){
image = Toolkit.getDefaultToolkit().getImage("opera.jpg");
}
publicvoid paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 30, 30, this);
}
}