package com.whygame;
import java.awt.*;
import java.awt.event.*;
import static com.whygame.GameUtil.*;
public class whygameframe extends Frame {
int x=100,y=100;//飞机的坐标
Image bg =GameUtil.getImage("images/bg.jpg");
Image palane=GameUtil.getImage("images/meinv .jpg" );
Plane plane=new Plane(palane,100,100,1);
class keymonitr extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
plane.addDriection(e);
}
@Override
public void keyReleased(KeyEvent e) {
plane.relDriection(e);
}
}
public void Launchframe(){
this.setTitle("Ying ge 's 飞机大战");
this.setVisible(true);//窗口默认不可见,设置为可见true
this.setSize(FrameWidth,FrameHeight);
this.setLocation(FrameWidth,FrameHeight);
//增加关闭窗口的动作
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start();
//启动键盘监听
this.addKeyListener( new keymonitr());
}
@Override
public void paint(Graphics g) {
g.drawImage(bg,0,0,FrameWidth,FrameHeight,null);
plane.drawMyself(g);
/* Color old=g.getColor();
g.setColor(Color.PINK);
g.drawLine(100,10,200,20);
g.drawRect(300,300,300,300);
g.drawOval(400,400,100,100);
g.drawString("你好",300,300);
g.setColor(old);
g.drawLine(200,50,400,100);
*/
}
public static void main(String[]args) {
long a1 = System.currentTimeMillis();
whygameframe frame = new whygameframe();
frame.Launchframe();
long a2 = System.currentTimeMillis();
System.out.println("共用时:" + (a2 - a1) + "ms");
}
class PaintThread extends Thread{
@Override
public void run() {
while(true){
repaint();
try {
sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//解决屏幕闪烁问题
private Image offScreenImage=null;
public void update(Graphics g){
if (offScreenImage==null){
offScreenImage=this.createImage(FrameWidth,FrameHeight);
}
Graphics gOff=offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
}
这是个什么错误,正常运行了几秒钟就不动了
