Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# How can i rotate a JPanel?

Anmol Mahatpurkar
Greenhorn
Posts: 10
I am working on a chess game. I made my chess board using a JPanel.

This JPanel consists 64 JLabels representing the 8x8 chess blocks.

I want to rotate this JPanel so that the entire chess board can be rotated according to the player's choice.

Please suggest me a way to do this.

Maneesh Godbole
Saloon Keeper
Posts: 11257
16
You can override paintComponent, cast the Graphics to Graphics2D and invoke the rotate(double theta) However, I suspect this would not suit your purpose as you will require to do all the painting yourself.

If you wish to rotate by 180 degrees (only), then the chess board (black white squares) do not change with rotation. The only change would be the chess pieces. i.e. piece[0][0] would need to be at piece[7][7] This will be easier to implement.

Rob Spoor
Sheriff
Posts: 20709
68
Maneesh Godbole wrote:You can override paintComponent, cast the Graphics to Graphics2D and invoke the rotate(double theta) However, I suspect this would not suit your purpose as you will require to do all the painting yourself.

Don't forget about any events. For a button to be rotated, not only must its painting be updated but click events etc need to be translated as well.

Richard Tookey
Bartender
Posts: 1166
17
[quote=Anmol Mahatpurkar
I want to rotate this JPanel so that the entire chess board can be rotated according to the player's choice.

Why rotate the JPanel. Surely it is just a case of changing the way the model is displayed on the JPanel.

Anmol Mahatpurkar
Greenhorn
Posts: 10
• 2
Thank you guys. I realized that changing the blocks from [0][0] to [7][7] was a much easier approach.

I was scared to implement it at first, because I thought it will affect my other algorithms. But it did not affect anything. This method worked perfectly fine. Thanks

Randall Twede
Ranch Hand
Posts: 4489
3
I was scared to implement it at first, because I thought it will affect my other algorithms.

i think everyone has felt that way. i know i have. what you do is copy everything into a new folder(version1.1 for example) and work on it there. that way you don't have to worry about screwing it up

Anmol Mahatpurkar
Greenhorn
Posts: 10
Yeah i will keep that in mind! Thanks

Piet Souris
Rancher
Posts: 1403
29
• 1
hi Anmol,

well, it CAN be done, a "rotating" panel, and I'll give you the code. I have just tested it, and it works a treat.
Keep in mind that when the panel has "rotated"180 degrees, you still need to exchange your icons in your
label[][] array, otherwise your chess pieces will be upside down. (This demo starts rotating when you click
in the panel, and resets when you click again).
But nevertheless, as a very nice eye-candy, here goes:

Greetings,
Piet

Campbell Ritchie
Sheriff
Posts: 50770
83
• 1
Piet Souris wrote:
Greetings,
Piet
What a nice app I would make one tiny change:-