Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java question: how can I stop my polygon from translating?

  1. May 8, 2007 #1
    I'm trying to create a computer game (a platformer); currently I'm just trying to display a Polygon on the screen.

    I have this class Charac, which contains a Polygon which defines its shape. If you display the bare Polygon, it'll put the shape at 0,0 on the screen. In order to move the Polygon to the right part of the screen, I have code which looks something like this:

    Code (Text):

    public void updateGraphics() {

       // ...

       /* xPosition and yPosition are int variables which are where I want the
        * Polygon to be displayed. particle is the name of the character/shape
        * you move around the screen. drawPanel is this separate class which
        * successfully displays whatever polygons you tell it to. */

       Polygon poly2 = particle.getPolygon();
       poly2.translate( xPosition, yPosition );

       drawPanel.setPolygon( poly2 );

       // ...
    }
     
    Here's the problem: when I do the above, particle's Polygon gets translated too, rather than just poly2. This means every time I update the screen, the Polygon keeps moving in the (xPosition,yPosition) direction.

    I remember reading a similar problem on these forums, and it involved clones or something. Can anyone give me a simple solution to translating poly2 while leaving particle.getPolygon() alone?
     
  2. jcsd
  3. May 8, 2007 #2

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Well, your code explicitly states that poly2 should refer to the same object as the particle's polygon. Why you chose this, I don't know.

    - Warren
     
  4. May 8, 2007 #3
    I chose to do that because I didn't know any better. I assumed that it'd be similar to dealing with integers, for example, where

    Code (Text):

    a = 2;
    b = a;
    a++;
     
    does not mean that b increments too.


    Btw, I just solved this problem by creating arrays of xpoints and ypoints, copying the coordinates from particle.getPolygon(), then making poly2 a new Polygon with those coordinates.
     
  5. May 8, 2007 #4

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Ah, I see. I thought you knew what you code meant, but there was perhaps a bug elsewhere.

    Java considers native types (int, char, boolean, etc.) differently than objects. When you copy integer variables into other integer variables, you literally make a copy of the data.

    However, Java treats objects differently. Objects are only pointed to by references, and copying references does not copy the actual object. When you assigned poly2 in your code, you were just making a copy of the reference returned by getPolygon().

    You can use the .clone() method of any object in Java to actually make a copy of an object. In other words, particle.getPolygon().clone() will make a real copy of the underlying Polygon object, and you'll have two distinct objects.

    - Warren
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?