I am trying to get the speed of a car object. Actually of all cars stored in an array and then pass that value to another variable.
So heres an example of what I have:
public class Car
{
private int speed;
public Car(int s)
{
speed = s;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int s)
{
speed = s;
}
}
I have a class that creates an array of cars.
public class Environment {
private Car[] garage;
private Random random;
public Environment(){
random = new Random();
populateGarage();
}
public void populateGarage()
{
garage = new Car[4];
int randomSpeed;
Car car;
for(int i= 0; i < garage.length; i++)
{
randomSpeed = random.nextInt(10);
if(randomSpeed < 5){
randomSpeed = randomSpeed +5;
}
car = new Car(carNames[i], randomSpeed);
garage.add(car);
System.out.println("car has speed "+ car.getSpeed());
}
All works fine up to this point. Now I am trying to access that value in a different class. Here's an example:
public class RaceDisplay extends JPanel implements ActionListener{
private int velX;
private int x;
private Car car;
private Environment env;
public RaceDisplay(){
x=0;
velX=env.getArray[0]... (the velocity value should be one of the car's speeds) <-------------
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// (....)
}
public void actionPerformed(ActionEvent e) {
x=x+velX;
if(x>=650){
x=0;
x=x+velX;
}
}
I'm stuck on how to access that information through another class. Any help is most welcome.