Reputation: 5
I've been following a tutorial series on youtube about making a game in java and at one point in the tutorial the program had to read the RGB values of each individual pixel in a 64x64 PNG image and create instances of classes accordingly but, when I finished the code all I got was a blank screen. The image was created in paint.net and I'm using eclipse neon 2. Thanks in advance! :)
Here's my code:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
public class Game extends Canvas implements Runnable{
private static final long serialVersionUID = 1L;
private boolean isRunning = false;
private Thread thread;
private Handler handler;
private SpriteSheet ss;
private BufferedImage level = null;
private BufferedImage sprite_sheet = null;
private BufferedImage floor = null;
public Game(){
new Window(1000, 563, "Wizard Game", this);
start();
handler = new Handler();
this.addKeyListener(new KeyInput(handler));
BufferedImageLoader loader = new BufferedImageLoader();
level = loader.LoadImage("wizard_level.png");
loadLevel(level);
sprite_sheet = loader.LoadImage("/sprite_sheet.png");
floor = ss.grabImage(4, 2, 32, 32);
}
private void start(){
isRunning = true;
thread = new Thread(this);
thread.start();
}
private void stop(){
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 60;
while(isRunning){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
delta --;
}
if(isRunning)
render();
frames ++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
public void tick(){
handler.tick();
}
public void render(){
BufferStrategy bs = this.getBufferStrategy();
if(bs == null){
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
for(int xx = 0; xx < 30*72; xx += 32){
for(int yy = 0; yy < 30*72; yy += 32){
g.drawImage(floor, xx, yy, null);
}
}
handler.render(g);
g.dispose();
bs.show();
}
private void loadLevel(BufferedImage image){
int w = image.getWidth();
int h = image.getHeight();
for(int xx = 0; xx > w; xx++){
for(int yy = 0; yy < h; yy++){
int pixel = image.getRGB(xx, yy);
int red = (pixel >> 16) & 0xff;
int blue = (pixel >> 8) & 0xff;
int green = (pixel) & 0xff;
if(red == 255)
handler.addObject(new Block(xx*32, yy*32, ID.Block, ss));
if(blue == 255)
handler.addObject(new Wizard(xx*32, yy*32, ID.Player, handler, ss));
}
}
}
public static void main(String args[]){
new Game();
}
}
Upvotes: 0
Views: 350
Reputation: 2778
The following for loop won't execute..
for(int xx = 0; xx > w; xx++){
it will loop while xx > w
which will always be false (where xx starts as 0 and the image has a positive width)
as per the yy
loop within it which is correct, change the greater than to less than
for(int xx = 0; xx < w; xx++){
if you are still not getting expected results, log the actual values of red
and blue
to see which colour elements are actually in the image. objects are only created when the red and blue components of a pixel are 255
Upvotes: 2