Java Icon Rotation

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
LVL 1
TopaceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

objectsCommented:
you should do *all* your painting from the paint() call to your component.
The getGraphics() method should not be used.
0
TopaceAuthor Commented:
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
objectsCommented:
ImageIcon is fine, its a wrapper around an Image (see getImage()).
0
TopaceAuthor Commented:
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
objectsCommented:
> 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
objectsCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
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
objectsCommented:
Topace,

Try the code I posted above, it should work fine.
Let me know if you have any problems.
0
TopaceAuthor Commented:
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
VenabiliCommented:
11 days had passed after you asked for 5 more days....
0
TopaceAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.