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!