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

x
?
Solved

Java Icon Rotation

Posted on 2004-11-08
15
Medium Priority
?
2,014 Views
Last Modified: 2012-06-22
Hello Everyone.

I am currently desiging a progam for an Air Traffic Control simulation.  (As you probably already read from my last question...thanks a lot for everyone who helps me out - I really appreciate it!)

Anyway, I have an ImageIcon called planeGraphic, which represents the plane flying on the screen.  I also have the following code:

    public void refresh()
    {
        // draw the plane graphic
        Graphics g = image.getGraphics();
        viewBox.clearScreen();
        planeGraphic.paintIcon(viewBox, g, xPosition,500);
        viewBox.repaint();
       
    }

Now, what I want to do it cause the plane's icon to be rotated a set number of radians.  I thought I could do it this way:

public void rotate(int radians)
{
        Image pImage = planeGraphic.getImage();
        Graphics2D pGraph = (Graphics2D)pImage.getGraphics();
        pGraph.rotate(radians);    
}

But this gives me the following error:

java.lang.IllegalAccessError: getGraphics() only valid for images created with createImage(w, h)

Is there a good way of rotating images, so that I don't have to create 360 different static image files?

Thanks,

~ace
0
Comment
Question by:Topace
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:JK2429
ID: 12526455
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12527008
You need to set the rotation on the reference to a g2d in an appropriate paint or paintComponent method:

public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D)g;
      g2d.rotate(Math.PI / 2);
      g2d.drawImage(...........);
      g2d.dispose();
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 12528512
you should do *all* your painting from the paint() call to your component.
The getGraphics() method should not be used.
0
 
LVL 1

Author Comment

by:Topace
ID: 12528879
So should I be using an object of type Image, rather than type ImageIcon?  What class should get the paint() method you described?

~ace
0
 
LVL 92

Expert Comment

by:objects
ID: 12528888
ImageIcon is fine, its a wrapper around an Image (see getImage()).
0
 
LVL 1

Author Comment

by:Topace
ID: 12528891
For reference, here is the PlaneImage class I am using:

 private class PlaneImage
    {
        public ImageIcon icon;
       
        public PlaneImage()
        {
            icon = new ImageIcon("goldPlane.gif");
            xPosition = 0;
        }
       
        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            icon.paintIcon(c, g, x, y);
        }
       
        public void move(int x)
        {
            xPosition = xPosition + x;
        }
    }
}

~ace
0
 
LVL 92

Expert Comment

by:objects
ID: 12528902
> What class should get the paint() method you described?

on the component that you want to paint the image on, and use paintComponent() if its not a top level frame.
The example posted by JK2429 shows exactly what is needed.
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 12528923
using the code from JK2429's example you code would change to something like:

private class PlaneImage
    {
        public ImageIcon icon;
       
        public PlaneImage()
        {
            icon = new ImageIcon("goldPlane.gif");
            xPosition = 0;
        }
       
        public void paintIcon(Component c, Graphics g, int x, int y)
        {
           Graphics2D g2d = (Graphics2D)g;
           AffineTransform origXform = g2d.getTransform();
           AffineTransform newXform = (AffineTransform)(origXform.clone());
           //center of rotation is center of the panel
           int xRot = this.getIconWidth()/2;
           int yRot = this.getIconHeight()/2;
           newXform.rotate(Math.toRadians(currentAngle), xRot, yRot);
           g2d.setTransform(newXform);
           //draw image centered in panel
            icon.paintIcon(c, g2d, x, y);
            g2d.setTransform(origXform);
        }
       
        public void move(int x)
        {
            xPosition = xPosition + x;
        }
    }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12529094
You don't need to worry about the AffineTransform - the code i posted operates on that anyway. Nor do you need to restore the original transform as it's not being used (at least not in the last code example)
0
 
LVL 92

Expert Comment

by:objects
ID: 12529143
Topace,

Try the code I posted above, it should work fine.
Let me know if you have any problems.
0
 
LVL 1

Author Comment

by:Topace
ID: 12796174
I apologize, I have not yet had a chance to test the last possible solution posted to this question.  Would it be possible to leave the question open for another 5 days, and I will give points to the appropriate expert.  I believe the question is solved, but I just have been super busy lately.  

Thank you,

~ace
0
 
LVL 20

Expert Comment

by:Venabili
ID: 12875080
11 days had passed after you asked for 5 more days....
0
 
LVL 1

Author Comment

by:Topace
ID: 12876228
Agh.  Sorry about the delay.  I spaced it out again....

It's been a busy month at work.  ;)

Thanks for all your help, I was able to get it to work.

~ace
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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