trivial but I can't do it! Need help
Hi,
I am really new to applet programming and I have created a simple slideshow (manually driven) but it doesn't work. It simply cannot draw images. This is my code. Please help!
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
publicclass SlideShowextends Appletimplements ActionListener{
Panel ImPanel =new Panel();
Canvas can =new Canvas();
Button Left =new Button(" <- ");
Button Right =new Button(" -> ");
Image[] images =new Image[3];
String[] text =new String[3];
int img_index = 0;
publicvoid init()
{
setLayout(new BorderLayout());
add(Left, BorderLayout.WEST);
add(Right, BorderLayout.EAST);
add(can, BorderLayout.CENTER);
images[0] = getImage(getDocumentBase(),"ss1.jpg");
images[1] = getImage(getDocumentBase(),"ss2.jpg");
images[2] = getImage(getDocumentBase(),"ss3.jpg");
text[0] ="image 1";
text[1] ="image 2";
text[2] ="image 3";
can.setBackground(Color.BLACK);
Left.addActionListener(this);
Right.addActionListener(this);
}
publicvoid paint(Graphics g)
{
g.drawImage(images[img_index], 0, 0, can);
}
publicvoid actionPerformed(ActionEvent event)
{
Object ob = event.getSource();
if (ob == Left)
{
if (img_index > 0)
img_index--;
}
elseif (ob == Right)
{
if (img_index < images.length)
img_index++;
}
repaint();
}
}
[3075 byte] By [
xpantaa] at [2007-11-27 7:10:06]

# 1
hi
check this out
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SlideShow extends Applet implements ActionListener {
Panel ImPanel = new Panel();
Canvas can = null;
Button Left = new Button(" <- ");
Button Right = new Button(" -> ");
Image[] images = new Image[3];
String[] text = new String[3];
int img_index = 0;
public void init()
{
can = new Canvas(){
public void paint(Graphics g) {
g.drawImage(images[img_index], 0, 0, this);
}
};
setLayout(new BorderLayout());
add(Left, BorderLayout.WEST);
add(Right, BorderLayout.EAST);
add(can, BorderLayout.CENTER);
images[0] = getImage(getDocumentBase(), "ss1.jpg");
images[1] = getImage(getDocumentBase(), "ss2.jpg");
images[2] = getImage(getDocumentBase(), "ss3.jpg");
text[0] = "image 1";
text[1] = "image 2";
text[2] = "image 3";
can.setBackground(Color.BLACK);
Left.addActionListener(this);
Right.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object ob = event.getSource();
if (ob == Left)
{
img_index--;
if (img_index < 0)
img_index = 0;
}
else if (ob == Right)
{
img_index++;
if (img_index == images.length)
img_index--;
}
can.repaint();
}
}
regards
Aniruddha