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?


4 Years
Discussion Span
Last Post by JamesCherrill

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.


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.

Edited by JamesCherrill: ps


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();

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

   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);
      // 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;

   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

   public void draw(Graphics2D g2d) {
      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;

   public void draw(Graphics2D g2d) {
      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?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.