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

  • Thread starter Thread starter JoAuSc
  • Start date Start date
  • Tags Tags
    Java Polygon
Click For Summary
The discussion revolves around a programming issue in creating a platformer game where a Polygon representing a character is incorrectly translated on the screen. The user initially attempts to display the Polygon by translating its position using a method that inadvertently modifies the original Polygon object, causing it to move every time the screen updates. The key problem identified is that in Java, objects are referenced rather than copied, meaning that assigning one object to another just copies the reference, not the actual object. A solution was found by creating new arrays for the Polygon's coordinates and instantiating a new Polygon object, effectively separating it from the original. The conversation highlights the importance of understanding object references in Java and suggests using the .clone() method to create true copies of objects.
JoAuSc
Messages
197
Reaction score
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:
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?
 
Technology news on Phys.org
JoAuSc said:
Here's the problem: when I do the above, particle's Polygon gets translated too, rather than just poly2.

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
 
chroot said:
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

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:
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.
 
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
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 9 ·
Replies
9
Views
10K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
10
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
8K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
8
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K