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

    Default Getting vaiables from a class that is not currently running its code

    Another class in this program needs to get the width and height of this JPanel after the code in this class has finished running and the JPanel is just sitting on the screen doing nothing. I have tried to return these values to the other class, but after the code in this class has ran these values the other class needs are = 0. Anyone help?

    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ProjectileAnimationJPanel extends JPanel{
    	//class objects
    	ControlPanelJPanel controlPanelJPanel = new ControlPanelJPanel();
    	RepaintThread repaintThread = new RepaintThread(this);
    	//variables
    	float elevationDP;
    	int elevation, panelWidth, panelHeight;
    	//constructor
    	public ProjectileAnimationJPanel(){
    //////////////////////////first printout////////////////////////
    		System.out.println("panelWidth printed from constructor()"+panelWidth);
    	}
    	//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 ground
    		g2.setColor(Color.GREEN);
    		g2.fillRect(0, 7*getHeight()/8, getWidth(), getHeight()/8+1);
    		
    		//drawing elevation
    		g2.fillRect(0, getHeight()-elevation-(this.getHeight()/8),this.getWidth()/10 , elevation+1);
    		
    		//drawing ball
    		g2.setColor(Color.RED);
    		g2.fillOval(getWidth()/10-getWidth()/27, getHeight()-elevation-getHeight()/8-getHeight()/20, getWidth()/27, getHeight()/20);
    		
    				
    		//drawing black lines
    		g2.setColor(Color.BLACK);
    		g2.drawLine(getWidth()/10, 7*getHeight()/8, this.getWidth(), 7*getHeight()/8);
    		g2.drawLine(0, getHeight()-elevation-(getHeight()/8), getWidth()/10, getHeight()-elevation-(getHeight()/8));
    		g2.drawLine(this.getWidth()/10, getHeight()-elevation-(getHeight()/8), getWidth()/10, 7*getHeight()/8);
    		
    		//setting panelWidth and panelHeight
    		panelWidth = getWidth();
    		panelHeight = getHeight();
    		
    		//////////////////////////secound printout////////////////////////
    		System.out.println("panelWidth printed from paintComponent()"+panelWidth);
    
    		//setting elevation variable
    		setElevation();
    		
    		//calling getPanelWidth()
    		getPanelWidth();
    	}
    	public void setElevation(){
    		elevationDP = (getHeight()*(controlPanelJPanel.getElevation()/50))/4;
    		elevation = Math.round(elevationDP);
    	}
    	public int getPanelWidth(){
    		////////////////////////third and forth printout//////////////////////////
    		System.out.println("panelWidth printed from getPanelWidth(): "+panelWidth);
    		return panelWidth;
    	}
    	public int getPanelHeight(){
    		return panelHeight;
    	}
    	public float getSizeOfMeter(){
    		return getPanelHeight()/200;
    	}
    	public void startRepaintThread(){
    		
    	}
    }
    Moderator edit: code tags added
    Last edited by peterhabe; 07-23-2010 at 08:20 PM. Reason: Moderator edit: code tags added

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I have tried to return these values to the other class
    How did you try?

    What does the code you posted have to do with your problem?
    I don't see any comments in the code that explains what you are trying to do???

    What do the println()s show?

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Any reason to not just call getWidth and getHeight rather than your custom getPanel*? The current code sets the variables panelHeight/Width before the panel is realized or visible, in which case the JPanel has no width or height.

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

    Default

    @ Norm

    I am trying to return the width and height of this JPanal to another class. I have tired using an object from this class and the getWidth/height methods, which never worked so I created the variables panelWidth and panelHeight and set them to the getWidth/height methods to return to the other class and set them to the getWidth/height methods, which also never worked. The println()s are so I can see if the vaiables accually have the values I want them to.

    @ doWhile
    I've already explained why the getWidth/Height methods are currently not there. When I print the getWidth/Height methods before the panelWidth/Heigth variavbles are set to the getWidth/Height methods, it prints out the width and height of the panel without a problem.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You probably have a problem with not having a decent reference to the displayed JPanel. I agree with Norm that your problem likely has nothing to do with the code you've posted.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    it prints out the width and height of the panel without a problem.
    If the variable: panelWidth is set and shown to be "correct". What changes the value of the variable between when it is shown and when you request it?

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

    Default

    That what im trying to find out. When I print out panelWidth straight after i've set it to getWidth() or called getPanelWidth() in paintComponent(), the value stored in panelWidth is what I want, but when my other class requests that variable from getPanelWidth() panelWidth = 0.

    This is the class that needs the width and height of the JPanel. It is needed in actionPerformed() - the second to last method where the thread for the animation is created.

    Background info:
    When the button "start" is clicked, a thread is created for the animation that will happen on the JPanel in the other class(ProjectileAnimationJPanel) and all the information the thread will need for the animation will be passed into the constructor of the AnimationThread class.

    Java Code:
    package projectileProgram;
    
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ControlPanelJPanel extends JPanel implements ChangeListener, ActionListener, ItemListener{
    	//variables
    	float elevation;
    	float projectionAngle = 0;
    	float projectionSpeed = 50;
    	float timePassed;
    	JSlider height = new JSlider(0,50,50);
        JSlider angle = new JSlider(0,90,0);
        JSlider speed = new JSlider(0,50,0);
        String heightValue;
        String angleValue = String.valueOf(angle.getValue());
        String speedValue = String.valueOf(speed.getValue());
        JButton start;
        JCheckBox timeSpeedGraph, verticalVelocityGraph, horozontalVelocityGraph;
        JLabel heightValues, angleValues, speedValues, time;
        //constructor
    	public ControlPanelJPanel(){
    		heightValue = String.valueOf(height.getValue());
    		elevation = Float.parseFloat(heightValue);
    		//height JSlider
    		height.setMajorTickSpacing(10);
    		height.setMinorTickSpacing(1);
    		height.setPaintTicks(true);
    		height.setPaintLabels(true);
    		height.setOpaque(false);
    		height.addChangeListener(this);
    		
    		//Angle JSlider 
    		angle.setMajorTickSpacing(10);
    		angle.setMinorTickSpacing(1);
    		angle.setPaintTicks(true);
    		angle.setPaintLabels(true);
    		angle.setOpaque(false);
    		angle.addChangeListener(this);
    		
    		//Speed JSlider 
    		speed.setMajorTickSpacing(10);
    		speed.setMinorTickSpacing(1);
    		speed.setPaintTicks(true);
    		speed.setPaintLabels(true);
    		speed.setOpaque(false);
    		speed.addChangeListener(this);
    		
    		//setting timePassed
    		JLabel time = new JLabel("Time/s: 0");
    		
    		//creating labels for Sliders	
    		JLabel heightLabel = new JLabel	("Height: ");
            JLabel angleLabel = new JLabel ("Angle: ");
            JLabel speedLabel = new JLabel ("Speed: ");
            
            //creating labels for value of sliders
            heightValues = new JLabel("Height Value: ");
            angleValues = new JLabel ("Angle Value: ");
            speedValues = new JLabel ("Speed Value: ");
            
            //start button
            JButton start = new JButton("start");
            start.setPreferredSize(new Dimension(75,25));
            start.addActionListener(this);
            
            //setting JCheckBoxes
            JCheckBox timeSpeedGraph = new JCheckBox("Time Speed Graph");
            timeSpeedGraph.setOpaque(false);
            timeSpeedGraph.addItemListener(this);
            
            JCheckBox verticalVelocityGraph = new JCheckBox("Vertical Velocity Graph");
            verticalVelocityGraph.setOpaque(false);
            verticalVelocityGraph.addItemListener(this);
            
            JCheckBox horozontalVelocityGraph = new JCheckBox("Horozontal Velocity Graph");
            horozontalVelocityGraph.setOpaque(false);
            horozontalVelocityGraph.addItemListener(this);
            
            //setting layout
            this.setLayout(new GridBagLayout());
            GridBagConstraints gc = new GridBagConstraints(); 
            
            //column 0
            gc.weightx = 25;
    		gc.weighty = 100;
            //adding start to screen 
    		gc.gridx = 0;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.NONE;
    		add(start, gc);
    		
    		//adding timePassed
    		gc.gridx = 0;
    		gc.gridy = 6;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.NONE;
    		add(time, gc);
            
    		//column 1
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding heightLabel to screen 
    		gc.gridx = 1;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(heightLabel, gc);
    		
    		//column 2
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding height to screen
    		gc.gridx = 2;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(height, gc);
    		
    		//adding heightValues to screen
    		gc.gridx = 2;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(heightValues, gc);
    		
    		//column 3
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding angleLabel to screen
    		gc.gridx = 3;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(angleLabel, gc);
    		
    		//column 4
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding speed angle to screen
    		gc.gridx = 4;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(angle, gc);
    		
    		//adding angleValues to screen
    		gc.gridx = 4;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(angleValues, gc);
    		
    		//column 5
    		gc.weightx = 25;
    		gc.weighty = 100;
    		//adding speedLabel to screen
    		gc.gridx = 5;
    		gc.gridy = 1;
    		gc.gridwidth = 1;
    		gc.gridheight = 1;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.EAST;
    		gc.fill = GridBagConstraints.NONE;
    		add(speedLabel, gc);
    		
    		//column 6
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding speed to screen
    		gc.gridx = 6;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(speed, gc);
    		
    		//adding speedValues to screen
    		gc.gridx = 6;
    		gc.gridy = 3;
    		gc.gridwidth = 2;
    		gc.gridheight = 3;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(speedValues, gc);
    		
    		//column 7
    		gc.weightx = 100;
    		gc.weighty = 100;
    		//adding timeSpeedGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 0;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(timeSpeedGraph, gc);
    		
    		//adding horozontalVelocityGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 2;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(horozontalVelocityGraph, gc);
    		
    		
    		//adding verticalVelocityGraph to screen
    		gc.gridx = 7;
    		gc.gridy = 4;
    		gc.gridwidth = 1;
    		gc.gridheight = 2;
    		gc.insets = new Insets(5,5,5,5);
    		gc.anchor = GridBagConstraints.CENTER;
    		gc.fill = GridBagConstraints.BOTH;
    		add(verticalVelocityGraph, gc);
    	}
    	//paint method
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		this.setBackground(Color.GREEN);
    	}
    	//returning elevation method
    	public float getElevation(){
    		return elevation;
    	}
    	public float getAngle(){
    		return projectionAngle;
    	}
    	public float getProjectionSpeed(){
    		return projectionSpeed;
    	}
    	//state listener
    	public void stateChanged(ChangeEvent event) {
    		JSlider source = (JSlider) event.getSource();
    		
    		//setting elevation/projectionAngle or projectionSpeed value to new JSlider Value
    		if(source.getValueIsAdjusting() != true){
    			if(event.getSource() == height){
    				heightValue = String.valueOf(height.getValue());
    				elevation = Float.parseFloat(heightValue);
    			}else if(event.getSource() == angle){
    				angleValue = String.valueOf(angle.getValue());
    				 projectionAngle = Float.parseFloat(angleValue);
    			}else if(event.getSource() == speed){
    				speedValue = String.valueOf(speed.getValue());
    			 	projectionSpeed = Float.parseFloat(speedValue);
    			 }
    		}
    	}
    	//action listener
    	public void actionPerformed(ActionEvent event){
    		ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel();
    		//this is where the width and height of the other JPanel is needed
    		Thread animationThread = new Thread(new AnimationThread(projectileAnimationJPanel, getElevation(), getAngle(), getProjectionSpeed(), projectileAnimationJPanel.getPanelWidth(), projectileAnimationJPanel.getPanelHeight(), projectileAnimationJPanel.getSizeOfMeter()));
    		animationThread.start();
    	}
    	//item listener
    	public void itemStateChanged(ItemEvent event){
    		
    	}
    }
    Last edited by peterhabe; 07-23-2010 at 07:11 PM.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I print out panelWidth straight after i've set it to getWidth() or called getPanelWidth() in paintComponent(), the value stored in panelWidth is what I want, but when my other class requests that variable from getPanelWidth() panelWidth = 0.
    If the variable is set once and not changed, then its value should stay the same.
    Is there more than one instance of the class?

    Can you show the print outs you get when you execute the program?

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

    Default

    These are the printouts I get when I get when I run the program:
    panelWidth printed from paintComponent()1246
    panelWidth printed from getPanelWidth(): 1246

    And the printout when I press the start button in the ControlPanelJPanel class:
    panelWidth printed from getPanelWidth(): 0

    I have updated my first post to show clearly where the printouts come from.
    Last edited by peterhabe; 07-23-2010 at 07:54 PM.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel();
    Is there more than one instance of the class?

    Add a println() to the constructor of the class with the panelWidth variable.

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

    Default

    Sorry about forgetting to answer that question. The line: "ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel();" is in the main class and in actionPerformed() in the ControlPanelJPanel class. Creating a class object is called an instance?

    These are all the printouts I get in order:
    panelWidth printed from constructor()0
    panelWidth printed from paintComponent()1256
    panelWidth printed from getPanelWidth(): 1256

    second run of paintComponent() - dont know why it runs twice but it happpens with all my classes in the program
    panelWidth printed from paintComponent()1246
    panelWidth printed from getPanelWidth(): 1246

    after I press the start button
    panelWidth printed from constructor()0
    panelWidth printed from getPanelWidth(): 0

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    Your printout show that you have created two instances of the class, the first one has the good values in it, the second one doesn't.
    Why are you creating the second instance of the class? You should have a reference to the first one. The one that is showing and has the correct value.

    Creating a class object is called an instance?
    Yes. More vocabulary. You create an instance/object of a class.

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

    Default

    How else can I call methods in the class ProjectileAnimationJPanel from the ControlPanelJPanel class without creating a ProjectileAnimationJPanel object in ControlPanelJPanel? It doesn't affect what is displayed on JPanel.
    Last edited by peterhabe; 07-23-2010 at 08:32 PM.

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    If you make a new instance of the class, it will have new variables with new values. The values you want are in the first instance of the class. You need to have a reference to the first instance available so you can use it to get values from it.

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

    Default

    I've finally got it working. The only instance is now in the main class. Thanks for the help Norm.

Similar Threads

  1. Running extensions other than .class
    By gwaldarick in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 08:57 AM
  2. Replies: 1
    Last Post: 02-04-2010, 11:12 AM
  3. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  4. running .class files on a website
    By jklsemicolon in forum Networking
    Replies: 10
    Last Post: 08-08-2008, 06:21 AM
  5. Problems in running client class
    By ai_2007 in forum Advanced Java
    Replies: 0
    Last Post: 06-30-2007, 02:57 PM

Posting Permissions

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