hi,

I have an application that paints a rectangle(car) and moves it across the screen using a timer when a start button is clicked.

The problem i have is i want to generate(paint) cars randomly at specific intervals (using a timer) when the cars disappear from the screen/street.

Now i cant use a paint method inside the action event (action listener of timer) and vice-versa i cant put the action event inside the paint.

So i have a vehicle class that produces a car like new rectcar(2,5,7)
I want to know where to put the code that will generate the cars randomly any ideas?

You didn't post any code, so I can't speak directly to your current structure, but you should be able to do all that in your current animation loop. If your paint method were to operate on a List of car objects, your animation loop can simply update/create/remove car objects in that list and call repaint.

while (running) {
  updateCars();
  repaint();
}
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  =43;
    private int   b_interval = 7000;
    private Timer a_timer, b_timer;


    public Anime() {

        a_timer = new Timer(a_interval, new Timer1());

       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...
        }
    }
}

junction class is a Jcomponent that paints the street/background.
How would i be able to generate new cars in the inner class of Timer2?

Inside Timer3 you can change all parameters of a_timer, vehicle.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.