Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Location and bounds of a component object

Kezia Matthews
Ranch Hand
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?
(a) setSize()
(b) setDimnesions()
(c) setBounds()
(d) setLocation()
(e) setPosition()
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.
Kezia.

James Thomson
Greenhorn
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
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:

Manoj Gupta
Greenhorn
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

Chin Loong
Ranch Hand
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
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/)

Ranch Hand
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.

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

Kezia Matthews
Ranch Hand
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.