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

  • Context: Java 
  • Thread starter Thread starter JoAuSc
  • Start date Start date
  • Tags Tags
    Java Polygon
Click For Summary

Discussion Overview

The discussion revolves around a programming issue in Java related to translating a Polygon object in a game development context. Participants explore the behavior of object references versus primitive types, particularly in the context of updating graphical representations in a platformer game.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested

Main Points Raised

  • One participant describes the issue of a Polygon being translated unexpectedly due to object reference behavior in Java.
  • Another participant points out that the code assigns poly2 to refer to the same Polygon object as particle, leading to both being affected by translations.
  • A participant explains their initial misunderstanding, comparing object behavior to primitive types, and shares a solution involving creating a new Polygon from the original coordinates.
  • Further clarification is provided about Java's handling of object references versus primitive types, emphasizing that copying a reference does not create a new object.
  • It is suggested that using the .clone() method could provide a solution by creating a distinct copy of the Polygon object.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the problem related to object references in Java, but there is no consensus on the best approach to resolve the issue, as different solutions are proposed.

Contextual Notes

The discussion highlights the distinction between object references and primitive types in Java, which may be relevant for understanding similar issues in object-oriented programming.

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
 

Similar threads

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