import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Car extends Frame
{
int x=80,y=80;
Image image; public void window() //生成窗口
{ try {
image = new ImageIcon(new URL("file:C:\\Documents and Settings\\Administrator\\桌面\\lesson5\\car.jpg")).getImage();
} catch (MalformedURLException ex) {
//Logger.getLogger(mCar.class.getName()).log(Level.SEVERE, null, ex);
}
setSize(600,500);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}}
);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
//******************************此行以下输入您的代码
if(e.getKeyCode()==KeyEvent.VK_UP)
y=y-10;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y=y+10;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x=x+10;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-10;
if(e.getKeyCode()==KeyEvent.VK_S)
{y=y-10;
x=x+10;}
//**********************************
repaint();
}
} ); }
public void paint(Graphics g)//在窗口中画图
{
g.drawImage(image, x, y, this);
}
public static void main(String[] args)//主方法,java的应用程序必须有且只有一个主方法
{
Car a=new Car();
a.window();
}
}I
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Car extends Frame
{
int x=80,y=80;
Image image; public void window() //生成窗口
{ try {
image = new ImageIcon(new URL("file:C:\\Documents and Settings\\Administrator\\桌面\\lesson5\\car.jpg")).getImage();
} catch (MalformedURLException ex) {
//Logger.getLogger(mCar.class.getName()).log(Level.SEVERE, null, ex);
}
setSize(600,500);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}}
);
addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
//******************************此行以下输入您的代码
if(e.getKeyCode()==KeyEvent.VK_UP)
y=y-10;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y=y+10;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x=x+10;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x=x-10;
if(e.getKeyCode()==KeyEvent.VK_S)
{y=y-10;
x=x+10;}
//**********************************
repaint();
}
} ); }
public void paint(Graphics g)//在窗口中画图
{
g.drawImage(image, x, y, this);
}
public static void main(String[] args)//主方法,java的应用程序必须有且只有一个主方法
{
Car a=new Car();
a.window();
}
}I