Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: using the setSize and setLocation methods of Rectangle class

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Exclamation using the setSize and setLocation methods of Rectangle class

    hi everyone. how are you?

    i have a class square that inherits the rectangle class. what i have is center coordinates of the center of a square. i have figured the arithmetic to getting the coords of the top left coord of the square (rectangle). what i need to do is to use the methods setSize and setLocation of the Rectangle class to tell the program that these new coordinates are where the square actually is, and then i can return the values to the user, but how do i properly use the methods setSize and setLocation?

    import java.awt.Rectangle;
     
    public class Square extends Rectangle
    {
    	public int x;
    	public int y;
    	public int sideLength;
     
    	public Square(int xCoord, int yCoord, int lengthOfSide)
    	{
    		x = xCoord;
    		y = yCoord;
    		sideLength = lengthOfSide;
    	}
     
    	int xCoordTopLeft = x - sideLength / 2;
    	int yCoordTopLeft = y + sideLength / 2;
     
    	Rectangle r = new Rectangle(x, y);
    	[B]r.setLocation(xCoordTopLeft, yCoordTopLeft); // error - says parentheses are misplaced
            r.setSize(sideLength, sideLength); //error[/B]
     
     
    	public int getArea()
    	{
    		int area = sideLength * sideLength;
    		return area;
    	}
     
     
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: using the setSize and setLocation methods of Rectangle class

    Your code must be placed inside of a method (or at least a static block). Otherwise, if it's not initializing a declared variable, Java will complain.

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. Help with GUIs please & methods
    By killerknight141 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2010, 07:12 AM
  3. Re-using methods?
    By Morevan in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 26th, 2010, 05:04 PM
  4. centering a label inside a rectangle
    By Brain_Child in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2009, 09:08 AM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 08:22 AM