Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help needed with images/event handler

Status
Not open for further replies.

knewbie

Programmer
Feb 5, 2002
1
US
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(&quot;Show&quot;);
p1.add(show);
show.addActionListener(this);

next = new Button(&quot;Next&quot;);
p1.add(next);
next.addActionListener(this);

prev = new Button(&quot;Previous&quot;);
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;

}

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top