• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help creating a method for rotate

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
Im trying to rotate a rectangle with a rotate method. Im not sure if I'm doing this correctly.
public void paint (Graphics g)
{ g.drawRect (x,y,length,width);}
how would i create a method to rotate 90 degrees?
public void rotate()
{ ? }
[ March 11, 2003: Message edited by: Jeremiah jjjjjjjjjjjjjjjj ]
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to create your own rotate method:
Say (x1,y1) is the upper-left corner of the original rectangle.
Width is its length along the x-axis, height is its length along the y-axis.
You probably want to compute what (x2, y2) was the dead center of the first rectangle (esimerkiksi: x2 = x1 + (width * .5)).
Assume that will also be your new rectangle's center, then backtrack to see what the coordinate's of the upper-left corner are now that newHeight=oldWidth and newWidth=oldHeight. This way you pivoted around the center of the original rectangle.
[ March 12, 2003: Message edited by: Elouise Kivineva ]
[ March 12, 2003: Message edited by: Elouise Kivineva ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeremiah, welcome to JavaRanch. Would you please change your displayed name to conform to the JavaRanch Naming Policy
Thanks
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To rotate a general rectangle, you should translate the rectangle so that its centre is on the origin. Then rotate each corner of the rectange around the origin using a rotation matrix (to be found in any computer graphics text/tutorial). Then translate the centre of the rectangle back to its original postition. Finally, draw the sides and fill appropriately. Of course, all this can be optimised in the case of a rotation of 90 degrees because the sine of 90 degrees is 1.0, and cosine of 90 degrees is 0.0. Do the maths first to simplify the code you have to write.
-Barry
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, if you don't want to reinvent the wheel you can use the methods of the java.awt.Graphics2D class.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic