0

So, today I decided to try to make my own game without using a tutorial, and if I have a problem, try to figure it out on my own. However, this problem is something that I don't understand.

Here is my code: Game class (where the image is supposed to be rendered):

public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;

private int width = 350;
private int height = 200;
private int scale = 3;

private Dimension size = new Dimension(width * scale, height * scale);

private Thread thread;
private boolean running = false;

private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

private Loader loader;

public Game() {
    JFrame frame = new JFrame("Game");

    frame.setPreferredSize(size);
    frame.setMaximumSize(size);
    frame.setMinimumSize(size);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.add(this);
    frame.setVisible(true);

    loader = new Loader();
}

public static void main(String[] args) {    
    Game game = new Game();
    game.start();

    new Images();
}

public void render() {
    BufferStrategy bs = super.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(Images.TEST, 10, 10, null);

    g.dispose();
    bs.show();
}

public synchronized void start() {
    if (running)
        return;
    else
        running = true;

    thread = new Thread(this);
    thread.start();
}

public synchronized void stop() {
    if (!running)
        return;
    else
        try {
            thread.join();
        } catch (Exception e) {
            e.printStackTrace();
        }
}

@Override
public void run() {

}

public int getWidth() {
    return width;
}

public int getHeight() {
    return height;
}

public int getScale() {
    return scale;
}

}

Loader class (where I load the image):

public class Loader {

public BufferedImage loadImage(String fileName) {
    try {
        System.out.println("Trying to load: " + fileName + " ... succeded!");
        return ImageIO.read(new File(fileName));
    } catch(Exception e) {
        e.printStackTrace();
    }
    System.out.println("Trying to load: " + fileName + " ... failed!");
    return null;
}

}

And my images class, where all of the images get set to a file:

public class Images {

public static Loader loader;

public static final BufferedImage TEST;


static {
    Loader loader = new Loader();

    TEST = loader.loadImage("res/test.png");
}

}

All I want to do, is simple display an image to the screen, yet this method does not seem to work. I do not know what I am doing wrong. And, no I haven't put in the wrong directory of the image. Thanks in advance!

1 Answers1

0

Since run is empty, nothing is performing an update/paint cycle. Start by updating the run method to perform call render...

@Override
public void run() {
    while (running) {
        render();
    }
}

The point of BufferStrategy is to take control of the painting process, so you now become responsible for performing it

Next, get rid of getWidth and getHeight, this is going to cause no end of issues and I wasted time trying to figure out why it wasn't displaying my full image.

The following...

frame.setPreferredSize(size);
frame.setMaximumSize(size);
frame.setMinimumSize(size);

is a bad idea, as the frame includes the window decorations, so your available content size will be reduced by the size of the windows decorations, which you are probably no expecting.

Instead, replace it with...

@Override
public Dimension getPreferredSize() {
    return size;
}

and the call pack on the window to pack the window decorations around your desired content size.

Don't get me started on frame.setResizable(false);

TEST = loader.loadImage("res/test.png"); worries me. If res/test.png is embedded within your application Jar then the loading process will fail. If it's externalised to the disk, then you are going to have issues with loading the images if the working directory is not the same as the installation directory - just be warned

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366