Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i rotate a JPanel?  RSS feed

 
Anmol Mahatpurkar
Greenhorn
Posts: 10
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Chrome Eclipse IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 4683
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah i will keep that in mind! Thanks
 
Piet Souris
Rancher
Posts: 1943
66
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 55333
157
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:
Greetings,
Piet
What a nice app I would make one tiny change:-
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!