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

In summary: 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.
  • #1
JoAuSc
198
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
  • #2
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
 
  • #3
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.
 
  • #4
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
 

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

1. What is translation in Java?

Translation in Java refers to the process of moving or shifting an object's position in a specified direction and distance. This is commonly used to create animations or manipulate the position of objects in a graphical user interface.

2. How can I stop my polygon from translating?

To stop a polygon from translating in Java, you can use the setTranslateX() and setTranslateY() methods and set the values to 0. This will reset the translation and keep the polygon in its current position.

3. Can I control the speed of translation in Java?

Yes, you can control the speed of translation in Java by using a Timeline object and specifying the duration and distance of the translation. You can also use the setRate() method to adjust the speed during the translation.

4. How do I prevent other objects from being affected by the translation of a polygon?

To prevent other objects from being affected by the translation of a polygon, you can use the setTranslateX() and setTranslateY() methods on the specific object you want to translate. This will keep the translation isolated to that object only.

5. Is it possible to rotate a polygon while also translating it?

Yes, it is possible to rotate a polygon while also translating it in Java. You can use the setRotate() method to specify the angle of rotation and the setTranslateX() and setTranslateY() methods to specify the distance and direction of translation.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
33
Views
8K
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
Back
Top