Results 1 to 5 of 5
- 08-26-2010, 06:44 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 43
- Rep Power
- 0
getHeight() giving two different values for the same JPanel
The getHeight() of projectileAnimationJPanel is giving two different values depending on when it is run. I need both of the values for the height to be the same.
Heres the main class:
And heres the JPanel which i need the height of:Java Code:package projectileProgram; import javax.swing.*; import java.awt.*; public class ProjectileProgramFixedSize { //class objects /////////////////////////////JPanel i need the height of//////////////////////////////// ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel(); ControlPanelJPanel controlPanelJPanel = new ControlPanelJPanel(); TimeSpeedGraphJPanel timeSpeedGraphJPanel = new TimeSpeedGraphJPanel(controlPanelJPanel); HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel = new HorizontalVelocityGraphJPanel(); VerticalVelocityGraphJPanel verticalVelocityGraphJPanel = new VerticalVelocityGraphJPanel(); //variables JScrollPane timeSpeedGraphJScrollPane = new JScrollPane(timeSpeedGraphJPanel); JScrollPane horizontalVelocityGraphJScrollPane = new JScrollPane(horizontalVelocityGraphJPanel); JScrollPane verticalVelocityGraphJScrollPane = new JScrollPane(verticalVelocityGraphJPanel); //main method public static void main(String args[]){ new ProjectileProgramFixedSize(); } //constructor public ProjectileProgramFixedSize(){ JFrame frame = new JFrame("Projectile Program"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting frame frame.setSize(1920,1080); frame.setLayout(new GridBagLayout()); //setting layout GridBagConstraints gc = new GridBagConstraints(); gc.gridx = 0; gc.gridy = 0; gc.gridwidth = 1; gc.gridheight = 3; gc.weightx = 100; gc.weighty = 100; gc.insets = new Insets(5,5,5,5); gc.anchor = GridBagConstraints.CENTER; gc.fill = GridBagConstraints.BOTH; frame.add(projectileAnimationJPanel, gc); gc.gridx = 1; gc.gridy = 0; gc.gridwidth = 1; gc.gridheight = 1; gc.weightx = 50; gc.weighty = 50; gc.insets = new Insets(5,5,5,5); gc.anchor = GridBagConstraints.CENTER; gc.fill = GridBagConstraints.BOTH; frame.add(timeSpeedGraphJScrollPane, gc); gc.gridx = 1; gc.gridy = 1; gc.gridwidth = 1; gc.gridheight = 1; gc.insets = new Insets(5,5,5,5); gc.anchor = GridBagConstraints.CENTER; gc.fill = GridBagConstraints.BOTH; frame.add(horizontalVelocityGraphJScrollPane, gc); gc.gridx = 1; gc.gridy = 2; gc.gridwidth = 1; gc.gridheight = 1; gc.insets = new Insets(5,5,5,5); gc.anchor = GridBagConstraints.CENTER; gc.fill = GridBagConstraints.BOTH; frame.add(verticalVelocityGraphJScrollPane, gc); gc.gridx = 0; gc.gridy = 3; gc.gridwidth = 2; gc.gridheight = 1; gc.weighty = 5; gc.insets = new Insets(5,5,5,5); gc.anchor = GridBagConstraints.CENTER; gc.fill = GridBagConstraints.BOTH; frame.add(controlPanelJPanel, gc); frame.setResizable(false); frame.setVisible(true); settingVariablesForOtherClasses(); } public void settingVariablesForOtherClasses(){ //creating thread to set elevation Thread settingElevationThread = new Thread(new SettingElevationThread(controlPanelJPanel, projectileAnimationJPanel)); settingElevationThread.start(); //setting x and y positions in ProjectileAnimationJPanel class on first run projectileAnimationJPanel.settingXAndYPosisions(projectileAnimationJPanel.getWidth()/20-projectileAnimationJPanel.getHeight()/40, projectileAnimationJPanel.getHeight()-controlPanelJPanel.getElevation()-projectileAnimationJPanel.getHeight()/8-projectileAnimationJPanel.getHeight()/20); //giving actionPerformed() in ControlPanelJPanel class the variables needed to start and run threads /////////////////////////where the hight of the JPanel is needed//////////////////// controlPanelJPanel.settingValues(projectileAnimationJPanel, timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel, controlPanelJPanel, projectileAnimationJPanel.returnWidth(), projectileAnimationJPanel.returnHeight(), projectileAnimationJPanel.getSizeOfMeter()); //starting repaint thread Thread repaintThread = new Thread(new RepaintThread(projectileAnimationJPanel, timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel)); repaintThread.start(); } }
Java Code:package projectileProgram; import javax.swing.*; import java.awt.*; public class ProjectileAnimationJPanel extends JPanel{ //variables float elevationDP, meter; int elevation, previousElevation, xPosition, yPosition; int i = 0; boolean run = false; //paint method public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; super.paintComponent(g2); this.setBackground(Color.BLUE); //adding antialiasing g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //drawing elevation g2.setColor(Color.GREEN); g2.fillRect(0, getHeight()-elevation-(this.getHeight()/8),this.getWidth()/20 , elevation+1); //drawing ground g2.setColor(Color.GREEN); g2.fillRect(0, 7*getHeight()/8, getWidth(), getHeight()/8+1); //drawing ball g2.setColor(Color.RED); g2.fillOval(xPosition, yPosition, getHeight()/20, getHeight()/20); //drawing black lines g2.setColor(Color.BLACK); //drawing on ground g2.drawLine(getWidth()/20, 7*getHeight()/8, getWidth(), 7*getHeight()/8); //drawing line on top of elevation g2.drawLine(0, 7*getHeight()/8-elevation, getWidth()/20, 7*getHeight()/8-elevation); //drawing line at side of elevation g2.drawLine(getWidth()/20, 7*getHeight()/8-elevation, getWidth()/20, 7*getHeight()/8); /////////////////value for getHeight() is 875////////////// System.out.println(getHeight()); } public void setElevation(float e){ elevationDP = (getWidth()*(e/50))/8; elevation = Math.round(elevationDP); //testing for change in elevation if(previousElevation != elevation){ //setting height ball to new elevation settingXAndYStartingPosition(); //setting previousElevation to new elevation previousElevation = elevation; } } public float getSizeOfMeter(){ meter = Float.parseFloat(String.valueOf((getWidth()))); return meter/400; } public float getElevation(){ return elevation; } public void settingXAndYPosisions(double x, double y){ xPosition = Math.round(Float.parseFloat(String.valueOf(x))); yPosition = Math.round(Float.parseFloat(String.valueOf(y))); } public void settingXAndYStartingPosition(){ xPosition = getWidth()/20-getHeight()/40; yPosition = 7*getHeight()/8-elevation-getHeight()/20; } public int returnWidth(){ return getWidth(); } ////////////////method returning height of Jpanel to main class//////////////// public int returnHeight(){ /////////////// value of getHeight() is 872/////////////////// System.out.println(getHeight()); return getHeight(); } }Last edited by peterhabe; 08-26-2010 at 07:08 PM.
- 08-26-2010, 08:09 PM #2
Can you make a small program that compiles and demonstrates the problem.
Your posted code doesn't compile because of missing classes.
When does the value change relative to other GUI events? Events like setVisible, setResizable, adding of the component to a container
Does it change more than once? What causes the change?
- 08-27-2010, 02:09 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 43
- Rep Power
- 0
I've found a solution. The part of the program that needs the dimensions of the JPanel now gets the dimensions when it needs it instead of using the values the methods above get on first run. I think the change in dimensions is because there are three panels next to the panel above, which are drawn on after this one and one pixel is added to each of them for what ever reason.
- 02-13-2012, 10:43 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 15
- Rep Power
- 0
Re: getHeight() giving two different valpues for the same JPanel
hi,
i have seen that you are working on projectile motion. i ma interested in modeling physical problem and i wish to know what does your projectile animation program do
thanks in advance
- 02-13-2012, 10:54 PM #5
Re: getHeight() giving two different valpues for the same JPanel
Don't post to long dead threads. This one's 1½ years old, and the member you're addressing hasn't posted here for almost 5 months.
When you have a question, start your own thread -- they're free.
Closing this thread.
db
edit You might like to give more importance to replying to responses in a thread that you started: converting double to pixel coordinatesWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
getWidth(),GetHeight() using POI
By Zeus in forum New To JavaReplies: 0Last Post: 08-02-2010, 12:28 PM -
Giving Inputs to Java Applet
By rasikh in forum Java AppletsReplies: 2Last Post: 10-05-2009, 03:08 PM -
getWidth && getHeight
By courteous in forum New To JavaReplies: 1Last Post: 01-18-2009, 10:39 PM -
Giving Warning
By tanmoy.b81 in forum New To JavaReplies: 1Last Post: 12-24-2008, 09:21 AM -
getHeight() problem
By sapata in forum AWT / SwingReplies: 1Last Post: 05-18-2008, 04:16 AM


LinkBack URL
About LinkBacks

Bookmarks