I have been working on a program to run a basic 2 dimensional sidescrolling game. Right now I am trying to get the basic image window working. I was able to get a simple version to run, but now that I have developed a more complicated code, I am getting errors when I try to run the program. Here is the bit of code for the main program (I can give the constructors for environment() class if needed).
import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.Timer; public class gameTester extends JFrame { private KeyboardPanel keyboardPanel = new KeyboardPanel(); private static int charPoint[] = {30, 120}; private static int bgPoint[] = {0, -300}; private static environment[] environment; public static int[] getCharPoint() { return charPoint; } public static void setCharPoint(int[] charPoint) { gameTester.charPoint = charPoint; } public static int[] getBgPoint() { return bgPoint; } public static void setBgPoint(int[] bgPoint) { gameTester.bgPoint = bgPoint; } public static environment[] getEnvironment() { return environment; } public static void setEnvironment(environment[] environment) { gameTester.environment = environment; } public static void main(String[] args){ int[][] layout = getMap0(); int temp = 0; for (int i = 0; i < layout.length; i++){ for (int j = 0; j < layout[1].length; j++){ if (layout[i][j] == 1){ temp++; } } } environment[] tempEnvironment = new environment[temp]; for (int i = 0; i < layout.length; i++){ for (int j = 0; j < layout[1].length; j++){ if (layout[i][j] == 1){ int point[] = {i * 50, j * 50}; tempEnvironment[i] = new environment("basicBlock", point); } } } environment = tempEnvironment; gameTester frame = new gameTester(); frame.setTitle("Tester"); frame.setSize(400, 300); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public gameTester(){ String vp = "null"; setLayout(new BorderLayout(10, 20)); add(new AnimatedPanel(vp), BorderLayout.CENTER); add(keyboardPanel, BorderLayout.SOUTH); keyboardPanel.setFocusable(true); } static class AnimatedPanel extends JPanel{ ImageIcon playerIcon = new ImageIcon("Documents/Projects/Photoshop/NinjaRight.gif"); Image playerIcon2 = playerIcon.getImage(); ImageIcon background = new ImageIcon("Documents/Projects/Photoshop/backgroundtest.gif"); Image background2 = background.getImage(); ImageIcon basicBlock = new ImageIcon("Documents/Projects/Photoshop/block.gif"); Image basicBlock2 = basicBlock.getImage(); public AnimatedPanel(String message){ Timer timer = new Timer(62, new TimerListener()); timer.start(); } protected void paintComponent(Graphics g){ super.paintComponent(g); g.drawImage(background2, bgPoint[0], bgPoint[1], 800, 600, this); for (int i = 0; i < environment.length; i++){ int[] temp = environment[i].getPoint(); g.drawImage(basicBlock2, temp[0], temp[1], 50, 50, this); } g.drawImage(playerIcon2, charPoint[0], charPoint[1], 90, 50, this); } class TimerListener implements ActionListener{ public void actionPerformed(ActionEvent e){ repaint(); } } } static class KeyboardPanel extends JPanel{ private int x; private int y; private int x2; private int y2; public KeyboardPanel(){ addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ switch (e.getKeyCode()){ case KeyEvent.VK_DOWN: break; case KeyEvent.VK_UP: break; } repaint(); } }); } } public static int[][] getMap0(){ int[][] map = new int[32][12]; map[0][0] = 1; map[1][0] = 0; map[2][0] = 0; map[3][0] = 0; map[4][0] = 0; map[5][0] = 0; map[6][0] = 0; map[7][0] = 0; map[8][0] = 0; map[9][0] = 0; map[10][0] = 0; map[11][0] = 0; map[12][0] = 0; map[13][0] = 0; map[14][0] = 0; map[15][0] = 0; map[16][0] = 0; map[17][0] = 0; map[18][0] = 0; map[19][0] = 0; map[20][0] = 0; map[21][0] = 0; map[22][0] = 0; map[23][0] = 0; map[24][0] = 0; map[25][0] = 0; map[26][0] = 0; map[27][0] = 0; map[28][0] = 0; map[29][0] = 0; map[30][0] = 0; map[31][0] = 1; map[0][1] = 1; map[1][1] = 0; map[2][1] = 0; map[3][1] = 0; map[4][1] = 0; map[5][1] = 0; map[6][1] = 0; map[7][1] = 0; map[8][1] = 0; map[9][1] = 0; map[10][1] = 0; map[11][1] = 0; map[12][1] = 0; map[13][1] = 0; map[14][1] = 0; map[15][1] = 0; map[16][1] = 0; map[17][1] = 0; map[18][1] = 0; map[19][1] = 0; map[20][1] = 0; map[21][1] = 0; map[22][1] = 0; map[23][1] = 0; map[24][1] = 0; map[25][1] = 0; map[26][1] = 0; map[27][1] = 0; map[28][1] = 0; map[29][1] = 0; map[30][1] = 0; map[31][1] = 1; map[0][2] = 1; map[1][2] = 0; map[2][2] = 0; map[3][2] = 0; map[4][2] = 0; map[5][2] = 0; map[6][2] = 0; map[7][2] = 0; map[8][2] = 0; map[9][2] = 0; map[10][2] = 0; map[11][2] = 0; map[12][2] = 0; map[13][2] = 0; map[14][2] = 0; map[15][2] = 0; map[16][2] = 0; map[17][2] = 0; map[18][2] = 0; map[19][2] = 0; map[20][2] = 0; map[21][2] = 0; map[22][2] = 0; map[23][2] = 0; map[24][2] = 0; map[25][2] = 0; map[26][2] = 0; map[27][2] = 0; map[28][2] = 0; map[29][2] = 0; map[30][2] = 0; map[31][2] = 1; map[0][3] = 1; map[1][3] = 0; map[2][3] = 0; map[3][3] = 0; map[4][3] = 0; map[5][3] = 0; map[6][3] = 0; map[7][3] = 0; map[8][3] = 0; map[9][3] = 0; map[10][3] = 0; map[11][3] = 0; map[12][3] = 0; map[13][3] = 0; map[14][3] = 0; map[15][3] = 0; map[16][3] = 0; map[17][3] = 0; map[18][3] = 0; map[19][3] = 0; map[20][3] = 0; map[21][3] = 0; map[22][3] = 0; map[23][3] = 0; map[24][3] = 0; map[25][3] = 0; map[26][3] = 0; map[27][3] = 0; map[28][3] = 0; map[29][3] = 0; map[30][3] = 0; map[31][3] = 1; map[0][4] = 1; map[1][4] = 0; map[2][4] = 0; map[3][4] = 0; map[4][4] = 0; map[5][4] = 0; map[6][4] = 0; map[7][4] = 0; map[8][4] = 0; map[9][4] = 0; map[10][4] = 0; map[11][4] = 0; map[12][4] = 0; map[13][4] = 0; map[14][4] = 0; map[15][4] = 0; map[16][4] = 0; map[17][4] = 0; map[18][4] = 0; map[19][4] = 0; map[20][4] = 0; map[21][4] = 0; map[22][4] = 0; map[23][4] = 0; map[24][4] = 0; map[25][4] = 0; map[26][4] = 0; map[27][4] = 0; map[28][4] = 0; map[29][4] = 0; map[30][4] = 0; map[31][4] = 1; map[0][5] = 1; map[1][5] = 0; map[2][5] = 0; map[3][5] = 0; map[4][5] = 0; map[5][5] = 0; map[6][5] = 0; map[7][5] = 0; map[8][5] = 0; map[9][5] = 0; map[10][5] = 0; map[11][5] = 0; map[12][5] = 0; map[13][5] = 0; map[14][5] = 0; map[15][5] = 0; map[16][5] = 0; map[17][5] = 0; map[18][5] = 0; map[19][5] = 0; map[20][5] = 0; map[21][5] = 0; map[22][5] = 0; map[23][5] = 0; map[24][5] = 0; map[25][5] = 0; map[26][5] = 0; map[27][5] = 0; map[28][5] = 0; map[29][5] = 0; map[30][5] = 0; map[31][5] = 1; map[0][6] = 1; map[1][6] = 0; map[2][6] = 0; map[3][6] = 0; map[4][6] = 0; map[5][6] = 0; map[6][6] = 0; map[7][6] = 0; map[8][6] = 0; map[9][6] = 0; map[10][6] = 0; map[11][6] = 0; map[12][6] = 0; map[13][6] = 0; map[14][6] = 0; map[15][6] = 0; map[16][6] = 0; map[17][6] = 0; map[18][6] = 0; map[19][6] = 0; map[20][6] = 0; map[21][6] = 0; map[22][6] = 0; map[23][6] = 0; map[24][6] = 0; map[25][6] = 0; map[26][6] = 0; map[27][6] = 0; map[28][6] = 0; map[29][6] = 0; map[30][6] = 0; map[31][6] = 1; map[0][7] = 1; map[1][7] = 0; map[2][7] = 0; map[3][7] = 0; map[4][7] = 0; map[5][7] = 0; map[6][7] = 0; map[7][7] = 0; map[8][7] = 0; map[9][7] = 0; map[10][7] = 0; map[11][7] = 0; map[12][7] = 0; map[13][7] = 0; map[14][7] = 0; map[15][7] = 0; map[16][7] = 0; map[17][7] = 0; map[18][7] = 0; map[19][7] = 0; map[20][7] = 0; map[21][7] = 0; map[22][7] = 0; map[23][7] = 0; map[24][7] = 0; map[25][7] = 0; map[26][7] = 0; map[27][7] = 0; map[28][7] = 0; map[29][7] = 0; map[30][7] = 0; map[31][7] = 1; map[0][8] = 1; map[1][8] = 0; map[2][8] = 0; map[3][8] = 0; map[4][8] = 0; map[5][8] = 0; map[6][8] = 0; map[7][8] = 0; map[8][8] = 0; map[9][8] = 0; map[10][8] = 0; map[11][8] = 0; map[12][8] = 0; map[13][8] = 0; map[14][8] = 0; map[15][8] = 0; map[16][8] = 0; map[17][8] = 0; map[18][8] = 0; map[19][8] = 0; map[20][8] = 0; map[21][8] = 0; map[22][8] = 0; map[23][8] = 0; map[24][8] = 0; map[25][8] = 0; map[26][8] = 0; map[27][8] = 0; map[28][8] = 0; map[29][8] = 0; map[30][8] = 0; map[31][8] = 1; map[0][9] = 1; map[1][9] = 0; map[2][9] = 0; map[3][9] = 0; map[4][9] = 0; map[5][9] = 0; map[6][9] = 0; map[7][9] = 0; map[8][9] = 0; map[9][9] = 0; map[10][9] = 0; map[11][9] = 0; map[12][9] = 0; map[13][9] = 0; map[14][9] = 0; map[15][9] = 0; map[16][9] = 0; map[17][9] = 0; map[18][9] = 0; map[19][9] = 0; map[20][9] = 0; map[21][9] = 0; map[22][9] = 0; map[23][9] = 0; map[24][9] = 0; map[25][9] = 0; map[26][9] = 0; map[27][9] = 0; map[28][9] = 0; map[29][9] = 0; map[30][9] = 0; map[31][9] = 1; map[0][10] = 1; map[1][10] = 0; map[2][10] = 0; map[3][10] = 0; map[4][10] = 0; map[5][10] = 0; map[6][10] = 0; map[7][10] = 0; map[8][10] = 0; map[9][10] = 0; map[10][10] = 0; map[11][10] = 0; map[12][10] = 0; map[13][10] = 0; map[14][10] = 0; map[15][10] = 0; map[16][10] = 0; map[17][10] = 0; map[18][10] = 0; map[19][10] = 0; map[20][10] = 0; map[21][10] = 0; map[22][10] = 0; map[23][10] = 0; map[24][10] = 0; map[25][10] = 0; map[26][10] = 0; map[27][10] = 0; map[28][10] = 0; map[29][10] = 0; map[30][10] = 0; map[31][10] = 1; map[0][11] = 1; map[1][11] = 1; map[2][11] = 1; map[3][11] = 1; map[4][11] = 1; map[5][11] = 1; map[6][11] = 1; map[7][11] = 1; map[8][11] = 1; map[9][11] = 1; map[10][11] = 1; map[11][11] = 1; map[12][11] = 1; map[13][11] = 1; map[14][11] = 1; map[15][11] = 1; map[16][11] = 1; map[17][11] = 1; map[18][11] = 1; map[19][11] = 1; map[20][11] = 1; map[21][11] = 1; map[22][11] = 1; map[23][11] = 1; map[24][11] = 1; map[25][11] = 1; map[26][11] = 1; map[27][11] = 1; map[28][11] = 1; map[29][11] = 1; map[30][11] = 1; map[31][11] = 1; return map; } }
Error code on the console looks something like this:
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694) at javax.swing.RepaintManager.access$700(RepaintManager.java:41) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access$000(EventQueue.java:85) at java.awt.EventQueue$1.run(EventQueue.java:643) at java.awt.EventQueue$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method)
etc….. for a vastly large quantity of lines.
EDIT: The error only occurs after everything is already loaded, and it seems to happen when the timer attempts to redraw the screen. Ive commented out timer, and still get the error when the program tries to draw its initial screen. It has something to do with my paintComponent(Graphics g) command, but I am not sure where exactly.