g.drawImage上的NullPointerException

时间:2019-03-28 10:51:18

标签: java

我正在用Java为学校项目编写赛车游戏。我创建了带有属性图像的类“ Vehicle”。我还创建了JDessin类(法语),在其中绘制了车辆图像。但是,我得到这个错误:

  

线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException       在JDessin.paint(JDessin.java:37)       在java.desktop / javax.swing.JComponent.paintToOffscreen(JComponent.java:5255)       在java.desktop / javax.swing.RepaintManager $ PaintManager.paintDoubleBufferedFPScales(RepaintManager.java:1707)       在java.desktop / javax.swing.RepaintManager $ PaintManager.paintDoubleBuffered(RepaintManager.java:1616)       在java.desktop / javax.swing.RepaintManager $ PaintManager.paint(RepaintManager.java:1556)       在java.desktop / javax.swing.RepaintManager.paint(RepaintManager.java:1323)       在java.desktop / javax.swing.JComponent._paintImmediately(JComponent.java:5203)       在java.desktop / javax.swing.JComponent.paintImmediately(JComponent.java:5013)       在java.desktop / javax.swing.RepaintManager $ 4.run(RepaintManager.java:865)       在java.desktop / javax.swing.RepaintManager $ 4.run(RepaintManager.java:848)       在java.base / java.security.AccessController.doPrivileged(本机方法)       在java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)       在java.desktop / javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:848)       在java.desktop / javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:823)       在java.desktop / javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:772)       在java.desktop / javax.swing.RepaintManager $ ProcessingRunnable.run(RepaintManager.java:1890)       在java.desktop / java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)       在java.desktop / java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)       在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:721)       在java.desktop / java.awt.EventQueue $ 4.run(EventQueue.java:715)       在java.base / java.security.AccessController.doPrivileged(本机方法)       在java.base / java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)       在java.desktop / java.awt.EventQueue.dispatchEvent(EventQueue.java:740)       在java.desktop / java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)       在java.desktop / java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)       在java.desktop / java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)       在java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)       在java.desktop / java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)       在java.desktop / java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

import java.awt.*;
import java.awt.event.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;

public class JDessin extends JPanelAnime implements KeyListener {
static final int T_CANVAS = 500;      // taille du Canvas en pixels (carré)   
static final int D_INFLUENCE = 25;  
static final int D_MIN_DU_BORD = 30; 
Piste piste; // la piste
Vehicule vehicule;// le vehicule


public JDessin(int d) {
super(d); 
try {
   // appel du constructeur de la classe mère 
setPreferredSize (new Dimension(T_CANVAS, T_CANVAS));
vehicule = new Vehicule (T_CANVAS - 200, T_CANVAS - 200, 0, 0, 0, ImageIO.read(new File("voiture_exemple.jpg")) , "NORMAL");
addKeyListener(this);  // rend le canvas réactif au déplacement du véhicule
}
catch(IOException exc) {
    exc.printStackTrace();
}
}


/* dessin sur le Canvas (méthode invoquée automatiquement lorsque nécessaire) */
  public void paint(Graphics g) {
 // On dessinne le fond, puis le véhicule et la piste :
 g.setColor(Color.BLACK);
 g.fillRect(0, 0, T_CANVAS, T_CANVAS);
 g.setColor(Color.WHITE);
 g.fillRect(D_MIN_DU_BORD, D_MIN_DU_BORD, T_CANVAS - 2*D_MIN_DU_BORD, T_CANVAS - 2*D_MIN_DU_BORD);
 g.drawImage(vehicule.image, vehicule.x, vehicule.y,null);
 }

 }

0 个答案:

没有答案