importjava.awt.*;importjava.awt.event.*;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjavax.swing.*;publicclasszhandouextendsJFrameimplementsKeyLis...
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
public class zhandou extends JFrame implements KeyListener
{
Image roleImage,Image1;
int x,y;
public zhandou()
{
super("MOVE");
Container c=getContentPane();
setSize(320,240);
setVisible(true);
loadImage();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setFocusable(true);
addKeyListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
//drawRole(g);
g.drawImage(roleImage,x,y,this);
g.drawImage(Image1,x,y,this);
}
public void loadImage()
{
ImageIcon icon=new ImageIcon("tupian/草地.jpg");
roleImage=icon.getImage();
ImageIcon ic=new ImageIcon("tupian/right.gif");
Image1=ic.getImage();
}
class Thread1 extends Thread
{
}
/*private void drawRole(Graphics g)
{
g.drawImage(roleImage,100,100,this);
}*/
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y=y-5;
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
y=y+5;
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x=x+5;
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-5;
else if(e.getKeyCode()==KeyEvent.VK_ENTER)
x=x+10;
repaint();
}
public void keyReleased(KeyEvent e)
{
}
public void keyTyped(KeyEvent e)
{
}
public static void main(String args[])
{
new zhandou();
}
}
展开