package com.bjsxt.plane;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import static com.bjsxt.plane.Constant.GAME_HEIGHT;
/**
* 游戏主窗口
*/
public class MyGameFrame extends Frame {
Image planeImg = GameUtil.getImage("images/plane.png");
Image bg = GameUtil.getImage("images/bg.jpg");
static int count = 0;
GameObject plane2 = new GameObject(planeImg,100,100,3,22,33);
@Override
public void paint(Graphics g) { //g当作是一支画笔
System.out.println("绘制窗口次数:"+count);
count++;
g.drawImage(bg,0,0,500,500,null);
plane2.drawMyself(g);
}
//初始化窗口
public void launchFrame(){
this.setTitle("飞机大战·尚学堂");
setVisible(true); //窗口是否可见
setSize(Constant.GAME_WIDTH, GAME_HEIGHT); //窗口大小
setLocation(400,400); //窗口打开的位置\
this.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0); //正常关闭
}
});
new PaintThread().start(); //启动重画窗口的线程
}
//定义了一个重画窗口的线程
//定义内部类是为了方便直接使用窗口类的相关方法
class PaintThread extends Thread{
@Override
public void run() {
while(true){
repaint(); //内部类可以直接使用外部类的成员
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Image offScreenImage = null;
public void update(Graphics g){
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH, GAME_HEIGHT);//这是游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage,0,0,null);
}
public static void main(String[] args){
MyGameFrame gameFrame = new MyGameFrame();
gameFrame.launchFrame();
}
}
package com.bjsxt.plane;
import java.awt.*;
/**
* 游戏物体的根类
*/
public class GameObject {
Image img; //图片
double x,y; //物体的坐标
int speed; //物体移动的速度
int width,height; //物体的宽度和高度
public GameObject(Image img, double x, double y, int speed, int weight, int height) {
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}
public void drawMyself(Graphics g){
g.drawImage(img,(int)x,(int)y,width,height,null);
}
}

完全按照老师视频敲得呀,但是运行后不显示小飞机,麻烦老师帮我看一下是哪里错了,实在是找不到