Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Using variable in different class

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Using variable in different class

    Hi everyone,

    I'm new on this forum and also with Java. I was wondering if someone could help me out with this problem i'm having.

    I have 2 classes and I need to use a variable from one class in another.

    The variable is "startsnelheid" in "BeginsnelheidPaneel" and I need to use it in "SimulatiePaneel" to change "velX".

    I have made it the way I think it should work but it doesn't. Could someone help me out with this?

    Thnx in advance

    package flashed;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;

    public class BeginsnelheidPaneel extends JPanel implements ActionListener
    {
    private Image weg;
    KiesLocatie KL = new KiesLocatie();
    private String location;

    private JButton start;
    private JButton terug;
    private JLabel geefsnelheid;
    private JTextField snelheid;


    public FlashedFrame ff;

    public int startsnelheid;


    public BeginsnelheidPaneel(FlashedFrame fframe)
    {
    this.ff = fframe;
    this.setLayout(null);

    start = new JButton("Start");
    start.setFont(new Font("Blackoak Std",1,25)); //lettertype
    start.setForeground(Color.RED);//Letterkleur
    start.setBackground(new Color(50,50,50)); //Achtergrond
    start.setBounds(1040,700,255,50);
    start.setOpaque(false);
    start.addActionListener(this);
    this.add(start);

    terug = new JButton("Terug naar menu");
    terug.setFont(new Font("Blackoak Std",1,20)); //lettertype
    terug.setForeground(Color.RED);//Letterkleur
    terug.setBackground(new Color(50,50,50)); //Achtergrond
    terug.setBounds(80,700,490,50);
    terug.setOpaque(false);
    terug.addActionListener(this);
    this.add(terug);

    geefsnelheid = new JLabel("Geef een beginsnelheid in voor de wagen. Let op de maximumsnelheid!");
    geefsnelheid.setFont(new Font("Lucida Sans Unicode",1,18));
    geefsnelheid.setForeground(Color.RED);
    geefsnelheid.setBackground(Color.LIGHT_GRAY);
    geefsnelheid.setBounds(100,320,1100,20);
    geefsnelheid.setOpaque(false);
    this.add(geefsnelheid);

    snelheid = new JTextField(4);
    snelheid.setFont(new Font("Lucida Sans Unicode",1,30));
    snelheid.setForeground(Color.RED);
    snelheid.setBackground(Color.LIGHT_GRAY);
    snelheid.setBounds(100,340,100,35);
    this.add(snelheid);

    //startsnelheid = Integer.parseInt(snelheid.getText());


    this.setOpaque(false);
    }

    public void actionPerformed(ActionEvent e)
    {

    if (e.getSource()==terug)
    {
    ff.remove(this);
    ff.add(ff.menu);
    ff.validate();
    ff.menu.requestFocus();
    ff.repaint();
    }

    if(e.getSource()==start)
    {
    ff.remove(this);
    ff.add(ff.simulatie);
    ff.validate();
    ff.simulatie.requestFocus();
    ff.repaint();

    /*startsnelheid = Integer.parseInt(snelheid.getText());
    System.out.print(startsnelheid);*/
    }
    try
    {
    startsnelheid = Integer.parseInt(snelheid.getText());
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(null, "Geef een getal in!",
    "Foute ingave", JOptionPane.ERROR_MESSAGE);
    }

    }

    public void paintComponent(Graphics g)
    {
    location = KL.gekozenlocatie;
    if(location==null)
    {
    location = "Autosnelweg (120)";
    }
    weg = new ImageIcon("C:\\Users\\Khaled\\Desktop\\Informatica \\Afbeeldingen\\Locaties\\achtergrond\\"+location+ ".png").getImage();
    g.drawImage(weg,0,0,null);
    }
    }

    package flashed;

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;

    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JPanel;
    import javax.swing.Timer;

    public class SimulatiePaneel extends JPanel implements ActionListener, KeyListener
    {
    Timer time = new Timer(1, this);

    public FlashedFrame ff;

    int x = -230;

    private JButton einde;

    private GolvenUit golven;

    private Image auto;
    KiesAuto KA = new KiesAuto();
    private String car;

    private Image weg;
    KiesLocatie KL = new KiesLocatie();
    private String location;

    private Image flits;
    KiesRadar KR = new KiesRadar();
    private String radar;

    MobielLocatiePaneel MLP = new MobielLocatiePaneel(null);
    private int posmob;

    BeginsnelheidPaneel BsP = new BeginsnelheidPaneel(null);
    public double snelheid = BsP.startsnelheid;

    private double velX = snelheid/25;



    public SimulatiePaneel(FlashedFrame fframe/*, BeginsnelheidPaneel bv*/)
    {
    this.ff = fframe;
    this.setLayout(null);

    //this.BsP = bv;

    golven = new GolvenUit(this);
    golven.setBounds(0,0,1366,768);
    this.add(golven);

    time.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);

    einde = new JButton("Bekijk overtreding");
    einde.setFont(new Font("Adobe Caslon Pro Bold",1,30));
    einde.setForeground(Color.RED);
    einde.setBackground(new Color(0,0,0));
    einde.setBounds(930,700,400,50);
    einde.setOpaque(false);
    einde.addActionListener(this);
    this.add(einde);


    //String auto = (String)KA.gekozenauto;
    }

    public void paintComponent(Graphics g)
    {
    location = KL.gekozenlocatie;

    if(location==null)
    {
    location = "Autosnelweg (120)";
    }
    weg = new ImageIcon("C:\\Users\\Khaled\\Desktop\\Informatica \\Afbeeldingen\\Locaties\\achtergrond\\"+location+ ".png").getImage();
    g.drawImage(weg,0,0,null);



    car = KA.gekozenauto;
    if(car==null) //als er nx is gekozen (car heeft dan geen waarde) moet je ervoor zorgen dat het degene die als eerste staat kiest.
    {
    car = "Ferrari";
    }
    auto = new ImageIcon("C:\\Users\\Khaled\\Desktop\\Informatica \\Afbeeldingen\\Voertuigen\\Geknipt\\"+car+".png") .getImage();
    g.drawImage(auto, x, 440,null);



    radar = KR.gekozenradar;
    posmob = MLP.posmfp;
    if(radar==null)
    {
    radar = "Vast";
    }
    flits = new ImageIcon("C:\\Users\\Khaled\\Desktop\\Informatica \\Afbeeldingen\\Radar\\"+radar+".png").getImage();
    //tekenen hieronder:
    if(radar=="Vast")
    {
    g.drawImage(flits,800,535,null);
    }

    if(radar=="Mobiel")
    {
    g.drawImage(flits,posmob*8,535,null); // de gekozen locatie van de mobiele flitspaal moet hier gelinked worden!!!! MobielLocatiePaneel.posmfp !!!!!!!!!!!!
    }
    }

    public void actionPerformed(ActionEvent e)
    {
    if(x>1562)
    {
    x = 1561;
    //velX=0;
    }
    if(x<-230)
    {
    x = -230;
    //velX=0;
    }
    x += velX;
    repaint();

    if(e.getSource()==einde)
    {
    ff.remove(this);
    ff.add(ff.einde);
    ff.validate();
    ff.einde.requestFocus();
    ff.repaint();
    }


    }

    public void keyPressed(KeyEvent e)
    {
    int c = e.getKeyCode();
    if(c == KeyEvent.VK_LEFT)
    {
    velX -= 1;
    }
    if (c == KeyEvent.VK_RIGHT)
    {
    velX += 1;
    }
    System.out.print(snelheid);

    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){}
    }


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Using variable in different class

    In what way is it not working? Are you getting an error message? If so, please post the error message text. Everything seems fine with the code. At any rate answer the two questions I have posed and you should be able to get some more accurate help.
    Last edited by KucerakJM; May 10th, 2012 at 07:06 PM.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: Using variable in different class

    KucerakJM, actually, since startsnelheid is an int, it is automatically initialized to 0.
    This is a handy link for remembering the Primitive Default Values (scroll down to the table): Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Using variable in different class

    Quite right, completely slipped my mind, thank you for the clarification as well. Now to edit that last post.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    Thnx guys. As i already said i'm new to java. I read the link you uploaded but I still can't figure out why my code is not working.

    I do t get any errors but I know that the value of startsnelheid is equal to 0.0

    Thnx for the replies.

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,699
    Thanks
    25
    Thanked 334 Times in 310 Posts

    Default Re: Using variable in different class

    Look at this line of code:
    BeginsnelheidPaneel BsP = new BeginsnelheidPaneel(null);
    public double snelheid = BsP.startsnelheid;

    When you create a BeginsnelheidPaneel object, its startsnelheid variable is not initialized in the constructor, so its value is automatically set to 0. On the next line, you create and set the snelheid variable to the value of Bsp's startsnelheid variable, which is 0, therefore snelheid is equal to 0.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  7. The Following User Says Thank You to aussiemcgr For This Useful Post:

    ICEPower (May 12th, 2012)

  8. #7
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    What I understand from what you just said is:

    I should make a new variable x in BeginsnelheidPaneel, and in the constructor of BeginsnelheidPaneel I should equal x to startsnelheid. And in Simulatiepaneel I should
    BeginsnelheidPaneel BsP = new BeginsnelheidPaneel(null);
    	public double snelheid = BsP.startsnelheid;

    I tried that but it didn't work. I can't figure out why it doesn't work, it's probably something really easy but I just keep looking in the wrong way

  9. #8
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    Please can someone help me out with this? I really don't see why it doesn't work. I tried everything I know of doing

  10. #9
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Using variable in different class

    Quote Originally Posted by ICEPower View Post
    I should make a new variable x in BeginsnelheidPaneel, and in the constructor of BeginsnelheidPaneel I should equal x to startsnelheid. And in Simulatiepaneel I should
    Hello ICEPower!
    Read again post #6 to understand why startsnelheid is 0 (the default value).
    You don'to have to make a new variable x. You can just give startsnelheid an initial value in BeginsnelheidPaneel's constructor.

  11. #10
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    So what you are saying is:

    public BeginsnelheidPaneel(FlashedFrame fframe)
    {
    startsnelheid = 0;
    this.ff = fframe;
    this.setLayout(null);
    .
    .
    .}
    That's something that I had already done and it didn't work. That's why I suggested to make a new variable.

  12. #11
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Using variable in different class

    Quote Originally Posted by ICEPower View Post
    That's something that I had already done and it didn't work. That's why I suggested to make a new variable.
    What do you mean it didn't work?
    startsnelheid = 0;
    You assign startsnelheid to 0. But you can give it the value you want (it not necessary to be 0) and then use it as you want.
    Also if you want to change startsnelheid's value in your SimulatiePaneel class you can do it (in the constructor or in a method of it) by a simple assignment (since startsnelheid has public access) like;
    BsP.startsnelheid = 10; //10 is an example you can give it any value you want
    Hope I was clear.

  13. The Following User Says Thank You to andreas90 For This Useful Post:

    ICEPower (May 12th, 2012)

  14. #12
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    public BeginsnelheidPaneel(FlashedFrame fframe, double startsnelheid)
    {
    this.ff = fframe;
    this.setLayout(null);
    .
    .
    .}

    this also doesn't work because that only listens to what's in the constructor and not to the actionPerformed.

    Please can you give me another hint?
    This is a project I have, it's due to monday

  15. #13
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    Quote Originally Posted by andreas90 View Post
    What do you mean it didn't work?
    startsnelheid = 0;
    You assign startsnelheid to 0. But you can give it the value you want (it not necessary to be 0) and then use it as you want.
    Also if you want to change startsnelheid's value in your SimulatiePaneel class you can do it (in the constructor or in a method of it) by a simple assignment (since startsnelheid has public access) like;
    BsP.startsnelheid = 10; //10 is an example you can give it any value you want
    Hope I was clear.
    If you look at the code of SimulatiePaneel, you'll see what the idea is.

    I have a car and 'startsnelheid' is it's startvelocity. So when startsnelheid has a value for example 5. the car should be moving at that speed.
    That didn't happen. Not only nothing happend also the car suddenly ended up at the end of the screen...(all the way to the right)

  16. #14
    Junior Member
    Join Date
    May 2012
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Using variable in different class

    startsnelheid = Integer.parseInt(snelheid.getText());
    This line doesn't work when I do 'startsnelheid = 0;' in BeginsnelheidPaneel. Whatever I type 'startsnelheid' equals to 0 in SimulatiePaneel...

Similar Threads

  1. Abstract class variable help
    By star12345645 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2012, 08:40 AM
  2. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 10:17 AM
  3. Must I add a Class Variable?
    By maress in forum Java Theory & Questions
    Replies: 1
    Last Post: February 24th, 2011, 04:40 AM
  4. Access and set variable in parent class through child
    By java_newbie in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 19th, 2011, 11:44 PM
  5. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM