java - 图像渲染在框架之外?

标签 java image swing concurrency jframe

我正在用 Java 开发一个游戏,在渲染时遇到问题(我相信是内容 Pane 的问题)。我有一个屏幕类,它将背景和所有 Sprite 绘制到图像上。然后框架使用 doubleBuffer 显示图像。由于某些奇怪的原因,图像渲染超出了帧的边缘。您可以在下面的链接中看到图像向左渲染 3 像素,向上方渲染 28 像素。有谁知道这可能是什么原因造成的? ![在此处输入图像描述][1]

http://imageshack.us/photo/my-images/41/weirdg.png/

public class Game extends JFrame implements Runnable{
private static final long serialVersionUID = 1L;

//graphics
public BufferStrategy buffy;
BufferedImage image;
Screen screen;

public Boolean running = false;
public Boolean playerTurn = false;

public InputManager input;
public Level level;
//JButton b;

public static final int HEIGHT = 452;
public static final int WIDTH = 768;

public Game() {
    super("GridWars");
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel drawPanel = new JPanel();
    drawPanel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    drawPanel.setLayout(null);
    drawPanel.setOpaque(false);
    //drawPanel.setLocation(50,50);

    setContentPane(drawPanel);
    setResizable(false);
    pack();
    setLocationRelativeTo(null);
    setVisible(true);
    requestFocus();
    createBufferStrategy(2);

    //b = new JButton("this sucks");

    //getContentPane().add(b);
    //b.setBounds(300, 300, 100, 50);


    buffy = getBufferStrategy();
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    screen = new Screen(WIDTH, HEIGHT);
    input = new InputManager(this);
    level = new Level(WIDTH, HEIGHT, input, this);
}

public void start() {
    running = true;
    new Thread(this).start();
}
public void setup(){

}
public void run() {
    final double TICKS = 30.0;
    final double UPDATE_INTERVAL_NS = 1000000000 / TICKS;
    double pastUpdateNS = System.nanoTime();

    int updateCount = 0;
    int frameCount = 0;

    final double FRAPS = 60.0; 
    final double RENDER_INTERVAL_NS = 1000000000 / FRAPS;
    double pastRenderNS = System.nanoTime();

    int pastSecondNS = (int) (pastUpdateNS/1000000000);

    while(running) {
        double nowNS = System.nanoTime();

        if(nowNS - pastUpdateNS >= UPDATE_INTERVAL_NS) {
            update();
            pastUpdateNS += UPDATE_INTERVAL_NS;
            updateCount++;
        }

        float interp = Math.min(1.0f, (float) ((nowNS - pastUpdateNS) / UPDATE_INTERVAL_NS) );
        render(interp);
        pastRenderNS += RENDER_INTERVAL_NS;
        frameCount++;

        int thisSecondNS = (int) (pastUpdateNS/1000000000);
        if (thisSecondNS > pastSecondNS) {
            //System.out.println("TICKS: "+updateCount+" | FRAPS: "+frameCount);
            updateCount = 0;
            frameCount = 0;
            pastSecondNS = thisSecondNS;
        }

        while( nowNS - pastRenderNS < RENDER_INTERVAL_NS && nowNS - pastUpdateNS < UPDATE_INTERVAL_NS) {
            try { Thread.sleep(1); } catch(Exception e) {};
            nowNS = System.nanoTime();
        }
    }
}

public void update() {
    input.update();
    level.update();
}

public void render(float interp) {
    level.render(screen, interp);

    image = screen.getImage();
    Graphics g = buffy.getDrawGraphics();
    g.drawImage(image, 0, 0, null, null);
    //b.repaint();
    g.dispose();
    buffy.show();
}

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

最佳答案

0,0坐标Graphics您从 buffy.getDrawGraphics(); 获得的对象正好在 JFrame 的左上角并且它忽略了框架装饰。

  1. UPD我忘记了一个明显的选项。 JFrame.getInsets()提供有关装饰的信息。您只需改变渲染即可。

  2. 您可以使框架不被装饰( setUndecorated(true) )并自己渲染/管理窗口控件。

  3. 或者,我认为这是更简单的方法,您会忘记在 JFrame 上直接渲染,地点Canvas并使用它来代替。 Canvas还包含createBufferStrategy方法,因此您需要进行一些简单的更改。

    JPanel drawPanel = new JPanel();
    drawPanel.setLayout(new BorderLayout());
    Canvas canvas = new Canvas();
    canvas.setPreferredSize(new Dimension(WIDTH, HEIGHT));
    drawPanel.add(canvas, BorderLayout.CENTER);
    
    // some code skipped
    
    canvas.setIgnoreRepaint(true); //important
    canvas.createBufferStrategy(2);
    
    buffy = canvas.getBufferStrategy();
    

我创建了简单的 demo几天前有类似的渲染以获得另一个答案。也许会有帮助。

关于java - 图像渲染在框架之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9610399/

相关文章:

java - 为什么 IntelliJ 将注释标题放在错误的位置……仅用于测试?

java - 将字节写入文件而不删除现有字节

java - ART(Android 运行时环境)中的 ArrayList 循环性能

image - React - map 中的动态图像

javascript - 图像未在 Blade View 中加载

javascript - 绘制到 Canvas 或用作图像源时缺少 SVG 图像和图案

java - 设置drawString,使(0,0)位于绘图区域内

java - JTextField 上的两个 KeyListener

java - 将值从 JPanel 类传递到 JFrame 类

java - 制作 JFrame 和 Observable 对象