==Java, Graphics 2d, performance really drops when rotating an image using affine transform(please help a noob )==

Posted on 2006-05-28
Last Modified: 2012-05-05
Hey, i'm making a spaceship game, and i use buffered .gif that are loaded before they are used.
(The images are only loaded once before anyone says that :D )

However, when i have a reasonably sized image rotating in real time, such as a 533 by 182, at normal scale my fps drops really low, like into the mid teens, and i'm not sure why.
This only seems to happen when the iamge rotates, the fps stays fine when the image is translating.
I use Affine Transforms to do my rotating.
And i'm already using the createCompatibleImage GraphicsConfiguration to help increase performance, but i still get this problem.

I've also noticed if i zoom in realy close (using scaling ffrom affine transform) the same thing happens if i rotate, and my fps drops really low as well.
and the problem with the fps dropping when rotating is alot reduced if i zoom out alot as well.

Anyone got any idea how i can imrpove performance.

I'm buffering images as so:

      public BufferedImage createCompatible(int width, int height, int transparency) {
            GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration() ;
            BufferedImage compatible = gc.createCompatibleImage(width,height,transparency);
            return compatible;

and the following code block is the one i use to render the spaceships (Actors)

      public void paint(Graphics2D g){
                  AffineTransform at = new AffineTransform();
            at.scale(Sector42Main.UNIVSCALE, Sector42Main.UNIVSCALE);
                        - (((Actor)Sector42Main.playeractors.get(FOCUS_ID)).getWidth()/2) ,
                        + ((Stage.HEIGHT/2)/Sector42Main.UNIVSCALE)
                        - (((Actor)Sector42Main.playeractors.get(FOCUS_ID)).getHeight()/2));
      at.scale(xscale, yscale);
            g.drawImage( spriteCache.getSprite(spriteNames[currentFrame]),at, stage);

any help would be really welcome.
Question by:FatNewbie
    1 Comment
    LVL 1

    Accepted Solution

    I'm not quite sure what all the fuss about it being a BufferedImage is (considering I use the Image interface and ImageIcon class without any issues), but you may want to try loading the Gif into the BufferedImage class like this:

    The reason I use the ImageIcon class is because it takes care of loading the media over the network, etc. The AWT loading methods and such will simply display a blank image until it's loaded, which might not be what you want. Here is some sample code, how well does it run?

    import java.awt.*;
    import java.awt.geom.*;
    import java.awt.event.*;
    import javax.swing.*;

     *      Shows a simple rotating image
    public class Sandbox extends JFrame{
          /** Entry point for example */
          public static void main(String[] args){
                // Create and show the JFrame
                Sandbox instance = new Sandbox();
                instance.setSize(new Dimension(500, 500));
          ImageIcon image = new ImageIcon("C:\\google.gif");
          double angle = 0;
           *      Creates an instance of the Sandbox JFrame that displays
           *      a simple rotating Gif image.
          public Sandbox(){
                setContentPane(new TestPanel());
          class TestPanel extends JComponent implements ActionListener{
                Timer repaintTimer;
                AffineTransform transform = new AffineTransform();
                      repaintTimer = new Timer(1000/60, this);
                public void paintComponent(Graphics lg){
                      // Cast the legacy graphics to the newer graphics API
                      Graphics2D g = (Graphics2D)lg;
                      transform.translate(getWidth() * 0.5f, getHeight() * 0.5f);
                            image.getImage().getWidth(null) * 0.5f,
                            image.getImage().getHeight(null) * 0.5f
                      g.fillRect(0, 0, getWidth(), getHeight());
                      g.drawImage(image.getImage(), transform, null);
                public void actionPerformed(ActionEvent event){
                      if(event.getSource() == repaintTimer){

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now