In the early seconds when I launched my program everything was fine but as time went by, my main Object(the animal(pokemon) in the screenshot) got this kind of strange bug where the object image sheared/flipped for no reason. I have this program that rotates 25 sub objects(gray blob in the screenshot) and a main object that can be move and rotated by the user.
Here's my screenshot:
started to launch
After 90 seconds(approx.)
When I removed the rotate code of sub objects, I didn't get the bug.
sub objects no rotation
Here's my transform code on my main and sub objects
I already commented the rotate code
I tried to change this to paintComponent and the problem still persisted.
is there any suggestion why this bug happens? I hope someone can help me on this as this bug is very challenging to fix for me because this bug randomly happens. I'll try to find a way to solve this and post my solution if I fix it.
I fixed it, sort of. I don't know if I fix this bug for good, but what I did to fix the bug was to create another BufferedImage for the main object. So, Sub objects group has their own bufferedimage and the main object has its own bufferedimage. As of now, my image didn't flip/shear, but I'm not really sure if the bug on my program is fixed because this bug can appear abrupt and random. I don't know the cause of this bug but base on my assumption, a graphic transformation may mess up if there's too much transform operation happening in the graphic.
I found another simple fix to this bug. Just reorder the codes in the paint() method
So, I draw the sub and main object first then draw the status information.
And this is the result:
I assume the reason why this bug happens is because paint() is slower than the timer. So, the timer updates the objects transform values in the program and paint() draws the transformation of objects. In my assumption, timer is faster than paint(), so, the timer might update the objects transform values while paint() is still drawing the objects. So, the transform values in Graphics2D gets scrambled and produces unexpected result.
Practice 'til your hands twitch.
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad: