Results 1 to 5 of 5
  1. #1
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default 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:
    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();
    	}
    }
    And heres the JPanel which i need the height of:
    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 08:08 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default

    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?

  3. #3
    peterhabe is offline Member
    Join Date
    Jul 2010
    Posts
    43
    Rep Power
    0

    Default

    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.

  4. #4
    mkj
    mkj is offline Member
    Join Date
    Jan 2012
    Posts
    15
    Rep Power
    0

    Default 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

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default 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 coordinates
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. getWidth(),GetHeight() using POI
    By Zeus in forum New To Java
    Replies: 0
    Last Post: 08-02-2010, 01:28 PM
  2. Giving Inputs to Java Applet
    By rasikh in forum Java Applets
    Replies: 2
    Last Post: 10-05-2009, 04:08 PM
  3. getWidth && getHeight
    By courteous in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 11:39 PM
  4. Giving Warning
    By tanmoy.b81 in forum New To Java
    Replies: 1
    Last Post: 12-24-2008, 10:21 AM
  5. getHeight() problem
    By sapata in forum AWT / Swing
    Replies: 1
    Last Post: 05-18-2008, 05:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •