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){}
}