Killstone
Killstone

Reputation: 21

Trying to load an image in Java 11 just shows a blank screen

I am trying to make a 2d game using java, but I tried to load a picture in my program and BAM, blank screen with no image. I have tried to go on many websites and videos but still can't seem to find out the problem. Please let me know if you can solve the issue.

Here is my Image(blob:https://stackoverflow.com/a780e8ed-b1e4-48c8-be38-72097c5cde98) and my code down below.

Game Class

package com.helloworld;

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 = 8974055809715228424L;

    public static final int WIDTH = 640, HEIGHT = 480;
    
    private LoadImage loadImage;
    private Thread thread;
    private Handler handler;
    public boolean running = false;
    
    private BufferedImage testImage;
    
    public Game() {
        loadImage = new LoadImage();
        handler = new Handler();
        this.addKeyListener(new KeyInput(handler));
        
        handler.addObject(new Player(50, 100 , ID.Player));
        new Window(WIDTH,HEIGHT,"Game",this);
    }
    
    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }
    
    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        }catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 / amountOfTicks;
        double delta = 0;
        long timer = System.nanoTime();
        int frames = 0;
        while(running) {
            long now = System.nanoTime();
            delta += (now - lastTime) / ns;
            lastTime = now;
            while(delta >= 1) {
                tick();
                delta--;
            }
            if(running)
                render();
            frames++;
            
            if(System.currentTimeMillis() - timer > 1000 ) {
                timer += 1000;
                System.out.println("FPS: " + frames);
                frames = 0;
            }
        }
        stop();
    }
    
    private void tick() {
        handler.tick();
    }

    private void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs == null) {
            this.createBufferStrategy(3);
            return;
        }
        
        Graphics g = bs.getDrawGraphics();
        
        g.setColor(Color.white);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        
        g.drawImage(testImage, 20, 20, null);
        g.drawImage(LoadImage.picture, 300, 300, 50, 50, null);
        if (LoadImage.loaded) {
            
        }
        handler.render(g);
        g.dispose();
        bs.show();
    }

    public static void main(String[] args){ 
        new Game();
    }


}

Window Class

package com.helloworld;

import java.awt.Canvas;
import java.awt.Dimension;

import javax.swing.JFrame;

public class Window extends Canvas {

    private static final long serialVersionUID = -1661499041936726815L;
    
    public Window(int width, int height, String title, Game game) {
        JFrame frame = new JFrame(title);
        
        frame.setPreferredSize(new Dimension(width,height));
        frame.setMinimumSize(new Dimension(width,height));
        frame.setMaximumSize(new Dimension(width,height));
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.add(game);
        frame.setVisible(true);
        game.start();
    }
}

Handler Class

package com.helloworld;

import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

    LinkedList<GameObject> object = new LinkedList<GameObject>();
    
    public void tick() {
        for(int i = 0; i < object.size(); i++) {
            GameObject tempObject = object.get(i);
            
            tempObject.tick();
        }
    }
    
    public void render(Graphics g){
        for(int i = 0; i < object.size(); i++) {
            GameObject tempObject = object.get(i);
            tempObject.render(g);
        }
    }
    
    public void addObject(GameObject object) {
        this.object.add(object);
    }
    
    public void removeObject(GameObject object) {
        this.object.remove(object);
    }
}

Game Object Class

package com.helloworld;

import java.awt.Graphics;

public abstract class GameObject {
    
    protected int x,y;
    protected ID id;
    protected int velX, velY;
    
    public GameObject(int x, int y, ID id) {
        this.x = x;
        this.y = y;
        this.id = id;
        
    }
    public abstract void tick();
    public abstract void render(Graphics g);
    
    public void setX(int x) {
        this.x = x;
    }
    
    public void setY(int y) {
        this.y = y;
    }
        
    public int getX() {
        return x;
    }
    
    public int getY() {
        return y;
    }

    public void setId(ID id) {
        this.id = id;
    }
    
    public ID getId() {
        return id;
    }
    
    public void setVelX(int velX) {
        this.velX = velX;
    }
    
