• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Defining the exact bottom of panel for graphic component

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.



If I have a JFrame object and it's size has been set to a frameWidth of 300 and a frameHeight of 500 and I have a ball (circle) with radius 20 then I intend to draw the circle on the frame using the following



I wish my circle to always be placed "sitting" at the bottom of the frame and to me, it seems logical that this is done by setting my y co-ordinate to be the height of the frame less the radius*2(i.e the height of the circle) so that if I use the following it should be correct (please ignore my random setting for x for now)

have decide to store the co-ordinates in a Point object reference as below so I will eventually pass pos.x and pos.y as my x and y co-ordinates.




but each time I run the form my circle is not visible and is only vsible if I make the y co-ordinate much smaller(shorter). Why is this? Is this not the correct logic or do I have to consider anything else?

Thanks,

Colm
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
do I have to consider anything else?
The JFrame paint method is a Container method and draws the container before passing its graphics context to its children which they use to draw themselves. It can draw on top of the containers children.
Check the insets of the JFrame and move your ball up by the bottom inset to clear the lower border.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In a word, don't.

Don't draw directly on a JFrame for many reasons including loss of double buffering. Instead draw on a JPanel or JComponent, and then place the drawing panel in the JFrame's contentPane (or as a replacement contentPane). Next, please read the Sun graphics tutorials which will explain this and more.
 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.



I am actually adding a JPanel to a JFrame but when I all getInsets() , I get a value of zero back from the Insets object ref I used with that method, so I still end up with no value to assist me place my circle right on the frame floor with the bottom just touching it.


Any other ideas?

Thanks.

Colm
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have been given the answer. The Insets of a JPanel will be zero. Custom painting is done by overriding the paintComponent() method of the JPanel. So you do your painting relative to the panel. Then you add the panel to the content pane of the frame. Assuming you use the default BorderLayout, the panel will be positioned to take all the available space of the frame.

If you need further help then you need to create a SSCCE (Short, Self Contained, Compilable and Executable, Example Program), that demonstrates the incorrect behaviour.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wish my circle to always be placed "sitting" at the bottom of the frame and to me, it seems logical that this is done by setting my y co-ordinate to be the height of the frame less the radius*2(i.e the height of the circle)
If you are drawing in the frame, yes.
If you are drawing in another component, no. You use the width and height of the component you are drawing in to locate your ball in the component.
The JFrame and the JPanel child are not the same.
 
He's giving us the slip! Quick! Grab this tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic