Hey can someone help me out, I've been trying to fix this for about 2 hours now, and I'm a noob when it comes to game programming and Java(only been reading and programming with it for a week now, this is my first non-tutorial driven attempt at anything).
Basically from my understanding my tile map class should be done correctly, here's the code in case I am mistaken: (file name is Map.java and its part of the tilemap package)
package tilemap;
import java.awt.Graphics2D;
import java.awt.*;
import javax.swing.*;
public class Map{
private final int TILE_SIZE = 40;
private Image wall;
private Image floor;
private int[][] map = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }};
public Map() {
wall = new ImageIcon("wall.gif").getImage();
floor = new ImageIcon("floor.gif").getImage();
}
public void paint(Graphics2D g) {
// loop through all the tiles in the map rendering them
// based on whether they block or not
for (int across=0;across<10;across++) {
for (int down=0;down<13;down++) {
if (map[across][down] == 1) {
g.drawImage(wall, across*TILE_SIZE, down*TILE_SIZE, null);
} else {
g.drawImage(floor, across*TILE_SIZE, down*TILE_SIZE, null);
}
}
}
}
}
Now I think the issue is in this section here in my GameCanvas.java:
public void gameLoop() {
boolean gameRunning = true;
long last = System.nanoTime();
// keep looking while the game is running
while (gameRunning) {
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
// clear the screen
g.setColor(Color.black);
g.fillRect(0,0,1000,1000);
// render our game objects
map.paint(g);
// flip the buffer so we can see the rendering
g.dispose();
strategy.show();
// pause a bit so that we don't choke the system
try { Thread.sleep(4); } catch (Exception e) {};
}
}
And the full GameCanvas.java in case its else where:
package tilemap;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import tilemap.Map;
public class GameCanvas extends Canvas {
/** The buffered strategy used for accelerated rendering */
private BufferStrategy strategy;
/** The map our player will wander round */
private Map map;
public static void main(String[] args) {
new GameCanvas();
}
public GameCanvas() {
Frame frame = new Frame("Tile Map Example");
frame.setLayout(null);
frame.add(this);
frame.setSize(500,500);
frame.setResizable(true);
frame.setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
map = new Map();
// start the game loop
gameLoop();
}
public void gameLoop() {
boolean gameRunning = true;
long last = System.nanoTime();
// keep looking while the game is running
while (gameRunning) {
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
// clear the screen
g.setColor(Color.black);
g.fillRect(0,0,1000,1000);
// render our game objects
map.paint(g);
// flip the buffer so we can see the rendering
g.dispose();
strategy.show();
// pause a bit so that we don't choke the system
try { Thread.sleep(4); } catch (Exception e) {};
}
}
}
I think the issue is when I go to draw/paint the screen there's something I'm not taking care of correctly, or maybe I'm not calling the right method (I've seen like 3 ways to draw to the screen, Canvas (which I think I'm using), JFrames/Jpanels, and OpenGL(which I have no idea about))
Please let me know what the issue is if you can spot it, cause I really have no idea without an compilation error poping up.
Thank you!