Hi all,
I am currently designing an image viewer applet that displays and array images. It contains a next and previous button to scroll through the images, I have this part working fine. Next I want to implement captions to display for each image, these will be displayed below the image in the south region of the border layout.
What is the best way to go about this?
My code is posted below.
Thanks :)
import java.applet.*;
import java.awt.*;
import javax.swing.*;
public class MySlideShow extends Applet
{
private Button prev = new Button("Previous");
private Button next = new Button("Next");
private MediaTracker tracker = null;
private Image img[] = new Image[3];
private String imgNames[] = {"image1.jpg", "image2.jpg", "image3.jpg"};
private String captions[] = {"image1", "image2", "image3"};
private int currentImage = 0;
public void init()
{
Panel nextButton = new Panel();
nextButton.add(next);
Panel prevButton = new Panel();
prevButton.add(prev);
setLayout(new BorderLayout());
add("East", nextButton);
add("West", prevButton);
loadImages();
}
public boolean action(Event e, Object o)
{
if (e.target == prev)
handlePrev();
else if (e.target == next)
handleNext();
return true;
}
public void paint(Graphics g)
{
g.drawImage(img[currentImage], 200, 200, this);
}
public void loadImages()
{
tracker = new MediaTracker(this);
try
{
for (int i = 0; i < imgNames.length; i++)
{
img[i] = getImage(getDocumentBase(), imgNames);
tracker.addImage(img[i], i);
}
}
catch(Exception e)
{
System.err.println("Error loading image");
}
try
{
tracker.waitForAll();
}
catch(Exception e)
{
System.err.println("Unknown error while loading images");
}
}
public void handlePrev()
{
if (currentImage > 0)
currentImage--;
else
currentImage = imgNames.length-1;
repaint();
}
public void handleNext()
{
if (currentImage < imgNames.length-1)
currentImage++;
else
currentImage = 0;
repaint();
}
}