How can I display a series of images one by one every time the user clicks the next button? I have few images stored in array. I used button next to go from image to image. How to programme the button? I used action listener but I just don't know how to get back to the array.
I know I'm being lazy here by not writing the entire code, but here goes anyway.
class whatEver implements ActionListener{
JButton btnNext = new JButton();
static int fileNumber = 0;
static int totalFiles;
void functionLoadInterface () //Function with
{
btnNext.addActionListener(this);
//Initialize array ImageArray with file paths.
}
public void cycleImage()
{
String file = ImageArray[fileNumber%totalFiles];
//Code to load the image wherever follows
}
public void actionPerformed(ActionEvent e)
{
filenumber++;
this.cycleImage();
}
Thanks for the help. But I have another problem. I dont know how to displayed it on JPanel. Here is the segment of the code.
public class mainFrame extends JFrame implements ActionListener{
private JButton answer1 = new JButton; private JButton answer2 = new JButton; private JButton answer3 = new JButton; private JButton answer4 = new JButton; private JButton answer5 = new JButton; private JButton answer6 = new JButton;
JPanel panel;
int fileNumber = 0; int totalFiles;
String[] a = {"image1.txt", "image2.txt",image3.txt",image4.txt" }; List files = Arrays.asList(a);
//CONSTRUCT THE FRAME public mainFrame(){
Collections.shuffle(files); ...... GridBagLayout m = new GridBagLayout(); Container c = (Container)getContentPane(); c.setLayout (m); GridBagConstraints con;
panel = new JPanel(); panel.setPreferredSize(new Dimension(600,600)); panel.setBackground(Color.white); con = new GridBagConstraints(); con.gridy = 2; con.gridx = 0; con.gridwidth = 50; con.gridheight = 10; con.fill = GridBagConstraints.BOTH; m.setConstraints(panel, con); c.add(panel);
//Button con = new GridBagConstraints(); con.anchor=GridBagConstraints.EAST; con.gridy = 3; con.gridx = 50; con.insets= new Insets(0,10,3,0); m.setConstraints(answer1, con); c.add(answer1); answer1.addActionListener(this); .............
functionLoadInterface();
setSize(new Dimension(780,700)); setVisible(true); }
public void functionLoadInterface () { answer1.addActionListener(this); answer2.addActionListener(this); answer3.addActionListener(this); answer4.addActionListener(this); answer5.addActionListener(this); answer6.addActionListener(this);
panel.add () -------------> I dont know what method to put on the bracket so that the drawing will be displayed on the JPanel }
public void actionPerformed(ActionEvent e) { fileNumber++; this.readInputFile(files, totalFiles); }
// Code working fine public void readInputFile(List files, int totalFiles) {} public void paintComponent(Graphics g) { }
}