[CODE]/*
* Test7.java
*
* Created on 2007年2月1日, 下午5:12
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author lbf
*/
public class Test7 extends JPanel implements Runnable{
private int y;//用来保存Y的座标,由线程更新, paintComponent调用
private Image left,up,right,down;
/** Creates a new instance of Test7 */
public Test7() {
initImage();
initWindow();
new Thread(this).start();
}
private void initImage(){
left=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/left.GIF"));
up=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/up.GIF"));
right=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/right.GIF"));
down=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("pic/down.GIF"));
try{
MediaTracker mt=new MediaTracker(this);
mt.addImage(left,0);
mt.addImage(up,1);
mt.addImage(right,2);
mt.addImage(down,3);
mt.waitForAll();
}
catch(Exception exe){
exe.printStackTrace();
}
}
private void initWindow(){
JFrame jf=new JFrame("test");
jf.add(this,BorderLayout.CENTER);
jf.setSize(300,600);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(left,10,y,this);
g.drawImage(up,50,y,this);
g.drawImage(down,100,y,this);
g.drawImage(right,150,y,this);
}
public void run(){
while(true){
try{
Thread.sleep(100);
if(y<=0){
y=600;
}
y-=10;
repaint();
}
catch(Exception exe){
exe.printStackTrace();
}
}
}
public static void main(String[] args) {
new Test7();
}
}[/CODE]
这是四个键 一起向上移动,你想哪个动就哪个动
可惜不是你,陪我到最后