错误: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, 0, 0, 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(100, 100);
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);
}
}