Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Writing a method to draw a rotatable rectangle using Graphics2D

1. ## 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.

```//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

2. ## 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)

3. ## The Following User Says Thank You to copeg For This Useful Post:

johnpooley3 (May 17th, 2012)

4. ## Re: Writing a method to draw a rotatable rectangle using Graphics2D

Is this the correct way to implement that class?

```public static void drawRotatedRect(Graphics2D g, double x, double y, double heading, double h, double w)
{
AffineTransform returnTransform = g.getTransform();
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:

```java.lang.ArrayIndexOutOfBoundsException: 63
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 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)
java.lang.ArrayIndexOutOfBoundsException: -16
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 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)
java.lang.ArrayIndexOutOfBoundsException: -67
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 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)
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 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)
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 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)
java.lang.ArrayIndexOutOfBoundsException: -36
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 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)