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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.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:

    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!

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

Similar Threads

  1. Java Code Help on Draw method
    By RADical in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 24th, 2012, 07:17 PM
  2. Writing output from a void method to a file
    By TheWhopper858 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: December 9th, 2011, 05:21 AM
  3. Unable to draw a rectangle
    By n00b in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 07:32 AM
  4. Change the random draw line here for a mouseListener draw
    By Panda23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2011, 03:29 AM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM

Tags for this Thread