如何在awt中填充整个window

How to fill the whole window in awt

我正在使用 java awt 渲染一个简单的矩形。我还有一个矩形用作 window 的背景。问题是,即使背景矩形设置为 window 的宽度和高度,它仍然不适合整个对象。我已经尝试 Google 这个,但发现结果与我的需求无关。这是什么原因造成的?

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.image.BufferStrategy;
import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Game implements Runnable{

   final int WIDTH = 640;
   final int HEIGHT = 480;

   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;

   boolean running = false;

   public Game(){
      frame = new JFrame("Prototyping");

      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);


      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);

      panel.add(canvas);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);

      canvas.createBufferStrategy(2); 
      bufferStrategy = canvas.getBufferStrategy();

      canvas.requestFocus();
   }



   public void run(){

      running = true;
      while(running)         
         render();
   }

   private void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }

   protected void update(){

   }

   protected void render(Graphics2D g){
      g.setColor(Color.GRAY);
      g.fillRect(0, 0, WIDTH, HEIGHT);

      g.setColor(Color.BLUE);
      g.fillRect(100, 0, 200, 200);
   }

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

}

这在这里完美无缺。仔细看看有什么不同,因为我忘了改变了什么。

import java.awt.*;
import java.awt.image.BufferStrategy;
import javax.swing.*;

public class Game implements Runnable{

   final int WIDTH = 640;
   final int HEIGHT = 480;

   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;

   boolean running = false;

   public Game(){
      frame = new JFrame("Prototyping");

      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(new GridLayout());


      canvas = new Canvas();
      //canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);

      panel.add(canvas);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setResizable(false);
      frame.setVisible(true);

      canvas.createBufferStrategy(2); 
      bufferStrategy = canvas.getBufferStrategy();

      canvas.requestFocus();
   }

   public void run(){
      running = true;
      while(running)         
         render();
   }

   private void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      render(g);
      g.dispose();
      bufferStrategy.show();
   }

   protected void render(Graphics2D g){
      g.setColor(Color.GRAY);
      g.fillRect(0, 0, WIDTH, HEIGHT);

      g.setColor(Color.BLUE);
      g.fillRect(100, 0, 200, 200);
   }

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