EDITED (Updated code + question):
I'm working on creating a 2D top-down RPG and I'm having many troubles painting small 32x32 images (256 total). I've had success painting many squares of grading colors though. So i can confirm that the nested for-loop maps correctly. Based off of the code i have attached, can anyone help me understand where I go wrong?
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++;
}
}
}
Error log from console:
> 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)