[英]Background image not showing in Java Game
這是我的一個較早的問題的跟進問題。 我正在制作一個Java游戲,它基本上是一個帶有字符圖像的JFrame,一些由fillRect()組成的健康欄都位於背景圖像之上。 問題是出現了健康欄和角色,但沒有背景圖像。
這是Game類的簡化版本,它具有main()和render()方法:
public class Game extends Canvas implements Runnable{
public static boolean running = false;
public Thread gameThread;
private BufferedImage playerSpriteSheet;
private ImageManager im;
private static Player player;
private static HealthBar healthBars;
private static BackgroundImage backgroundImage;
public void init(){
ImageLoader loader = new ImageLoader();
playerSpriteSheet = loader.load("/spriteSheet.png");
SpriteSheet pss = new SpriteSheet(playerSpriteSheet);
im = new ImageManager(pss);
backgroundImage = new BackgroundImage("/background.png");
player = new Player(800, 250, im);
healthBars = new HealthBar(200, 200);
this.addKeyListener(new KeyManager());
}
public synchronized void start() {
if(running)return;
running = true;
gameThread = new Thread(this);
gameThread.start();
}
public synchronized void stop() {
if(!running)return;
running = false;
try {
gameThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void run() {
init();
long lastTime = System.nanoTime();
final double amountOfTicks = 60D;
double ns = 1_000_000_000/amountOfTicks;
double delta = 0;
long now = System.nanoTime();
while(running)
{
delta += (now - lastTime)/ns;
lastTime = now;
if(delta >= 1)
{
tick();
delta--;
}
render();
}
stop();
}
public void tick() {
player.tick();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null)
{
createBufferStrategy(3); //Use 5 at most
return;
}
Graphics g = bs.getDrawGraphics();
//RENDER HERE
backgroundImage.render(g);
player.render(g);
healthBars.render(g);
//END RENDER
g.dispose();
bs.show();
}
public static void main(String[] args)
{
JLabel backgroundImage;
JLabel controlKeyPanel;
JLabel statusLabel;
Game game = new Game();
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame("Title");
frame.setResizable(false);
frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
frame.setLayout(new BorderLayout());
backgroundImage = new JLabel(new ImageIcon("/background.png"));
String htmlButtonGuide = "words";
controlKeyPanel = new JLabel(htmlButtonGuide);
statusLabel = new JLabel("label");
frame.add(backgroundImage, BorderLayout.CENTER);
frame.add(controlKeyPanel, BorderLayout.EAST);
frame.add(statusLabel, BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(game);
frame.setVisible(true);
game.start();
//Program seems to continue running after ESC
}
public static Player getPlayer() {
return player;
}
}
這是BackGroundImage類:
public class BackgroundImage {
private Image background = null;
public BackgroundImage(String s) {
if(s == null)
{
background = getImage(s);
}
}
public void render(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(background, 0, 0, 1200, 600, null);
}
public Image getImage(String path) {
Image tempImage = null;
File image2 = new File(path);
try {
tempImage = ImageIO.read(image2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tempImage;
}
}
我關心的是render()方法重用“ g” Graphics對象將所有3件事添加到屏幕上。 有人告訴我不要將輕質的Swing健康欄與沉重的AWT背景和特征結合在一起? 誰能指出我正確的方向以顯示背景? 渲染方法不應該照顧背景嗎? 我只需要設置一次背景。 它不需要像健康欄和角色一樣不斷更新,對嗎?
讓我們從...開始
backgroundImage = new BackgroundImage("/background.png");
變成...
File image2 = new File(path);
要么
File image2 = new File("/background.png");
這樣您就可以看到...這有問題嗎? 這是請求駐留在當前驅動器根目錄中的文件...不是我想的那樣...
圖像存儲在主項目文件夾中的“ res”文件夾中
這將建議您使用...
backgroundImage = new BackgroundImage("res/background.png");
假設圖像不是嵌入式資源。
下一個...
public BackgroundImage(String s) {
if (s == null) {
background = getImage(s);
}
}
因此,您只想在引用為null
嘗試加載圖像?
旁注...
frame.setSize(WIDTH * SCALE, HEIGHT * SCALE);
這是一個壞主意,因為框架的邊框會占據框架本身側面的空間。
最好重寫Canvas
的getPreferredSize
方法,並提供要使用的默認大小值,然后在框架上調用pack
。 這會將框架的大小計算為內容的首選大小加上框架邊框的要求...
您的“游戲循環”正在瘋狂……
while (running) {
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick();
delta--;
}
render();
}
基本上,這將盡可能快地運行,並減少其他線程運行的機會,最終使您的游戲(可能還有您的PC)屈服
這是運行循環的“簡單”概念。
public void run() {
init();
final long amountOfTicks = 60;
long ns = Math.round(1_000_000_000 / (double)amountOfTicks);
int frames = 0;
long frameStart = System.currentTimeMillis();
while (running) {
long startedAt = System.nanoTime();
tick();
render();
long completedAt = System.nanoTime();
long duration = completedAt - startedAt;
long frameEnd = System.currentTimeMillis();
if (frameEnd - frameStart >= 1000) {
System.out.println(frames);
frames = 0;
frameStart = System.currentTimeMillis();
} else {
frames++;
}
long rest = ns - duration;
if (rest > 0) {
rest = TimeUnit.MILLISECONDS.convert(rest, TimeUnit.NANOSECONDS);
try {
Thread.sleep(rest);
} catch (InterruptedException ex) {
}
}
}
stop();
}
基本上,它試圖確保每次迭代之間都有足夠的延遲,以維持您要定位的60fps ...而不會餓死系統...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.