会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132360个问题
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3391楼
JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3392楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3393楼
JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识 3394楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3395楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3396楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 3397楼

错误:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
 at fly.FlyGameFrame.paint(FlyGameFrame.java:25)
 at fly.FlyGameFrame.update(FlyGameFrame.java:75)
 at java.desktop/sun.awt.RepaintArea.updateComponent(RepaintArea.java:255)
 at java.desktop/sun.awt.RepaintArea.paint(RepaintArea.java:232)
 at java.desktop/sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:358)
 at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5072)
 at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
 at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
 at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
 at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
 at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
 at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
 at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
 at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
 at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
 at java.base/java.security.AccessController.doPrivileged(Native Method)
 at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
 at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
 at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
 at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
 at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
 at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
 at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.ja



代码:

package fly;

 
import javax.xml.crypto.Data;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;
 
public class FlyGameFrame extends Frame {
 
    Image plane = GameUil.getImage("image/plane.png");
    Image bg = GameUil.getImage("image/bg.jpg");
 
Shell []shells=new Shell[50];
explode explod;
Date start=new Date();
Date end;
long period;
 
 
plant p1=new plant(plane,100,100,10);
 
    @Override
    public void paint(Graphics g) {
        g.drawImage(bg, 00, constant.Game_Width,constant.Game_Height, null);
        g.setColor(Color.green);
        if(p1.live){
            period=(System.currentTimeMillis()-start.getTime())/1000;
            g.drawString("坚持"+period,30,50);
        }else {
            if(end==null){
                end=new Date();
                period=(end.getTime()-start.getTime())/1000;
            }
            g.setColor(Color.red);
            g.setFont(new Font("微软雅黑",Font.BOLD,30));
            g.drawString("最终时间"+period,200,200);
        }
           p1.Drawmyself(g);
        for (int i = 0; i < shells.length; i++) {
            shells[i].Drawmyself(g);
            boolean peng=shells[i].getRect().intersects(p1.getRect());
            if(peng){
 
                p1.live=false;
                if(explod==null){
 
                explod=new explode(p1.x,p1.y);}
                explod.drawmyself(g);
            }
        }
    }
 
    public void launchFrame() {
        this.setTitle("飞机大战");
        setVisible(true);
        setSize(constant.Game_Width, constant.Game_Height);
        setLocation(100100);
        //增加关闭窗口
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();//线程
        this.addKeyListener(new KeyMonitor());//启动键盘
        for (int i = 0; i < 50; i++) {
            shells[i]=new Shell();
 
        }
    }
    //方便直接使用窗口的相关方法
    class PaintThread extends Thread{
        @Override
        public void run() {
            while (true){
                repaint();
                try{
                    Thread.sleep(50);
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
p1.addDirection(e);
        }
 
        @Override
        public void keyReleased(KeyEvent e) {
            p1.munisDirection(e);
        }
    }
private Image offscreenimage =null;
    public void update(Graphics g){
        if (offscreenimage==null)
            offscreenimage=this.createImage(constant.Game_Width,constant.Game_Height);
        Graphics goff=offscreenimage.getGraphics();
        paint(goff);
        g.drawImage(offscreenimage,0,0,null);
 
    }
    public static void main(String[] args) {
        FlyGameFrame gameFrame = new FlyGameFrame();
        gameFrame.launchFrame();
    }
}
package fly;
 
import java.awt.*;
 
public class GameObject {
    Image img;
    double x,y;
    int spead;
    int width,height;
public GameObject(){
 
}
    public GameObject(Image img, double x, double y, int spead, int width, int height) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.spead = spead;
        this.width = width;
        this.height = height;
    }
 
    public GameObject(Image img, double x, double y, int spead) {
        this.img = img;
        this.x = x;
        this.y = y;
        this.spead = spead;
        this.width=img.getWidth(null);
        this.height=img.getHeight(null);
    }
 
    public void Drawmyself(Graphics g) {
        g.drawImage(img,(int)x, (int)y, width,height, null);
 
    }
    public  Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }
}
package fly;
 
import java.awt.*;
 
public class Shell extends GameObject{
    double degreee;//角度,炮弹沿着固定方向飞行
    public Shell(){
        x=200;
        y=200;
        degreee=Math.random()*Math.PI*2;
        width=10;
        height=10;
        spead=7;
    }
 
    @Override
    public void Drawmyself(Graphics g) {
        Color c=g.getColor();
        g.setColor(Color.blue);
        g.fillOval((int)x,(int)y,width,height);
        g.setColor(c);
        //根据自己的算法去指定移动路径
        x+=spead*Math.cos(degreee);
 
        y+=spead*Math.sin(degreee);
        if(y>constant.Game_Height-this.height||y<40){
            degreee=-degreee;
        }
        if(x<0||x>constant.Game_Width){
            degreee=Math.PI-degreee;
        }
    }
}
package fly;
 
import java.awt.*;
 
public class explode {
    double x,y;
    static Image []imgs=new Image[16];
    int count;
    static {
        for (int i = 0; i < 16; i++) {
            imgs[i]=GameUil.getImage("image/e"+"i+1"+".gif");
 
        }
    }
    public void drawmyself(Graphics g){
        if(count<16){
            g.drawImage(imgs[count],(int)x ,(int)y,null);
            count++;
        }
    }
    public explode(){
 
    }
    public explode(double x,double y){
        this.x=x;
        this.y=y;
    }
}
package fly;
 
import java.awt.*;
import java.awt.event.KeyEvent;
 
public class plant extends GameObject{
    boolean left,right,up,down;
    boolean live=true;
    @Override
    public void Drawmyself(Graphics g) {
       if(live){ super.Drawmyself(g);
        if(left){
            x-=spead;
        }
        if(right){
            x+=spead;
 
        }
        if (up){
            y-=spead;
        }
        if (down){
            y+=spead;
        }}
    }
    public void addDirection(KeyEvent e){
        if(e.getKeyCode()== KeyEvent.VK_LEFT){
            left=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            right=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_UP){
            up=true;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            down=true;
        }
    }
    public void munisDirection(KeyEvent e){
        if(e.getKeyCode()==KeyEvent.VK_LEFT){
            left=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_RIGHT){
            right=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_UP){
            up=false;
        }
        if(e.getKeyCode()==KeyEvent.VK_DOWN){
            down=false;
        }
    }
 
    public plant(Image img, double x, double y, int spead) {
        super(img, x, y, spead);
    }
}
package fly;
 
public class constant {
    public static final int Game_Width=500;
    public static final int Game_Height=500;
}
package fly;
 
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.net.URL;
 
 
public class GameUil {
    private GameUil(){
 
    }
  public static Image getImage(String path){
      BufferedImage img = null;
      URL u = GameUil.class.getClassLoader().getResource(path);
      try {
          img = ImageIO.read(u);
      catch (IOException e) {
          e.printStackTrace();
      }
      return img;
  }
 
    public static void main(String[] args) {
        Image img=GameUil.getImage("image/plane.png");
        System.out.println(img);
    }
}


JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练 3400楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 3402楼
JAVA 全系列/第一阶段:JAVA 快速入门/控制语句、方法、递归算法 3403楼
JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 3404楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 3405楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637