[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-28
3
Medium Priority
?
1,692 Views
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);
            at.translate(x
                        -(((Actor)Sector42Main.playeractors.get(FOCUS_ID)).x)
                        +((Stage.WIDTH/2)/Sector42Main.UNIVSCALE)
                        - (((Actor)Sector42Main.playeractors.get(FOCUS_ID)).getWidth()/2) ,
 
                        y
                        -(((Actor)Sector42Main.playeractors.get(FOCUS_ID)).y)
                        + ((Stage.HEIGHT/2)/Sector42Main.UNIVSCALE)
                        - (((Actor)Sector42Main.playeractors.get(FOCUS_ID)).getHeight()/2));
            at.rotate(r,getWidth()/2,getHeight()/2);
      at.scale(xscale, yscale);
            g.drawImage( spriteCache.getSprite(spriteNames[currentFrame]),at, stage);
      }



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

Accepted Solution

by:
nullmind earned 200 total points
ID: 16783149
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: http://www.jguru.com/faq/view.jsp?EID=53328

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));
            instance.setVisible(true);
      }
      
      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();
            
            TestPanel(){
                  repaintTimer = new Timer(1000/60, this);
                  repaintTimer.start();
            }
            
            public void paintComponent(Graphics lg){
                  // Cast the legacy graphics to the newer graphics API
                  Graphics2D g = (Graphics2D)lg;
                  
                  transform.setToIdentity();
                  transform.translate(getWidth() * 0.5f, getHeight() * 0.5f);
                  transform.rotate(
                        Math.toRadians(angle),
                        image.getImage().getWidth(null) * 0.5f,
                        image.getImage().getHeight(null) * 0.5f
                  );
                  
                  
                  
                  g.setColor(Color.black);
                  g.fillRect(0, 0, getWidth(), getHeight());
                  
                  g.drawImage(image.getImage(), transform, null);
                  
                  angle++;
            }
            
            public void actionPerformed(ActionEvent event){
                  if(event.getSource() == repaintTimer){
                        repaint();
                  }
            }
      }
}
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month18 days, 14 hours left to enroll

834 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