画布的底部和右边区域没有渲染

2022-01-29 09:22:17 标签 javaswingcanvasjframe

如果我用JFrame创建一个窗口底部有几行像素右边有几列在窗口外面所以它们不会显示出来。例如,当我创建一个800x600的窗口时,窗口将只显示左上角786x563区域的内容。被切断的数量似乎也随着窗口的大小而变化。到目前为止,我只是手动调整了东西的大小和位置。目前在我的代码是这样设置的:

import java.awt.*; //Canvas and Dimension
import java.util.*;
import javax.swing.JFrame;
import java.awt.image.BufferStrategy;
import java.awt.event.*;
public class Main extends Canvas implements Runnable {
    static final int WIDTH = 800, HEIGHT = WIDTH/4*3; //800, 600
    private Thread thread;
    private boolean running = false;
    
    public Main() { //constructor
        new Window(WIDTH, HEIGHT, "Game", this);
        requestFocus();
    }
    
    public synchronized void start() {
        thread = new Thread(this);
        thread.start();
        running = true;
    }
    
    public synchronized void stop() {
        try {
            thread.join();
            running = false;
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    
    public void run() {
        long lastTime = System.nanoTime();
        double amountOfTicks = 60.0;
        double ns = 1000000000 /amountOfTicks;
        double delta = 0;
        long timer = System.currentTimeMillis();
        int frames =0;
        while(running) {
            long now = System.nanoTime();
            delta+= (now - lastTime) / ns;
            lastTime = now;
            while(delta>=1) {
                tick();
                delta--;
            }
            if(running)
                render();
            frames++;
            if(System.currentTimeMillis() - timer > 1000) {
                timer+=1000;
                frames = 0;
            }
        }
        stop();
    }
    
    public void render() {
        BufferStrategy bs = this.getBufferStrategy();
        if(bs==null) {
            this.createBufferStrategy(3);
            return; 
        }
        Graphics g = bs.getDrawGraphics();
        //creates a line that should be 38 units above the bottom but instead is right along the bottom
        g.setColor(Color.GREEN);
        g.fillRect(1, 562, 30, 1);
        
        g.dispose();
        bs.show();
        try{Thread.sleep(5);} catch(Exception e) {}
    }
    
    public void tick() {
        
    }
    
    public static void main(String args[]) {
        new Main();
    }
}
class Window extends Canvas {
    public Window(int w, int h, String t, Main game) {
        JFrame frame = new JFrame(t);
        frame.setPreferredSize(new Dimension(w,h));
        frame.setMinimumSize(new Dimension(w,h));
        frame.setMaximumSize(new Dimension(w,h));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(game);
        frame.setLocationRelativeTo(null);
        frame.pack();
        frame.setVisible(true);
        game.start();
    }
}

如果我没有包含足够的代码来诊断问题,请告诉我,以便我可以把它的其余部分。

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码