Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[java] Generating new Vehicles

Started by ChaosTheory Mar 1, 2009 at 2:31 AM 0 replies 1k views
Original Post
ChaosTheory
ChaosTheory

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]
Fruny
Fruny
Following our discussion on #gamedev, it would appear you need to instantiate a collection (e.g. an array or something) to store the various car instances you are going to need. Creating individual variables like car1, car2, car3... is definitely not the way to go.

A quick google search on java collections returns this Sun tutorial on the topic. It might help you.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.