Original Post
package traffic;
import java.awt.Color;
import java.awt.Graphics2D;
public class vehicle {
private static final int size = 20;
private Color vehicleColor;
private int Xvehicle, Yvehicle, speedVehicle;
public vehicle(int x, int y, int velocity) {
vehicleColor = Color.BLUE;
Xvehicle = x;
Yvehicle = y;
speedVehicle = velocity;
}
public void drive() {
Xvehicle += speedVehicle;
}
public void draw(Graphics2D g2) {
g2.setColor(vehicleColor);
g2.fillRect(Xvehicle, Yvehicle, size, size);
}
}
package traffic;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Anime extends junction {
private vehicle vec_1 = new vehicle(80,365, 2);
private int a_interval = 35;
private int b_interval = 4000;
private Timer a_timer, b_timer;
public Anime() {
a_timer = new Timer(a_interval, new Timer1());
a_timer.start();
b_timer = new Timer(b_interval, new Timer2());
}
public void setAnimation(boolean StartStop) {
if (StartStop) {
a_timer.start();
b_timer.start();
} else {
a_timer.stop();
b_timer.stop();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g3 = (Graphics2D) g;
vec_1.draw(g3);
}
class Timer1 implements ActionListener {
public void actionPerformed(ActionEvent e) {
vec_1.drive();
repaint();
}
}
class Timer2 implements ActionListener {
public void actionPerformed(ActionEvent e) {
// g = (Graphics2D)this.getGraphics();
//create new vehicle randomly...
}
}
}
hi, my application paints a vehicle (rectangle) and moves across the 'street'. I am having a problem with generating/producing new vehicles when the current vehicles have dissappeared from the jframe/street. I am using a timer to produce a vehicle every 7 seconds, i think i am doing it in the wrong way. Any help/ideas? Thank you [Edited by - ChaosTheory on March 1, 2009 10:30:11 AM]