# Writing a method to draw a rotatable rectangle using Graphics2D

• May 17th, 2012, 04:38 PM
johnpooley3
Writing a method to draw a rotatable rectangle using Graphics2D
I am trying to create a method (for robocode) that draws a rotatable rectangle with the (X,Y) coordinates referring to the center of rotation. I have created a method stub, but cannot figure out the actual math for the problem.

Code Java:

```//draws a rectangle centered on (x, y) with h heading in degrees, 0 is up, 90 is right. Sides are of length l and of height w public static void drawRotatedRect(Graphics2D g, double x, double y, double h, double l, double w, Color c) { Color returnColor = g.getColor(); g.setColor(c);   //draw four sides using Graphics.drawLine()?   g.setColor(returnColor); }```

What mathy shenanigans can I use to calculate the XY's of the four corners, so I can pass them into Graphics.drawLine()? Thanks in advance
• May 17th, 2012, 05:32 PM
copeg
Re: Writing a method to draw a rotatable rectangle using Graphics2D
Unless you are required to write the math yourself, an easier way would be to use the methods available in the Graphics2D or AffineTransform class - namely rotate and translate (its always a good idea to get the AffineTransform prior to setting these methods, and reset it afterwards to avoid affecting the transform of future drawing)
• May 17th, 2012, 11:39 PM
johnpooley3
Re: Writing a method to draw a rotatable rectangle using Graphics2D
Is this the correct way to implement that class?

Code Java:

```public static void drawRotatedRect(Graphics2D g, double x, double y, double heading, double h, double w) { AffineTransform returnTransform = g.getTransform(); g.setTransform(AffineTransform.getRotateInstance(heading, x, y)); g.drawRect((int) (x-w/2), (int) (y-h/2), (int) w, (int) h); g.setTransform(returnTransform); }```

Because when I run this method (using robocode) it gives me this stack trace and draws a rectangle without rotation:

Code :

```java.lang.ArrayIndexOutOfBoundsException: 63 at net.sf.robocode.robotpaint.Graphics2DSerialized.readMethod(Graphics2DSerialized.java:2172) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1418) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) java.lang.ArrayIndexOutOfBoundsException: -16 at net.sf.robocode.robotpaint.Graphics2DSerialized.readMethod(Graphics2DSerialized.java:2172) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1418) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) java.lang.ArrayIndexOutOfBoundsException: -67 at net.sf.robocode.robotpaint.Graphics2DSerialized.readMethod(Graphics2DSerialized.java:2172) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1418) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) java.nio.BufferUnderflowException at java.nio.Buffer.nextGetIndex(Buffer.java:478) at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:336) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTranslate_int(Graphics2DSerialized.java:1619) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1422) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) java.nio.BufferUnderflowException at java.nio.Buffer.nextGetIndex(Buffer.java:478) at java.nio.HeapByteBuffer.getInt(HeapByteBuffer.java:336) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTranslate_int(Graphics2DSerialized.java:1619) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1422) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) java.lang.ArrayIndexOutOfBoundsException: -36 at net.sf.robocode.robotpaint.Graphics2DSerialized.readMethod(Graphics2DSerialized.java:2172) at net.sf.robocode.robotpaint.Graphics2DSerialized.processQueuedCall(Graphics2DSerialized.java:1418) at net.sf.robocode.robotpaint.Graphics2DSerialized.processTo(Graphics2DSerialized.java:1393) at net.sf.robocode.ui.battleview.BattleView.drawRobotPaint(BattleView.java:511) at net.sf.robocode.ui.battleview.BattleView.drawBattle(BattleView.java:325) at net.sf.robocode.ui.battleview.BattleView.update(BattleView.java:180) at net.sf.robocode.ui.battleview.BattleView.access\$500(BattleView.java:55) at net.sf.robocode.ui.battleview.BattleView\$BattleObserver.onTurnEnded(BattleView.java:683) at net.sf.robocode.battle.events.BattleEventDispatcher.onTurnEnded(BattleEventDispatcher.java:131) at net.sf.robocode.ui.battle.AwtBattleAdaptor.awtOnTurnEnded(AwtBattleAdaptor.java:128) at net.sf.robocode.ui.battle.AwtBattleAdaptor.access\$200(AwtBattleAdaptor.java:35) at net.sf.robocode.ui.battle.AwtBattleAdaptor\$BattleObserver\$2.run(AwtBattleAdaptor.java:208) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:682) at java.awt.EventQueue.access\$000(EventQueue.java:85) at java.awt.EventQueue\$1.run(EventQueue.java:643) at java.awt.EventQueue\$1.run(EventQueue.java:641) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext\$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:652) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)```

Thanks again!
• May 18th, 2012, 09:28 AM
copeg
Re: Writing a method to draw a rotatable rectangle using Graphics2D
The stack trace has nothing to do with the drawRotatedRect method you posted. See your drawRobotPaint...line 511 in the BattleView class to be exact