package com.sxt.planegame;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
public class PlaneGame extends Frame{
Image bg = GameUtil.getImage("images/bg.jpg");
Image planeImg = GameUtil.getImage("images/plane.png");
Plane plane = new Plane(planeImg, 250, 150);
Shell[] shells = new Shell[30];
//创建窗口方法
public void launchFrame() {
this.setTitle("尚学堂学员_刘江");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
this.setLocation(300, 300);
//窗口关闭,程序关闭
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//启动重画多线程
new paintThread().start();
//添加键盘监听
addKeyListener(new keyControl());
//在窗口加载时初始化炮弹个数
for (int i = 0; i < shells.length; i++) {
shells[i] = new Shell();
}
}
//paint是窗口的一个主要方法,用于绘制窗口
@Override
public void paint(Graphics g) {
g.drawImage(bg, 0, 0, null);
plane.drawSelf(g);
//画出20个炮弹
for(int i = 0; i < shells.length; i++) {
shells[i].drawSelf(g); //画出每个炮弹
boolean peng = shells[i].getRectangle().intersects(plane.getRectangle());
if(peng) {
System.out.println("相撞了");
plane.live = false;
}
}
}
//使用内部类+多线程实现动画效果
class paintThread extends Thread{
@Override
public void run() {
while(true) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//用键盘控制飞机
class keyControl extends KeyAdapter {
//按下键盘
@Override
public void keyPressed(KeyEvent e) {
plane.addKeyMonitor(e);
}
//松掉键盘
@Override
public void keyReleased(KeyEvent e) {
plane.minusKeyMonitor(e);
}
}
//主方法
public static void main(String[] args) {
PlaneGame planeGame = new PlaneGame();
planeGame.launchFrame();
}
//双缓冲解决闪烁问题,不使用JFrame原始处理
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);
}
}
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sxt.planegame.PlaneGame.paint(PlaneGame.java:57)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
老师,可以帮我看看这个问题吗?在paint()中重画20个炮弹的时候不会提示错误,运行时总是提示下面错误,导致无法实现碰撞问题。