我正在按照CodeNMore进行本教程的学习,直到他说出如何加载图像和校正为止,一切都很好。它没有给我任何错误,但是除了空白窗口外没有显示任何其他内容。我尝试查看他的代码,然后查看我的代码,但没有发现任何区别。
Game.java:
package dev.dylandummy.masterquest;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import dev.dylandummy.masterquest.display.Display;
import dev.dylandummy.masterquest.gfx.ImageLoader;
public class Game implements Runnable {
private Display display;
public int width, height;
public String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
private BufferedImage testImage;
public Game(String title, int width, int heigth){
this.width = width;
this.height = heigth;
this.title = title;
}
private void init() {
display = new Display(title, width, height);
testImage = ImageLoader.loadImage("/textures/test.png");
}
private void tick() {
}
private void render() {
bs = display.getCanvas().getBufferStrategy();
if(bs == null) {
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
//Clear Screen
g.clearRect(0, 0, width, height);
//Draw Here!
g.drawImage(testImage, 20, 20, null);
//End Draw!
bs.show();
bs.dispose();
}
public void run() {
init();
while(running) {
tick();
render();
}
stop();
}
public synchronized void start() {
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Display.java:
package dev.dylandummy.masterquest.display;
import java.awt.Canvas;
import java.awt.Dimension;
import javax.swing.JFrame;
public class Display {
private JFrame frame;
private Canvas canvas;
private String title;
private int width, height;
public Display(String title, int width, int heigth) {
this.title = title;
this.width = width;
this.height = heigth;
createDisplay();
}
private void createDisplay() {
frame = new JFrame(title);
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
canvas = new Canvas();
canvas.setPreferredSize(new Dimension(width, height));
canvas.setMaximumSize(new Dimension(width, height));
canvas.setMinimumSize(new Dimension(width, height));
frame.add(canvas);
frame.pack();
}
public Canvas getCanvas(){
return canvas;
}
}
ImageLoader.java:
package dev.dylandummy.masterquest.gfx;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImageLoader {
public static BufferedImage loadImage(String path) {
try {
return ImageIO.read(ImageLoader.class.getResource(path));
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}
}
Launcher.java:
package dev.dylandummy.masterquest;
public class Launcher {
public static void main(String[] args) {
Game game = new Game("The Master Quest", 640, 360);
game.start();
}
}
现在,对不起,如果这太多代码无法浏览,但是如果您有时间,您可以浏览一下并告诉我哪里出了问题吗?