Jpanel.getHeight() is returning zero
hi,
i want to place a rectangle in a jpanel at the bottom left corner of the panel. i have thought of using the getheight() method and then using the object.setbounds(getWidth(), getHeight(),x,y) to place the object at the bottom left corner
but the problem i am having is that both getWidth(), getHeight() are returning zero
can any body help me
Re: Jpanel.getHeight() is returning zero
Re: Jpanel.getHeight() is returning zero
dear pbrockway2
in my program i am using a syntax in the same order as the code i am posting here
import my.graphics.MyRect;
import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Xindepro{
public static void main (String[] args)
{
JFrame frame1 = new JFrame("x indepedence of projectile motion");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE);
frame1.setSize(900,900);
JPanel Panel2 = new JPanel();
Panel2.setBackground(Color.white);
frame1.getContentPane().add(Panel2,BorderLayout.CE NTER);
Panel2.setPreferredSize(new Dimension(600,600));
int x = Panel2.getHeight();
int y = Panel2.getWidth();
Panel2.setLayout(null);
MyRect car = new MyRect(0,0,30,50);
car.setBounds(y,x,50,50);
}
frame1.setVisible(true);
frame1.pack();
}
Re: Jpanel.getHeight() is returning zero
The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).
Since you are adding the panel to the frame at BorderLayout.CENTER and then calling pack(0, the panel will be displayed at its preferredSize -- which you have set in code. So why do you feel the need to query the panel's width and height?
Incidentally, setting the bounds of car to (y, x, 50, 50) will place it outside the visible area of the panel -- so it won't be seen. And those are singularly badly named variables -- x for height, which is in the direction of the y-axis, and vice versa. What prevented you from naming the variables width and height?
db
Re: Jpanel.getHeight() is returning zero
Quote:
Originally Posted by
DarrylBurke
The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).
Since you are adding the panel to the frame at BorderLayout.CENTER and then calling pack(0, the panel will be displayed at its preferredSize -- which you have set in code. So why do you feel the need to query the panel's width and height?
from reading in the tutorial, i have understood that the setPreferredsize is not a fixed and it may change depending on layout manager that i am using. i was afraid if i set the bounds of the cars to the values declared in the preferredsize(600,600) that the car may not appear if the preferredsize values were decreased by the system
Quote:
Originally Posted by
DarrylBurke
Incidentally, setting the bounds of car to (y, x, 50, 50) will place it outside the visible area of the panel -- so it won't be seen.
sorry i didnt get why you have considered that the car will not be seen, the car is showing but it is showing at the position (0,0) of Panel2 instead of showing at the position (y,x) this is my problem that i want to solve
Quote:
Originally Posted by
DarrylBurke
And those are singularly badly named variables -- x for height, which is in the direction of the y-axis, and vice versa. What prevented you from naming the variables width and height?
you are right but i was just trying to test if i was able to place the car in the correct position or not
db[/QUOTE]
Re: Jpanel.getHeight() is returning zero
Quote:
why you have considered that the car will not be seen, the car is showing but it is showing at the position (0,0) of Panel2 instead of showing at the position (y,x)
And where is the position (y,x)?
Code:
0
0 +----------------------+
| |
| |
| |
| |
| PANEL |
| |
| |
| |
| |x
+----------------------+-------+
y | |
| CAR |
| |
+-------+
db
Re: Jpanel.getHeight() is returning zero
Theoretically what you have drawn should be happening if we consider that the car is located at a position (y,x) but as i told you before i want to locate the car at a position
(y,x) = (0, getheight()- height of the car). the problem is that i am always getting the car at the (0,0) position cuz the getheight() is returning a zero
to simplifiy the problem what should i do if i want the getHeight() to return the actual height of the panel. In the code i have presented above where should i call the getHeight() method.
thanks
0
0+---------------------------+
| actual | |
| position | |
|--------- |
| |
| |
| |
| |
|-------- |
| desired| |
| position| |
+----------------------------+
Re: Jpanel.getHeight() is returning zero
Quote:
i want to locate the car at a position
(y,x) = (0, getheight()- height of the car)
So waste people's time by posting misleading code.
Quote:
Code:
int x = Panel2.getHeight();
int y = Panel2.getWidth();
Panel2.setLayout(null);
MyRect car = new MyRect(0,0,30,50);
car.setBounds(y,x,50,50);
Quote:
to simplifiy the problem what should i do if i want the getHeight() to return the actual height of the panel.
Is that a question? A question should end with a question mark. Also, a sentence should start with an uppercase letter. Please go through this page.
I already told you:
Quote:
Quote:
The panel has its default size of [0, 0] until the frame is realized either by calling pack() or setVisible(true).
db
Re: Jpanel.getHeight() is returning zero
Thanks for the grammar page.
If i was wasting your time you can simply ignore my question, i don't think you are forced to answer my questions
I would like to remind you that i am new to java and i am not getting where should i put the frame1.pack() in order to have a getheight() different of zero.
regards.