GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Location and bounds of a component object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Location and bounds of a component object" Watch "Location and bounds of a component object" New topic
Author

Location and bounds of a component object

Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107
Hi All,
I came across a review question in the Khalid's book, the answer of which I feel is not correct. The question goes like this:
Which of these methods can be used to manipulate the bounds of a Component object?
Select all valid answers.
(a) setSize()
(b) setDimnesions()
(c) setBounds()
(d) setLocation()
(e) setPosition()
Answers:
a,c,d
I agree with 'a' and 'c' but not with 'd'. How can location be used to manipulate the bounds ? Am I wrong or is the answer wrong? Can anybody clear this for me.
Thanks in advance.
Kezia.
James Thomson
Greenhorn

Joined: Nov 19, 2001
Posts: 6
The 'bounds' of a component are defined by it's x,y location, width and height. Therefore changing the location affects the bounds as does changing the size.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I do not see either why d is correct.
This code shows how the size o f button is not changed after calling setLocation on it:


SCJP2. Please Indent your code using UBB Code
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
public void setSize(int width, int height)
Resizes this component so that it has width width and height.
public void setSize(Dimension d)
Resizes this component so that it has width d.width and height d.height.
public void setBounds(int x, int y, int width, int height)
Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.
public void setBounds(Rectangle r)
Moves and resizes this component to conform to the new bounding rectangle r. This component's new position is specified by r.x and r.y, and its new size is specified by r.width and r.height
public void setLocation(int x, int y)
Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent.
public void setLocation(Point p)
Moves this component to a new location. The top-left corner of the new location is specified by point p. Point p is given in the parent's coordinate space.
I base my argument on setBounds by name saying it changes bounds of a component, it has a variant accepting first two parameters x and y, which change location of a component. With this view setLocation also changes the location i.e. x and y coordinates of the component and so should be seen as changing the bounds of an object.
Cheers,
Manoj


Cheers, <img src="smile.gif" border="0"> <br /><a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">Manoj</a><br />(<a href="http://www7.brinkster.com/manoj9/" target="_blank" rel="nofollow">http://www7.brinkster.com/manoj9/</a>)
Chin Loong
Ranch Hand

Joined: Nov 19, 2001
Posts: 45
i think it all boils down to what is actually "bounds"?
is it the location? or the size? or a mixture of both?
Manoj Gupta
Greenhorn

Joined: Oct 31, 2001
Posts: 29
Both size and location.
Check getBounds() description at: http://java.sun.com/j2se/1.4/docs/api/java/awt/Component.html#getBounds()

public Rectangle getBounds()
Gets the bounds of this component in the form of a Rectangle object. The bounds specify this component's width, height, and location relative to its parent.

------------------
Cheers,
Manoj
(http://www7.brinkster.com/manoj9/)
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
I think a,c ,d are correct as bound take into consideration size as well as location, by setting the location we are affecting the bounds of the object.

------------------
Muhammad Farooq
Sun Certified Programmer for Java 2 Platform


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Kezia Matthews
Ranch Hand

Joined: May 19, 2001
Posts: 107

public Rectangle getBounds()
Gets the bounds of this component in the form of a Rectangle object. The bounds specify this component's width, height, and location relative to its parent.

According to the definition of getbounds(), we come to know that bounds of a component is in the form of a rectangle, which is inturn determined by the width and height of the component. If you move a component with a definite width and height to another location, the width or height does not change; the component with the same width and height is moved to a different location. How can moving the component in this way be used to manipulate the bounds of the component? This is what I am not clear about.
Regards,
Kezia.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Location and bounds of a component object