如何在awt中填满整个窗口

时间:2015-04-21 02:14:09

标签: java swing awt java-2d

我使用java awt渲染一个简单的矩形。我还有一个矩形用作窗口的背景。问题是,即使背景矩形设置为窗口的宽度和高度,它仍然不适合整个事物。我已尝试使用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();
   }

}

1 个答案:

答案 0 :(得分:2)

这在这里完美无瑕。仔细考虑差异,因为我已经忘记了改变了什么。

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();
   }
}