2

I'm using java awt to render a simple rectangle. I also have a rectangle to be used as the background of the window. The thing is, even though the background rectangle is set to the window's width and height, it still doesn't fit the whole thing. I've tried to Google this, but found results irrelevant to my needs. What's causing this?

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

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    *"..even though the background rectangle is set to the window's width and height, it still doesn't fit the whole thing."* Wait ..what? The grey rectangle fills the UI (here). The blue rectangle is deliberately drawn smaller than the UI. As an aside: Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Apr 21 '15 at 04:50
  • @AndrewThompson Appreciate the links. Though the problem I'm seeing is the white space on the side: http://i.imgur.com/fcn7Ggx.gif . I assume this has to do with the null layout? – Positive x Squared Apr 21 '15 at 20:38
  • Oh my bad.. I think I might have added a layout before looking at the result. Change `panel.setLayout(null);` to `panel.setLayout(new GridLayout());` and see if it comes good.. – Andrew Thompson Apr 21 '15 at 20:53
  • @AndrewThompson that didn't do it either. – Positive x Squared Apr 21 '15 at 21:36

1 Answers1

2

This works flawlessly here. Go through it carefully for differences, since I've forgotten what is changed.

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();
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433