Results 1 to 8 of 8
  1. #1
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default How to find x and y values of Point objects?

    Hi, I'm writing a class Rectangle that has private int x, y, width, height, and Point p1. I need to change just the Point's x and y values.
    I'm having trouble getting the x and y values of a Point p. I brought in the Point p from main, so I'm getting the error: x has private access in Point.
    Is this because I didn't initialize p in the constructor itself?
    I would really appreciate any help!

    This is my constructor below:

    Java Code:
    public Rectangle(Point p, int width, int height)
    	{
    		this.p1 = p;
    		this.x = p.x;
    		this.y = p.y;
    		this.width = width;
    		this.height = height;
    	}

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to find x and y values of Point objects?

    In my version of the JSE classes the Point class has public members x and y ... are you using your own Point class?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to find x and y values of Point objects?

    Yes, I am using my own Point class. Even when I import the java.awt package, I still can't do ( this.x = p.x; )

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to find x and y values of Point objects?

    If it's your own Point class, then write a setX() method for it that's public. And use that method to change the value.

  5. #5
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to find x and y values of Point objects?

    Thanks, but how should I call that in my Rectangle constructor?

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to find x and y values of Point objects?

    You would call the setter methods whereever you want to set the values.

    -----

    You said you wanted to set a point's values. But you also said your rectangle also has x and y values. Are you sure you don't want to get the point's values and assign them to x and y of the rectangle? The process is the same: write getter methods for the point class and use them wherever you like - including in the rectangle constructor if that makes sense.

  7. #7
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: How to find x and y values of Point objects?

    Thank you so much! I finally got everything after three hours of confusion. Thanks a bunch.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: How to find x and y values of Point objects?

    You're welcome

Similar Threads

  1. Replies: 8
    Last Post: 11-22-2010, 10:09 AM
  2. Replies: 3
    Last Post: 09-29-2010, 02:38 PM
  3. Replies: 1
    Last Post: 07-14-2009, 07:04 PM
  4. Tracking Values Using Objects
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-20-2008, 08:18 PM
  5. Floating point values in SWT Spinner
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-07-2008, 04:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •