Canvas fillRect() not filling defined canvas

你说的曾经没有我的故事 提交于 2019-12-02 07:13:06

The example you've provide works fine. I modified it a little to demonstrate the use of getWidth/height

public void run() {
    while (gameRunning) {
        Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();

        graphics.setColor(Color.RED);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.setColor(Color.GREEN);
        int width = getWidth() - 50;
        int height = getHeight() - 50;
        graphics.fillRect(25, 25, width, height);
        graphics.setColor(Color.BLACK);
        FontMetrics fm = graphics.getFontMetrics();
        graphics.drawString("Frame Size: " + frame.getWidth() + "x" + frame.getHeight(), 0, fm.getAscent());
        graphics.drawString("Canvas Size: " + getWidth() + "x" + getHeight(), 0, fm.getAscent() + fm.getHeight());

        graphics.dispose();
        bufferStrategy.show();
        try {
            Thread.sleep(60);
        } catch (InterruptedException ex) {
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!