这是我早期的一个跟进问题。我正在制作一个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背景和角色结合起来?谁能指出我正确的方向来获得背景展示?渲染方法应该不关心背景吗?我只需要背景一次。它不需要像健康栏和角色那样不断更新吗?
答案 0 :(得分:2)
让我们开始......
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);
是一个坏主意,因为框架有边框占据框架本身的空间。
最好覆盖getPreferredSize
的{{1}}方法,并提供您要使用的默认大小值,然后在框架上调用Canvas
。这将计算框架的大小作为其优先尺寸的内容加上框架边框要求......
你"游戏循环"正在疯狂......
pack
基本上,这将尽可能快地运行,并将减少其他线程运行的机会,最终将您的游戏(可能是您的PC)带到它的膝盖
这是简单的"运行循环的概念......
while (running) {
delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1) {
tick();
delta--;
}
render();
}
基本上,它会尝试确保每次迭代之间有足够的延迟,以保持您尝试定位的60fps ...而不会使系统挨饿......