I was wondering if someone could help me. I am trying to construct a image applet. I am having trouble with initiating a picture. I have 3 buttons, one to start, one to show the next, and one to show previous.
Here is my code so far:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class homework3 extends Applet
implements ActionListener, ItemListener {
private Button show, next, prev;
private Pictures myPics;
private Image pics[] = new Image[3];
private String [] gifName = {"image1", "image2", "image3"};
Panel p1;
public void init() {
for (int i = 0; i < gifName.length; i++)
pics = null;
Panel p1 = new Panel();
show = new Button("Show"
p1.add(show);
show.addActionListener(this);
next = new Button("Next"
p1.add(next);
next.addActionListener(this);
prev = new Button("Previous"
p1.add(prev);
prev.addActionListener(this);
add(p1);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == show)
myPics.show();
if (event.getSource() == next)
myPics.next();
if (event.getSource() == prev)
myPics.prev();
repaint();
public void paint(Graphics g) {
// if (pics[currentImage] == null)
// show();
g.drawImage(pics[currentImage], 40, 40, this);
}
class Pictures {
public void show() {
pics[currentImage] = getImage(getDocumentBase(), gifName[currentImage]);
}
public void next() {
if (currentImage > gifName.length - 1)
currentImage++;
else
currentImage = 0;
repaint();
}
public void prev() {
if (currentImage > 0);
else
currentImage = gifName.length - 1;
}
}
Here is my code so far:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class homework3 extends Applet
implements ActionListener, ItemListener {
private Button show, next, prev;
private Pictures myPics;
private Image pics[] = new Image[3];
private String [] gifName = {"image1", "image2", "image3"};
Panel p1;
public void init() {
for (int i = 0; i < gifName.length; i++)
pics = null;
Panel p1 = new Panel();
show = new Button("Show"
p1.add(show);
show.addActionListener(this);
next = new Button("Next"
p1.add(next);
next.addActionListener(this);
prev = new Button("Previous"
p1.add(prev);
prev.addActionListener(this);
add(p1);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == show)
myPics.show();
if (event.getSource() == next)
myPics.next();
if (event.getSource() == prev)
myPics.prev();
repaint();
public void paint(Graphics g) {
// if (pics[currentImage] == null)
// show();
g.drawImage(pics[currentImage], 40, 40, this);
}
class Pictures {
public void show() {
pics[currentImage] = getImage(getDocumentBase(), gifName[currentImage]);
}
public void next() {
if (currentImage > gifName.length - 1)
currentImage++;
else
currentImage = 0;
repaint();
}
public void prev() {
if (currentImage > 0);
else
currentImage = gifName.length - 1;
}
}