Hi,
I am practicing my coding and having problems loading my tiled map onto my canvas. Please review this code and let me know where my mistake is? I have the problematic code surrounded with START and END comments. I greatly appreciate your help!
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
static Player player;
InputHandler IH;
JFrame frame;
public final int WIDTH = 400;
public final int HEIGHT = WIDTH / 16 * 9;
public final int SCALE = 2;
public final Dimension gameSize = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
public final String TITLE = "2dDemo";
public static int tickCount;
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
static boolean gameRunning = false;
Container cont;
String directory = "res/";
int tileSize = 10;
//2 dimensional array map
String testArena[][] = {
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "},
{" ", "#", "#", " ", " ", " ", " ", " ", "|", " "},
{" ", " ", " ", "#", " ", " ", "#", " ", "|", " "},
{" ", " ", " ", " ", " ", "#", " ", " ", "|", " "},
{" ", " ", " ", " ", "#", " ", " ", " ", "|", " "},
{" ", " ", "#", "#", " ", " ", " ", " ", "|", " "},
{" ", " ", " ", " ", " ", " ", " ", " ", "|", " "},
{"#", "#", "#", "#", "#", "#", "#", "#", "#", "#"},
};
public void run() {
while (gameRunning) {
tick();
render();
try{
Thread.sleep(10);
}catch(Exception e){
e.printStackTrace();
}
//System.out.println("TickCount: " + tickCount);
}
}
public synchronized void start() {
gameRunning = true;
new Thread(this).start();
}
public static synchronized void stop() {
gameRunning = false;
System.exit(0);
}
public Game() {
frame = new JFrame();
setMinimumSize(gameSize);
setPreferredSize(gameSize);
setMaximumSize(gameSize);
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
frame.setTitle(TITLE);
frame.setLocationRelativeTo(null);
IH = new InputHandler(this);
frame.addKeyListener(IH);
player = new Player(getWidth() / 2, getHeight() / 2);
///////////////Start Problematic Code
frame.getContentPane();
frame.setLayout(null);
//generate the map
for(int i = 0; i < testArena.length; i++) {
for(int j = 0; j < testArena[0].length; j++){
JLabel lbl = null;
if(testArena[j][i].equals("#")){
lbl = new JLabel(new ImageIcon(directory + "ground.png"));
} else if(testArena[j][i].equals(" ")){
lbl = new JLabel(new ImageIcon(directory + "air.png"));
} else if(testArena[j][i].equals("|")){
lbl = new JLabel(new ImageIcon(directory + "ladder.png"));
}
cont.add(lbl); //Add map to the screen
lbl.setBounds(i*tileSize, j*tileSize, tileSize, tileSize); //Tile scale
}
}
repaint();
frame.validate();
frame.setContentPane(cont);
///////////////END Problematic Code
}
public void tick() {
player.tick(this);
tickCount++;
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.setColor(new Color(72, 152, 72));;
g.fillRect(0, 0, getWidth(), getHeight());
//credits
g.setColor(new Color(40, 40, 40));
g.drawString("unenergizer", getWidth() - 70, getHeight() - 5);
//Render player
player.render(g);
g.dispose();
bs.show();
}
public static void main(String[] args) {
System.out.println("Made By unenergizer");
Game game = new Game();
game.start();
}
}
In the editor it get no errors, until run time. Here is what it says:
Thanks for the assistance!