Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Rotating an ImageIcon in Graphics2D

843804Apr 10 2003 — edited Jan 18 2005
I am still a beginner where Java programming is concerned. In order to improve my skills I decided to re-invent the wheel and write a pack (deck) of cards Class based around Swing. This in itself is not difficult however I have come across a problem. When displaying the suit 'icons' on a card the lower ones need to be inverted (rotated through 180 degrees). The simple and obvious solution would be to have a separate set of images which show the inverted suit 'icon'. However I believe it would be better to do this in code and therefore turned to trying to rotate the image using the Graphics2D Class but cannot get this to work. Here is the method from my Card Class that does (or rather doesn't) do this.

/** Paint the card acording to its suit and face value. */
public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
g2.drawImage(suitGraphic.getImage(),5,5,null);
g2.drawImage(suitGraphic.getImage(),65,5,null);
g2.drawImage(suitGraphic.getImage(),30,55,null);
g2.rotate(Math.toRadians(180));
g2.drawImage(suitGraphic.getImage(),5,105,null);
g2.drawImage(suitGraphic.getImage(),65,105,null);
}

The above generates the card 5 and suitGraphic is defined elsewhere as (for example):-
suitGraphic = new ImageIcon("spade.gif");

N.B. The above code compiles fine but when I run the test program the bottom two inverted (rotated) spade images do not appear. This is just a test at this stage ultimately there will be different code for each card face value.

I would be grateful if some one could point me towards a solution.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 15 2005
Added on Apr 10 2003
5 comments
173 views