I want to get the height and width of my JFrame so that a graphic should be at the same relative position even if the window is resized. To do this, I am trying to get the height and width in my constructor, but it always returns 0. What is the best way to do this?
public class FireworkComponent extends JComponent {
public FireworkComponent() {
//some variables ....
this.getHeight();
this.getWidth();
}
}
The dimensions will return 0
if the component isn't realized (i.e. visible). If you override the component's paintComponent
method, you will be able to retrieve the container's dimensions and set a graphic at a specific location by drawing on the component's Graphics
object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With