• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Location and bounds of a component object

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Kezia Matthews
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic