Canvas fillRect() not filling defined canvas

2019-03-01 10:03发布

问题:

I am extending a Canvas and adding it to a JFrame. I understand that AWT and Swing should not be mixed and that drawing on JPanel is preferred but i'm trying to follow a game engine tutorial and i'd like to stick to it since I got it working so far. The Canvas has minimumSize, maximumSize, and prefferedSize set to the dimensions of 550, 400. When I do a draw call graphics.draw(0,0,550,400) it doesn't fill the entire screen as it should. I changed the draw call to graphics.draw(0,0,560,410) essentially adding 10px to it and it filled the entire screen. Whats wrong?

BTW: graphics.draw(10,10,550,400 draws the rectangle starting at the corner exactly so I don't think the JFrame is the issue.

The main call inside Launcher class

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            game.setMinimumSize(DIMENSIONS);
            game.setMaximumSize(DIMENSIONS);
            game.setPreferredSize(DIMENSIONS);

            game.frame = new JFrame(NAME);
            game.frame.setLayout(new BorderLayout());
            game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            game.frame.add(game, BorderLayout.CENTER);
            game.frame.pack();

            game.frame.setResizable(false);
            game.frame.setLocationRelativeTo(null);
            game.frame.setVisible(true);

            Logger.log(TAG, "Game starting");
            game.start();
        }
    });
}

The draw call, Launcher.HEIGHT and WIDTH are 550,400

    public void draw(float deltaTime, Graphics2D graphics) {
    graphics.setColor(Color.BLACK);
    graphics.fillRect(0, 0, 550, 400);
    graphics.setColor(Color.DARK_GRAY);
    graphics.fillRect(0, 0, 150, 40);
    graphics.fillRect(0, Launcher.HEIGHT - 100, Launcher.WIDTH, 100);
    graphics.setColor(Color.LIGHT_GRAY);
    graphics.fillRect(125, Launcher.HEIGHT - 100, 100, 350);
}

The extended Canvas class

public abstract class Game extends Canvas implements Runnable {
private static final String TAG = "Game";

public JFrame frame;
public JPanel panel;
public boolean isApplet = false;

private boolean gameRunning = false;

BufferStrategy bufferStrategy;

private Screen screen;
private Thread renderThread;

public synchronized void start() {
    // Canvas
    setBounds(0, 0, 550, 400);
    setIgnoreRepaint(true);
    createBufferStrategy(2);
    bufferStrategy = getBufferStrategy();

    // Screen, Handlers, ETC
    screen = getStartScreen();

    // Threads
    renderThread = new Thread(this, Launcher.NAME + "_main");
    renderThread.start();
    gameRunning = true;
}

@Override
public void run() {
    long startTime = System.nanoTime();

    while (gameRunning) {
        float deltaTime = (System.nanoTime() - startTime) / 1000000000.0f;
        startTime = System.nanoTime();

        screen.update(deltaTime);

        Graphics2D graphics = (Graphics2D) bufferStrategy.getDrawGraphics();

        screen.draw(deltaTime, graphics);

        graphics.dispose();
        bufferStrategy.show();

        // FPS Counter

        // FPS Capper
    }
}
}

Reqested SSCCE

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {

    public static final int WIDTH = 550;
    public static final int HEIGHT = 400;
    public static final Dimension DIMENSIONS = new Dimension(WIDTH, HEIGHT);

    public static final String NAME = "SSCCE";

    public boolean gameRunning = false;

    public JFrame frame;
    public BufferStrategy bufferStrategy;

    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Game game = new Game();
                game.setMinimumSize(DIMENSIONS);
                game.setMaximumSize(DIMENSIONS);
                game.setPreferredSize(DIMENSIONS);

                game.frame = new JFrame(NAME);
                game.frame.setLayout(new BorderLayout());
                game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                game.frame.add(game, BorderLayout.CENTER);
                game.frame.pack();

                game.frame.setResizable(false);
                game.frame.setLocationRelativeTo(null);
                game.frame.setVisible(true);

                System.out.println("Game started");
                game.start();
            }
        });
    }

    public synchronized void start() {
        setSize(550, 400);
        setBounds(0, 0, 550, 400);
        setIgnoreRepaint(true);
        createBufferStrategy(2);
        bufferStrategy = getBufferStrategy();

        // Threads
        Thread renderThread = new Thread(this, NAME + "_main");
        renderThread.start();
        gameRunning = true;
    }

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

            graphics.setColor(Color.BLACK);
            graphics.fillRect(0, 0, WIDTH, HEIGHT);

            graphics.dispose();
            bufferStrategy.show();
        }
    }

}

As for the Java 2D game tutorial I am following it is a web series. Nevertheless, the link is the following click here. I modified the code quite a lot.

Modified for MadProgrammer

    import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class Game extends Canvas implements Runnable {

    public static final int WIDTH = 550;
    public static final int HEIGHT = 400;
    public static final Dimension DIMENSIONS = new Dimension(WIDTH, HEIGHT);

    public static final String NAME = "SSCCE";

    public boolean gameRunning = false;

    public JFrame frame;
    public BufferStrategy bufferStrategy;

    public static void main (String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Game game = new Game();
                game.setMinimumSize(DIMENSIONS);
                game.setMaximumSize(DIMENSIONS);
                game.setPreferredSize(DIMENSIONS);

                game.frame = new JFrame(NAME);
                game.frame.setLayout(new BorderLayout());
                game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                game.frame.add(game, BorderLayout.CENTER);
                game.frame.pack();

                game.frame.setResizable(false);
                game.frame.setLocationRelativeTo(null);
                game.frame.setVisible(true);

                System.out.println("Game started");
                game.start();
            }
        });
    }

    public synchronized void start() {
        setSize(550, 400);
        setBounds(0, 0, 550, 400);
        setIgnoreRepaint(true);
        createBufferStrategy(2);
        bufferStrategy = getBufferStrategy();

        // Threads
        Thread renderThread = new Thread(this, NAME + "_main");
        renderThread.start();
        gameRunning = true;
    }

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

            graphics.setColor(Color.RED);
            // Using getWidth()
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.setColor(Color.GREEN);
            // Using WIDTH which was used to set the size of the canvas
            graphics.fillRect(5, 5, WIDTH, HEIGHT);

            graphics.dispose();
            bufferStrategy.show();

            try {
                Thread.sleep(60);
            } catch (InterruptedException ex) {
            }
        }
    }

}

回答1:

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) {
        }
    }
}