    public void setVelY(int velY) {
        this.velY = velY;
    }
    
    public int getVelX() {
        return velX;
    }
    
    public int getVelY() {
        return velY;
    }
}

Player Class

package com.helloworld;

import java.awt.Color;
import java.awt.Graphics;

public class Player extends GameObject{

    public Player(int x, int y, ID id) {
        super(x, y, id);
    }

    public void tick() {
        x += velX;
        y += velY;
        
    }
    public void render(Graphics g) {
        g.drawImage(LoadImage.picture, 300, 300, 50, 50, null);
        
    }
    

}

Key Input Class

package com.helloworld;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyInput extends KeyAdapter{
    
    private Handler handler;
    
    public KeyInput(Handler handler) {
        this.handler = handler;
    }
    public void keyPressed(KeyEvent e) {
        int key = e.getKeyCode();
        
        for(int i = 0; i < handler.object.size(); i++) {
            GameObject tempObject = handler.object.get(i);
            if(tempObject.getId() == ID.Player ) {
                if(key == KeyEvent.VK_D) {tempObject.setVelX(5);}
                if(key == KeyEvent.VK_A) {tempObject.setVelX(-5);}
                if(key == KeyEvent.VK_W) {tempObject.setVelY(-5);}
                if(key == KeyEvent.VK_S) {tempObject.setVelY(5);}
            
            }
        }
            
    }
    
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();
        for(int i = 0; i < handler.object.size(); i++) {
            GameObject tempObject = handler.object.get(i);
            if(tempObject.getId() == ID.Player) {
            if(key == KeyEvent.VK_D) tempObject.setVelX(0);
            if(key == KeyEvent.VK_A) tempObject.setVelX(0);
            if(key == KeyEvent.VK_W) tempObject.setVelY(0);
            if(key == KeyEvent.VK_S) tempObject.setVelY(0);
        }
    }
        
}
    
}

Image Loader Class

package com.helloworld;

import java.awt.*;
import javax.swing.ImageIcon;

public class LoadImage {
    
    public static Image picture;
    public static boolean loaded = false;

    public void loadPics(Graphics g) {
        picture = new ImageIcon("C:\\Desktop\\Programming\\Extra Stuff\\cube.jpg").getImage();
        loaded = true;
    }
}

IDs

package com.helloworld;

public enum ID {
    
    Player(),
    
}

Upvotes: 0

Views: 583

Answers (1)

ozkanpakdil
ozkanpakdil

Reputation: 4622

You are drawing testImage but you never set it, try code below

public Game() throws Exception {
        loadImage = new LoadImage();
        URL urlInput = new URL("https://memorynotfound.com/wp-content/uploads/java-duke.png");
        testImage = ImageIO.read(urlInput);
        handler = new Handler();
        this.addKeyListener(new KeyInput(handler));

        handler.addObject(new Player(50, 100, ID.Player));
        new Window(WIDTH, HEIGHT, "Game", this);
    }

how it looks in my local hello from duke

If you want to load image from your project, it depends to your project structure, for maven, put the image under src/main/resources/some.jpg and load it like code below

public Game() throws Exception {
    loadImage = new LoadImage();
    testImage = ImageIO.read(getClass().getClassLoader().getResource("pugselfie.jpg"));
    handler = new Handler();
    this.addKeyListener(new KeyInput(handler));

    handler.addObject(new Player(50, 100, ID.Player));
    new Window(WIDTH, HEIGHT, "Game", this);
}

This is how it looks in my local pugs selfie in java

if you want to load the image from any path, just make sure file is there and path given correct in your code, example

public Game() throws Exception {
    loadImage = new LoadImage();
    testImage = ImageIO.read(new File("c:\\tmp\\pugselfie.jpg"));
    handler = new Handler();
    this.addKeyListener(new KeyInput(handler));

    handler.addObject(new Player(50, 100, ID.Player));
    new Window(WIDTH, HEIGHT, "Game", this);
}

here how it looks the project structure and UI in my local

pugselfie from tmp folder

Upvotes: 1

Related Questions