Hi,
I am doing a project on creating a traffic simulator for a particular road junction in java. I am looking for ideas on how I would go about recreating the visual of the junction in which I can have moving cars on. Would I use Graph with nodes or is there another way?

Thanks

Recommended Answers

All 7 Replies

If you want it to look good, here's how you do it:
Create an image file (jpeg or whatever) for the junction, and one or more small image files for the car(s).
Program a JPanel with your own paintComponent method. In that method draw the junction image on the JPanel, then draw each of the cars in the correct position (rotated to point on the right direction). Drawing the cars should be delegated to the Car class for simplicity. Every time your simulation updates the cars' positions you re-paint the JPanel.
If you haven;t done this kind of thing before there's a non-trivial learning curve. However, once you're "got" it the code is actually very short.
If you want to take this route then we can supply more detailed info.

So would this method enable me to apply different situations like inserting traffic lights etc. to change the flow of traffic? I would be interested in doing it this way. The junction I would hope to do can be found here http://www.openstreetmap.org/?lat=52.6468&lon=-8.56451&zoom=17 . Do you think this method would be aplicable to this junction?

I would divide this into two distinct parts.
First, build the simulation model, keeping track of what vehiclea are where and how they are moving. Just use simple print statements to debug it and confirm that it's working as expected.
Second, build a GUI layer on top of that to display what's in the model. You can adjust how sexy the GUI is depnding on how much time you have left, but if the model is wrong or incomplete then the GUI will be useless anyway.

The method I described is the standard way of displaying a simulation in Java in real time. I can promise you that it works just fine.

ps You may do better to start with a much simpler junction - eg a single cross-roads between a straight N-S road and a straight E- road.

Ok thanks I will start off building the simulation model. Is there any example of a working simulator done by this method so I could see it executing?

Here's a really really simple runnable demo that has the basic parts: the Sprite class is the simulation of different things (squares, circles) moving about. This demos the use of a timer to run the simulation in real time, and the display technique I described above to show the sprites moving. It's totally simple, but it shows the key components and how they fit together. It's deliberately short on comments so you'll have to study it to understand it before you apply it's ideas to your own problem.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;

public class Animation2 extends JPanel implements ActionListener {
   // Animation demo showing use of an abstract Sprite class to
   // model basic behaviour for multiple moving objects.
   // Sprites have two mandatory methods - update and draw

   Animation2(int width, int height) {
      setPreferredSize(new Dimension(width, height));
      new Timer(50, this).start();
   }

   @Override
   public void actionPerformed(ActionEvent arg0) {
      // called by Timer every 50 milliseconds
      for (SimpleSprite s : SimpleSprite.sprites)
         s.update(); // update positions of all sprites
      repaint();
   }

   @Override
   public void paintComponent(Graphics g) {
      super.paintComponent(g); // ensure background etc is painted
      Graphics2D g2d = (Graphics2D) g;
      for (SimpleSprite s : SimpleSprite.sprites)
         s.draw(g2d);// draw all sprites at latest positions
   }

   public static void main(String[] args) {
      // create and display the animation in a JFrame
       final JFrame frame = new JFrame("Animation 2 (close window to exit)");
     Animation2 animationPanel = new Animation2(600, 500);
      frame.add(animationPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      // add some sprites...
      new Square(0, 0, 3, 2, 40);
      new Ball(500, 0, -3, 3, 20);
      new Ball(0, 500, 2, -5, 30);
   }

}

abstract class SimpleSprite {
   // basic x,y movement,keeps a master list of Sprites

   public static final ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();

   float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)

   public SimpleSprite(float x, float y, float dx, float dy) {
      // initial position and velocity
      this.x = x;
      this.y = y;
      this.dx = dx;
      this.dy = dy;
      sprites.add(this);
   }

   public void update() { // update position and velocity every n milliSec
      // default - just move at constant velocity
      x += dx; // velocity in x direction
      y += dy; // velocity in y direction
   }

   abstract public void draw(Graphics2D g2d); 
      // just draw at current position, no updating.

}

class Ball extends SimpleSprite {

   int diameter;

   public Ball(float x, float y, float dx, float dy, int diameter) {
      super(x, y, dx, dy);
      this.diameter = diameter;
   }

   // no need to override update, constant velocity is OK

   @Override
   public void draw(Graphics2D g2d) {
      g2d.setColor(Color.blue);
      g2d.fillOval((int) x, (int) y, diameter, diameter);
   }
}

class Square extends SimpleSprite {

   int side;

   public Square(float x, float y, float dx, float dy, int side) {
      super(x, y, dx, dy);
      this.side = side;
   }

   @Override
   public void draw(Graphics2D g2d) {
      g2d.setColor(Color.red);
      g2d.fillRect((int) x, (int) y, side, side);
   }
}

Hi, I have rectangular sprites moving across the screen at the moment. How can I make them repeat after they go off screen?

Enhance the Sprite update method - check if the sprite has moved off-screen and reset its positiuon as required.

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.