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();
}
}
图像cup.gif与类,java和Html文件一起位于C盘中。每当我运行applet时,都不会生成图像。其他一切正常。请非常感谢任何帮助以使这项工作。图像应该根据applet上的按钮命令移动,但applet上没有显示图像。