I am trying to properly size a JPanel so that it exactly fits a rendered 8 x 8 checkerboard. When I zoom in using a paint program, I notice two extra pixels added to both the width and height ...
This isn't too bad but when I surround this CENTER panel with other JPanels (NORTH, SOUTH, EAST, WEST in the JFrame using BorderLayout) the white gap is noticeable.
I get around the issue by subtracting 2 pixels for both width and height in my call to setPreferredSize
but if this anomaly is due to a graphics driver bug, this isn't a good solution.
Curious if there is a cleaner solution. The code is provided below using JDK 7 64-BIT Windows 7 ...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JavaExample {
private static final Color DARK_SQUARE_COLOR = new Color(205, 133, 63);
private static final Color LIGHT_SQUARE_COLOR = new Color(245, 222, 179);
private static final int SQUARE_WIDTH = 16;
private static final int SQUARE_HEIGHT = 16;
public JavaExample() {
JFrame frame = new JFrame();
frame.add( new JPanel() {
private static final long serialVersionUID = 1L;
{
setPreferredSize(new Dimension(SQUARE_WIDTH * 8, SQUARE_HEIGHT * 8));
}
protected void paintComponent( Graphics g ) {
super.paintComponent(g);
for(int row = 0; row < 8; row++) {
for(int col = 0; col < 8; col++) {
g.setColor(getSquareColor(row, col));
g.fillRect(col * SQUARE_WIDTH, row * SQUARE_HEIGHT, SQUARE_WIDTH, SQUARE_HEIGHT);
}
}
}
private Color getSquareColor(int row, int col) {
return (row + col) % 2 == 0 ? LIGHT_SQUARE_COLOR : DARK_SQUARE_COLOR;
}
});
frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible( true );
}
public static void main(String [] args) {
new JavaExample();
}
}