Hello
I am a student of computer science year 3 and am doing my project on a road design simulator. I should be able to design a road on a java interface and simulate it with cars. Does anyone have an idea about how should i proceed with it?

Recommended Answers

All 14 Replies

The most important part I think: 2D or 3D?

It is also important to know exactly what you want to simulate. When you say "road design" it makes me think you are talking about traffic flow, meaning that if you give the simulator a map of roads including road signs and the number of lanes for each road, and then give it the amount of traffic going from various sources to various destinations, it will tell you how quickly traffic is getting where it needs to go and where the bottlenecks are. If that is what you want, then you need to decide so and your first step is doing some research into how traffic flows.

Another possibility is that you are working on simulating the pavement of a road, and how it deteriorates under traffic. There are various ways to construct a road and knowing when potholes will develop and when road repairs will likely be necessary might be useful. In that case the first step is also to research how roads deteriorate.

I can also imagine you might be wanting to simulate how a racing car can drive along a road. You might want to know something about maximum speeds and find the places on course with the greatest danger of losing control. In that case you need to know about the physics of racing cars.

i think it's a big thing...you are doing..You have to think lots of think when you are making simulator for road design.

For gui 2d and 3d objects(Like car,road,vehicle-heavy and normal,lanes,traffic signals) you need.

Also so many data configurations is also required as which lane has capacity of how many vehicles,what is the maximum/minimum vehicle speed for that lane,traffic signal change at every how much minute...and so on..?

It's really good project.i appreciate this kind of project...

well my project is a 2D project
first of all i need to draw a road
and simulate it with cars
any one has an idea how to do it?

Yes, lots of ideas, but your requirement is still far to vague for anyone to give a proper reply. What kind of simulation do you need for the cars? In how much detail (see previous posts)?
(In 1971/2 I worked on an IBM project to simulate the traffic flow in central Glasgow (a city in Scotland) - it took us more than 20 man years programming effort, excluding setting up the actual road definition data, and with just a command-line user interface. How far do you want to go with yours?)

Does "draw a road" mean just a couple of lines on the screen, or a full photo-realistic aerial view with the trees moving in the wind and people walking past, or what?

If you are in the third year of a CompSci degree then you should have a good idea of how to specify requirements for a program.

ok
Let me define it to you properly
The aim of my project is to be able to design only roads using graphics 2D on a GUI interface, not with a view of trees etc...
I should be able to cater for a one way road and a two lane road.
For example: when i click on a button "Road", it should allow me to draw on my interface. It should also allows me to extend my road such that i can enlarge it or increase its length.
for the simulation part, the aim is to generate a number of vehicles by using a time-based generation of vehicle and to simulate it on the roads that i have previously designed on my interface.
:)

You can do your basic drawing by extending JPanel with a custom paintComponent that draws the roads according to a model that defines their number oflanes, start & end copordinates etc, and draws the cars according to a car model that defines where the cars are and which way they are heading etc. Thew sophisication of the drawing is up to you - maybe start very simple (eg car = rectangle) and enhance later (eg car = jpeg image) if you have time.
You can use a MouseListener and a MouseMotionListener on that panel to detect clicks and drags that you can interpret as adding or modifing the model.
If I were doing this I would start with the model and just use hard-coded test data and print statements to confirm that the model correctly stores, updates and delivers all the data you need. Only then would I build the GUI, knowing I had a solid base to build from, and a sensible model/view architecture.

Until you have a class design for the model and some GUI sketches it's hard to be more specific.

I can not use the paint component.

You don't know how to, OR you're not allowed to?

I am not allow to use paint component..
i am trying it through a Jpanel..:)

Perhaps you misunderstood me? I was referring to the paintComponent(Graphics g) method for the JPanel, not to any stand-alone component that does painting. Capitalisation is everything in Java (eg JPanel, not Jpanel)

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;


public class Main extends JPanel
{
    private Graphics2D screen_graphics;
    private BufferedImage image;
    private Rectangle interface_rectangle;

    int width, height;
    int gridsize = 10;
    BasicStroke normal = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL);

    public Main()
    {
        super();
        //this.width = 1000;
        //this.height = 1000;   

        Dimension d = getSize();

        width = d.width;
        height = d.height;

        //Render images not only on screen
        //An image itself can be considered as a drawing surface
        //Use createGraphics() method of the BufferedImage class for this purpose
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        screen_graphics = (Graphics2D)(image.getGraphics());

        //Perform rendering and image manipulation
        screen_graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);  

        interface_rectangle = new Rectangle(gridsize, gridsize,
                                           ((width - gridsize * 2) / gridsize) * gridsize,
                                           ((height - gridsize * 2) / gridsize) * gridsize);                      
    }

    public static void  main (String args[])
    {
        JFrame f = new JFrame();
        Main m = new Main();
        f.setContentPane(m);
        f.setVisible(true);
        f.setSize(600,600);
    }
}

why is it such that my rectangle is not appearing on my interface when i run this code?

Because you are missing public void paintComponent(Graphics g) method inside your JPanel class? No paint, no display. You should reread what James said. You could search on Google for a sample graphic 2D drawing in Java.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main_Design extends JPanel 
{
    private Graphics2D gridGraphics;
    private BufferedImage gridImage;
    int  gridspace = 8;
    int width, height;
    private Rectangle drawingRectangle;

    public Main_Design()
    {
        this.width=1000;
        this.height=600;
        setBackground(Color.white);          
    }

    public void paint(Graphics g) 
    {
        super.paint(g);

        int h = getHeight();
        int w = getWidth();

       Dimension dimension = getSize();

       int height1 = dimension.height;
       int width2 = dimension.width;


        Graphics2D g2d = (Graphics2D)g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2d.translate(25, 5);

        drawingRectangle =
                new Rectangle(gridspace, gridspace,
                ((width2 - gridspace * 6) / gridspace) * gridspace,
                ((height1 - gridspace * 4) / gridspace) * gridspace);

        g2d.setColor(Color.BLACK);
        g2d.setStroke(new BasicStroke(6));
        g2d.draw(drawingRectangle);
        GridLayout grid = new GridLayout(3,4);

        paintGrid();
        g2d.drawImage(gridImage, width, height,this);    
    }

    public void paintGrid() 
    {        
        width = (getWidth()-1)/gridspace;
        height = (getHeight()-1)/gridspace;

        gridImage = (BufferedImage) createImage(width, height);

        gridGraphics = (Graphics2D) gridImage.createGraphics();
        gridGraphics.setColor(Color.white);
        gridGraphics.fillRect(0, 0, width, height);

        gridGraphics.setColor(Color.GRAY);
            for (int i = 0; i <= width; i++) 
            {
                int x = i * gridspace;
                gridGraphics.drawLine(x, 0, x, height*gridspace);
            }

            for (int i = 0; i <= height; i++) 
            {
                int y = i * gridspace;
                gridGraphics.drawLine(0, y, width*gridspace, y);
            }
    }

    public Dimension getPreferredSize() 
    {
            return new Dimension(width, height);
    }

    public static void main(String[] args) 
    {
        JFrame frame = new JFrame("Main");
        frame.setBackground(Color.white);
        frame.add(new Main_Design());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(350, 250);
       // frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

helloo
i have tried to put a grid in my background using the function paintGrid
but nothing is appearing when am running this code
anyone knows why?

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.