import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MoveIt extends Applet implements ActionListener
{
//declare variables and construct color
private Image cup;
public Panel keyPad;
public int top = 15;
public int left = 15;
private Button keysArray[];
//create components for applet
public void init()
{
cup = getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas = new Canvas();
setBackground(Color.blue);
keyPad = new Panel();
//Construct keyPad and 5 buttons
keysArray = new Button[5];
keysArray[0] = new Button("Up");
keysArray[1] = new Button("Down");
keysArray[2] = new Button("Right");
keysArray[3] = new Button("Left");
keysArray[4] = new Button("Center");
//set frame and keypad layout to border layout
setLayout(new BorderLayout());
keyPad.setLayout(new BorderLayout());
//add components to keyPadPanel
keyPad.add(keysArray[0], BorderLayout.NORTH);
keyPad.add(keysArray[1], BorderLayout.SOUTH);
keyPad.add(keysArray[2], BorderLayout.EAST);
keyPad.add(keysArray[3], BorderLayout.WEST);
keyPad.add(keysArray[4], BorderLayout.CENTER);
//Add keypadPanel and Canvas to frame
add(keyPad, BorderLayout.SOUTH);
add(myCanvas, BorderLayout.NORTH);
//add actionListener to each button
for(int i=0; i<5; i++)
{
keysArray[i].addActionListener(this);
}
}
//Drag image using paint method
public void paint(Graphics g)
{
g.drawImage(cup, left, top, this);
}
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
if(arg=="Up")
{
top-=15;
}
if(arg=="Down")
{
top+=15;
}
if(arg=="Left")
{
left-=15;
}
if(arg=="Rigth")
{
top+=15;
}
if(arg=="Center")
{
top=60;
left=125;
}
repaint();
}
}
The image cup.gif is in the C drive along with the class, java and Html file. Whenever I run the applet no image is produce. everything else works fine. Please any help to get this working will be greatly appreciated. The image is supposed to move in accordance to the button commands on the applet but there is no image being displayed on the applet.