渲染游戏时出现白屏

时间:2019-02-09 10:18:12

标签: java

当我运行游戏时,JFrame只是白色的。有人可以解释吗? 我不知道为什么会这样,而且我很难发现可能是什么问题。我希望你们中的一个可以解释/知道答案。我期待听到您的回答,我很乐意继续编码,但我陷入了atm的困境-Artycal。

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.IOException;

public class Game extends Canvas implements Runnable {

static GraphicsDevice device = GraphicsEnvironment
        .getLocalGraphicsEnvironment().getScreenDevices()[0];

private static JFrame frame;

private static Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

private BufferedImage image = new BufferedImage((int)width, (int)height, BufferedImage.TYPE_INT_ARGB);
private BufferedImage spriteSheet = null;

private static double width = screenSize.getWidth();
private static double height = screenSize.getHeight();

private boolean running;
private Thread thread;

private BufferedImage player;

public void  init() {
    BufferedImageLoader loader = new BufferedImageLoader();
    try{
        spriteSheet = loader.loadImage("/sprite_sheet.png");
    }catch (IOException e){
        e.printStackTrace();
    }

    SpriteSheet ss = new SpriteSheet(spriteSheet);
    player = ss.grabImage(1, 1, 32, 32);
}

public void run() {
    init();
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();

    while (running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        if (delta >= 1){
            tick();
            updates++;
            delta--;
        }
        render();
        frames++;

        if (System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println(updates + " Ticks, Fps " + frames);
            updates = 0;
            frames = 0;
        }
    }
    stop();
}

private void render() {

    BufferStrategy bs = this.getBufferStrategy();

    if (bs == null){
        createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();

    g.setColor(new Color(81, 218, 221));
    g.drawRect(0, 0, getWidth(), getHeight());

    g.setColor(new Color(81, 218, 221));
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);

    g.setColor(new Color(255, 174, 80));
    g.drawImage(player, 100, 100, this);

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

}

private void tick() {

}

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

    frame = new JFrame("Game");

    frame.setMaximumSize(new Dimension((int)width, (int)height));
    frame.setMinimumSize(new Dimension((int)width, (int)height));
    frame.setPreferredSize(new Dimension((int)width, (int)height));

    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);

    frame.add(game);
    frame.pack();

    frame.setVisible(true);

    //device.setFullScreenWindow(frame);

    game.start();
}

private synchronized void start() {
    if (running)
        return;

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

private synchronized void stop() {
    if (!running)
        return;

    running = false;
    try {
        thread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    System.exit(1);
}
}

1 个答案:

答案 0 :(得分:0)

一些注意事项:

  1. BufferedImage的INT_ARGB的默认alpha为0(完全清除),因此除非更改像素的alpha值,否则您将看不到任何东西。

  2. drawRect仅绘制矩形的轮廓。使用fillRect绘制整个矩形。

  3. 据我了解,
  4. 绘制BufferedImages并不使用Graphics对象的当前颜色,因为它们主要定义为由不同颜色的像素组成的矩形。如果要更改其颜色,请尝试绘制到图像自己的Graphics对象。

  5. 您正好将两张图(图像和矩形)重叠,因此您只会看到一张。

我希望我终于有所帮助。