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
Quote:
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);
}
}
Quote:
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){}
}
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.
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)
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.
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.
Re: Using variable in different class
Look at this line of code:
Code java:
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.
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
Code java:
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 :(
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
Re: Using variable in different class
Quote:
Originally Posted by
ICEPower
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.
Re: Using variable in different class
So what you are saying is:
Quote:
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.
Re: Using variable in different class
Quote:
Originally Posted by
ICEPower
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?
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;
Code :
BsP.startsnelheid = 10; //10 is an example you can give it any value you want
Hope I was clear.
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
Re: Using variable in different class
Quote:
Originally Posted by
andreas90
What do you mean it didn't work?
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;
Code :
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)
Re: Using variable in different class
Quote:
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...