This site requires JavaScript, please enable it in your browser!
Greenfoot back
SPower
SPower wrote ...

2013/8/22

Memory intensive rotating

SPower SPower

2013/8/22

#
Hi all, For a new game I need to rotate some image constantly, but using the stander rotate method from GreenfootImage gives a weird result after using it over and over every act method. So I used this code:
// beamOriginal is a BufferedImage
// beam is the image where the rotated beamOriginal is displayed
// beamGraphics is recieved by calling createGraphics() on the BufferedImage of beam.
beam.clear();
double rot = Math.toRadians(getRotation());
AffineTransform at = new AffineTransform();
at.translate(beamOriginal.getWidth()/2, beamOriginal.getHeight()/2);
at.rotate(rot);
at.translate(-beamOriginal.getWidth()/2, -beamOriginal.getHeight()/2);
beam.clear();
beamGraphics.drawImage(beamOriginal, at, null);
but, that seems to take up quite a bit of memory (I get the OutOfMemoryError earlier than before I used it). Can someone help me and/or give me some advice what to do? Thanks in advance!
K_O_P K_O_P

2013/8/22

#
Why don't you just use the 'setRotation' method?
SPower SPower

2013/8/22

#
Because the image needed to be rotated is not the image of an Actor, it's a seperate image.
davmac davmac

2013/8/22

#
Do you re-create beamGraphics each time you use it, and if so, are you calling dispose() on it? The fact that you have two images where you previously had one may also be an issue.
SPower SPower

2013/8/22

#
No, I just create beamGraphics at the initialization of the Actor. But maybe I have to call dispose() when the Actor gets removed from the world.
davmac davmac

2013/8/22

#
To be honest it shouldn't really matter, since the graphics object should get garbage collected and then automatically disposed anyway; I was just suggesting something to try.
SPower SPower

2013/8/22

#
Indeed, it didn't really help :(
K_O_P K_O_P

2013/8/22

#
What about creating a new GreenfootImage and the just rotate this?
SPower SPower

2013/8/22

#
That's what I did previously: it did let me play longer without giving the OutOfMemoryError, but I hoped this would take up less memory.
danpost danpost

2013/8/22

#
You could just create an Actor class for working with the image (not have the actor placed in the world, but just have a reference to the actor). As an example, refer to the 'getActorImage(Actor)' method in the PIP class of my PIP Actor Class scenario.
MatheMagician MatheMagician

2013/8/22

#
Take a look at this scenario. It shouldn't be laggy with one image.
SPower SPower

2013/8/22

#
@danpost I'm sorry to say it danpost, but that one is the most memory so far :(
You need to login to post a reply.