user3225609
user3225609

Reputation: 19

How to set a location of point using Point class?

I am trying to set a location of geometric figure that appears in JFrame window. For example if it's a rectangle so i need to move it's left upper point 10 pixels right, and left lower point 10 pixels down.

I was trying to do like this but it didn't work:

public void relocate(ArrayList<MyShape> newShape){

    int x1, x2, y1 , y2;

    for(int i = 0; i < newShape.size(); i++){

        x1 = (int)newShape.get(i).p1.getX();
        y1 = (int)newShape.get(i).p1.getY();

        x2 = (int)newShape.get(i).p2.getX();
        y2 = (int)newShape.get(i).p2.getY();


        newShape.get(i).setLocation(x1 + 10, y1);
        newShape.get(i).setP1(newShape.get(i).getP1());
        newShape.get(i).setLocation(x2, y2 + 10);
        newShape.get(i).setP2(newShape.get(i).getP2());


        if(newShape.get(i).getCol() != null){
            newShape.get(i).setCol(Color.BLUE);
        }


    }

    repaint();
}

Upvotes: 1

Views: 1089

Answers (3)

Voltboyy
Voltboyy

Reputation: 129

Not sure what MyShape does, but it seems that newShape.get(i).setP1 just overrides newShape.get(i).setLocation right before it. Try adjusting your code like this:

//Change this
newShape.get(i).setLocation(x1 + 10, y1);
newShape.get(i).setP1(newShape.get(i).getP1());
newShape.get(i).setLocation(x2, y2 + 10);
newShape.get(i).setP2(newShape.get(i).getP2());
//To this
newShape.get(i).getP1().setLocation(x1 + 10, y1);
newShape.get(i).getP2().setLocation(x2, y2 + 10);

I hope this helps.

Upvotes: 1

Mark Adelsberger
Mark Adelsberger

Reputation: 45659

The following analysis is based on assumptions about how MyShape works since you've declined to provide those details when asked for them.

Look at your algorithm. For each shape:

1) You're capturing the original values of P1 and P2. So far so good.

2) You're calling the shape's setLocation() method with an adjusted value for P1. Now, what does setLocation() do? Understanding that is part of why you were asked about the MyShape class, and saying it's a superclass for all shapes doesn't help.

3) You tell the shape to set its P1 to whatever value you get by querying its P1. If the getters and setters do anything reasonable, this won't do anything.

4) You then call setLocation() again, this time with an adjusted value for P2. Again, what does this do? It probably undoes whatever you accomplished in step 2.

5) You tell the shape to set its P2 to whatever value you get by querying its P2. Again, this probably doesn't do anything.

What you want to do, I assume, is set P1 based on the adjusted values you calculated in step 2; and set P2 based on the adjusted values in step 4. But that's not what your code says unless secLocation, getP1, and getP2 are all doing very ununtuitive things.

Upvotes: 1

Tamas Rev
Tamas Rev

Reputation: 7166

Your code should work - in a sense that it changes the location of the objects on the screen. So, what's going on here? What's wrong with your solution?

I can give you a few hints though:

You can check if any other part of the code is messing with the location setting. It's possible that they override your settings.

Maybe this code runs outside of the AWT Thread. You can check it with SwingUtilities.isEventDispatchThread().

You can reuse the Point-s in Swing. Points are mutable object. This leads to not so clean code, but performance is important in this case:

MyShape shape = newShape.get(i);
Point p = shape.getP1();
p.setLocation(p.x, p.y + 10);

Maybe a revalidate() call can help too:

public void relocate(ArrayList<MyShape> newShape) {
    // some code changing locations
    revalidate(); // this is it
    repatint();
}

Upvotes: 1

Related Questions