Skip to main content

Drawing to canvas problem...

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
x0pk1n
Offline
Joined: 2012-07-12

Hello, so i am dabbling in making a game and i am a fairly messy worker and then i go back and tidy it up. So please ignore any stupid naming conventions or methods or anything. As I go back later and tidy up.

So basically I am trying to make a main menu, i just want to draw something to the screen and have it stay there. I am writing the graphics to a image atm and displaying the image as i tried just drawing to the canvas and it just flickered up onto the screen and then it went blank. I changed it to a runnable so i could put in sleep() to try and error hunt. I've removed all my error hunting code now. It turns out when i have a sleep() before or after the image is put on the screen for longer than 50ms the rectangle stays on screen and doesn't flicker away like 1ms after it was drawn.

I'm not sure what's going on. I am probably not that clear and I apologises.

	@Override
public void run() {
if(Constants.DEBUG_MODE){
System.out.println("Location: Menu_Graphics drawMenu()");
}
Menu_Logic_Graphics.g = Menu_Logic_Graphics.menuCanvas.getGraphics();
bufferImage = Menu_Logic_Graphics.menuCanvas.createImage(Constants.WIDTH * Constants.SCALE, Constants.HEIGHT * Constants.SCALE);
bufferGraphics = bufferImage.getGraphics();
bufferGraphics.clearRect(0, 0, Constants.WIDTH * Constants.SCALE, Constants.HEIGHT * Constants.SCALE);

//drawing items goes in here
bufferGraphics.drawRect(123, 37, 100, 100);

//print image at the end top the canvas so theres no flickering with drawing
try {
sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Menu_Logic_Graphics.g.drawImage(bufferImage,0,0,Constants.WIDTH * Constants.SCALE, Constants.HEIGHT * Constants.SCALE,Main.frame);
}