• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java Graphics

CefiroZ

Limp Gawd
Joined
Jan 17, 2004
Messages
218
I'm not very well versed in Java graphics and I am having trouble sorting through all these Graphics2D, BufferedImage, etc. classes.

I currently have a program that is a little sluggish on the animation front. I am inheriting from JPanel so double-buffering is taken care of, so no flicker, but the motion is not smooth. So, I have decided that reducing the amount of drawing may fix this. The best idea I came up with is to draw the static part of the JPanel one time. The JPanel is basically a grid that an object slides across. I want to draw this grid ahead of time and then draw the object and its path dynamically

So, in paintComponent I want to call something to draw the previously-drawn-to-something (BufferedImage? Graphics2D?) static grid and then go on with drawing my object and its path over top of this. Is this possible and, if so, how do I do so? Or are there better solutions?

Thanks guys!
 
Drawing a grid should not be very complicated, and therefore I doubt it is the source of your problem. Do you have any mechanism to keep the frame rate constant? If not take a look at this run method.
 
I have to agree with wookie, I'm currently about 90% through a game of Bomberman in java and it's still blazingly fast regardless of all the drawing it's doing. Are you drawing everything to an offscreen image file and simply drawing that image on the panel in the Paint() or Paint Component() method? That is how I've done it and I've had no problems in terms of smoothness or speed.
 
chomsky said:
Are you drawing everything to an offscreen image file and simply drawing that image on the panel in the Paint() or Paint Component() method?

I used to inherit from Canvas and I was doing double-buffering with an off screen image. When I switched to JPanel, from what I've read, the Graphics object 'g' that is received by paintComponent is already an offscreen image and the double-buffering is handled automatically. So, I am writing right to 'g'. Am I wrong about this?
 
I implemented the run method from the link to get a constant fps, and it sort-of worked =)

The puck (the actual animation part) looks good: runs across the screen nice and smooth. So that part is now fixed. The problem is that the grid that the program is drawing flickers behind the puck, which it never did before.
 
I've always done the double buffering manually, and in the end, it's very quick to convert your code to do so and it may even be more elegant of a coding solution, so that is what I would recommend. Just make a global Image object, have a method that on every update will get that Image's graphics object, and draw to that image. Then after that method is called call your paint() method and give it the sole task of checking to see if the image is null and if not get the panel's graphics object (the g that's passed in as you previously mentioned) and g.drawImage(imageName, 0,0,null).
I have examples lying around if you need them, but a fantastic resource is here:
http://fivedots.coe.psu.ac.th/~ad/jg/

He always manually double buffers and as someone who spent an entire chapter basically explaining the different methods you could use to get the system time, he's pretty thorough and probably doing it for a reason.
 
Okay guys, thank you so much for your help so far. The good news is that I have managed to get the drawing to look wonderful using that very helpful link (thanks chomsky!).

This has led me to one more problem though. Since my drawing functions are now renderScene (draws to offscreen) and paintScreen (displays offscreen to screen), they do not automatically get called sometimes. For example, upon start up the program leaves the drawing area blank. Another example is if you run the program, pause it, maximize a different window, and then open the simulation back up you have a blank drawing area once again. This is seems to be because Java doesn't know what function to call. I've tried having paintComponent() and update() call my functions but that didn't seem to help. Any ideas?
 
Back
Top