Hi i new to java2D api. I noticed the draw size is actually smaller than what you set in the properties of the components that get added to a container(Jframe). Is there a proper way of fixing this ??? Here is my code
import java.awt.*;
import javax.swing.JFrame;
import java.awt.image.*;
import java.util.concurrent.*;
public class Game extends Canvas implements Runnable {
public Game(){
super();
Thread gameThread = new Thread(this);
gameThread.start();
}
public void run(){
init();
gameLoop();
}
private void init(){
//set canvas properties
setSize(600,600);
setBackground(Color.WHITE);
setIgnoreRepaint(true);
// Acquiring the current Graphics
// Device and Graphics Configuration
GraphicsEnvironment graphicsEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice graphicsDev = graphicsEnv.getDefaultScreenDevice();
GraphicsConfiguration graphicsConf = graphicsDev.getDefaultConfiguration();
//create and set frame properties
JFrame frame = new JFrame(graphicsConf);
frame.setTitle("Test Game");
frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setIgnoreRepaint(true);
frame.getContentPane().add(this);
frame.validate();
frame.setVisible(true);
createBufferStrategy(2);
}
private void gameLoop(){
while(true){
gameUpdate();
gameRender();
//Thread.yield();
}
}
private void gameUpdate(){
}
private void gameRender(){
BufferStrategy strategy = getBufferStrategy();
// Render single frame
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics graphics = strategy.getDrawGraphics();
graphics.fillRect(0, 540, 10, 50);
graphics.fillRect(590, 50, 10, 50);
graphics.dispose();
// Repeat the rendering if the drawing
//buffer contents were restored
} while (strategy.contentsRestored());
// Display the buffer
strategy.show();
// Repeat the rendering if the drawing buffer was lost
} while (strategy.contentsLost());
}
public static void main(String[] args) {
new Game();
}
}