在JPanel上绘制一系列图块图像

时间:2017-03-06 20:22:05

标签: java swing nullpointerexception graphics2d imageobserver

已编辑(更新代码+问题):

我正致力于创建2D自上而下的RPG,并且我在绘制小型32x32图像(总共256张)时遇到了很多麻烦。我已经成功地绘制了许多分级颜色的方块。所以我可以确认嵌套的for循环正确映射。基于我附上的代码,任何人都可以帮助我理解我哪里出错了吗?

public class TestClass extends JPanel {
public JPanel floor;
public Point[] imgPoints;
public Image[] tileImgs;
private Dimension d = new Dimension(512, 512);
private Graphics g;
private int size;


/*
 * This is the Floor class which is supposed to print level tiles 
 * tilesize is specified by 'int size'.
 * Constructor instantiates a JPanel which is
 * used inside a graphical interface (JFrame)
 */

public TestClass() throws IOException {
    floor = new JPanel();
    floor.setSize(new Dimension(512, 512));
    floor.setMinimumSize(new Dimension(512, 512));

    tileImgs = new Image[256];
    int k = 0;
    int xImg = 1;
    for (Image x : tileImgs) {
        if (xImg < 10){
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + "00" + xImg + ".png"));
        }
        else if (xImg < 100 && xImg >= 10){
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + "0" + xImg + ".png"));
            }
        else{
            tileImgs[k] = ImageIO.read(this.getClass().getResource("/hyp/hyptosis" + xImg + ".png"));   
        }
        k++;
    }
}

public void paint(Graphics g) {
    super.paint(g);
    setDoubleBuffered(true);
    int c = 0;
    int pointCount = 0;
    size = 32;
    for (int i = 0; i < d.getWidth(); i += size) {
        for (int j = 0; j < d.getHeight(); j += size) {
            pointCount++;
        }
    }
    c = 0;
    Point[] imgPoints = new Point[pointCount];
    for (int i = 0; i < d.getWidth(); i += size) {
        for (int j = 0; j < d.getHeight(); j += size) {
            imgPoints[c] = (new Point(i, j));
            c++;
        }
    }

    paintComponent();

}

// Paint level-tiles.
private void paintComponent() {
    int k = 0;
    for (Point q : imgPoints) {
        q = imgPoints[k];
        ImageObserver imgObs = null;
        int xx = (int) q.getX();
        int yy = (int) q.getY();
        g.drawImage(tileImgs[k], xx, yy, imgObs);
        this.imageUpdate(tileImgs[k], FRAMEBITS, xx, yy, size, size);
        repaint();
        k++;

    }
}

}

来自控制台的错误日志:

> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at gameObjects.TestClass.paintComponent(TestClass.java:82)
    at gameObjects.TestClass.paint(TestClass.java:76)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1200(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(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.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)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at javax.swing.JComponent.paintComponent(Unknown Source)
    at gameObjects.TestClass.paintComponent(TestClass.java:82)
    at gameObjects.TestClass.paint(TestClass.java:76)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintToOffscreen(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
    at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
    at javax.swing.RepaintManager.paint(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at java.awt.Window.paint(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at javax.swing.RepaintManager$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
    at javax.swing.RepaintManager.access$1200(Unknown Source)
    at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(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.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)

0 个答案:

没有答案