Hello.
I'm trying to create a traffic simulator using java applet. I've seen a couple of source codes and to be honest, I'm just clearly confused on how to go about it.
What I've done so far is paint the image of the road junction that I am trying to run the cars on. So i've got an image of a 4 way cross junction.

How do I simulate a car object to move on a specific road? I know that I would need a Car class.

I'm not asking for the code itself, just an understanding on how to go about doing it.
Thanks.

sounds want to build a big graph basically, each "node" is a junction (and has certain properties like throughput ect) and each "connection" is a road, which has a certain length associated with it etc

Your cars could be "stored" on the roads, with junctions and length determining how long they stay at that specific junction before being allowed to pass through a junction onto a different road... If you are taking into account que size, basically a saturation of the road that affects other roads then things get more complicated...

depends on how you want to do it.

unless you are making a visual simulation (I took it as a kind of statistical stress program that can tell you where there is a large amount of traffic, not draw pretty cars moving arround)

You can do simple animation in Java by using a Timer object (from the swing package, not the util package). You give it a delay (in milliseconds) and an ActionListener, and then you start the timer. After that, every time the timer goes off, the actionPerformed method in the ActionListener is called.

So to animate a car, for example, in your Car class you would maintain variables for the x & y coordinates of the car that get used when the car is drawn (using paint or paintComponent). Then in the actionPerformed method, you would update those variables and then repaint the car. This is the basic idea.

unless you are making a visual simulation (I took it as a kind of statistical stress program that can tell you where there is a large amount of traffic, not draw pretty cars moving arround)

Haha. Yeah, I want to draw pretty cars moving around :P, rather than the boring graph :)

Thanks for the ideas guys. Appreciate it. I'll try using the timer object.

Well kind of like a game then, but without input?

Id make a big canvas (Jpanel?) with custom drawing and a runnable interface with the old 60 fps drawing loop

car objects extend some custom drawable interface to draw splodges where their position is, hava an array of everyhting drawable and each frame loop through and call the interface draw method Take a look at some java game examples, gut them for their runable interfaces/double buffering etc

reminds me of things i used to do in basic when i was a kid (i made a plant - sheep - wolf ecosystem simulator, it was allways unstable and they all died in the end :( )

Hmm. There won't really be much interaction from the user other than clicking on checkboxes which would ideally affect the simulation in one way or another. I'm pretty much a beginner in java also.
What do you think is easier? Animation with the timer object or as you mentioned, the game approach?

I think the timer approach is simpler just to get a few cars moving around the screen. But if the UI gets complicated, there might be some lags in repainting the components. So it depends on how slick you want to get and how many objects will be moving around.

Well, I have no experience with the timer approach. The game approach may be faster (ie you could have more cars), but probably harder? And you wouldn't interact much with Java UI building.

I suggest writing a tictactoe game with JPanels, get used to Java a little before doing someting with full on animation (it shouldnt take long)

I started Java programming by writing games, and I didnt understand alot of what was going on Java-wise (I wasn't sure how to use JFrames/panels or anything very well) But you get good at other things...

I first used java about 2 years ago and havent done much since. I also started making a game, but likewise, I didn't really learn much java.

I've got about 2 and a bit months to make this traffic simulation. So whatever I do, I need to do it fast lol. I'll try the tictactoe game.

Thanks again.

tictactoe games are good to learn about event handeling and JPanel interactions and UI stuff (JButtons etc), it really depends on what you want to learn, it also introduces things like file saving (scores?) and basic logic ( if statements for loops arrays etc) without you havning to do custom animation, rendering and thread/loop control - If you want to rush past all the UI stuff though you can! Go straight to making a custom drawing panel etc

I just had a quick read through the following tutorial for tictactoe and it was very easy to understand.

http://forum.codecall.net/java-tutorials/2141-java-tutorial-tic-tac-toe.html

I'm not new to programming as a whole and I understand how to use loops etc. I was just confused on the approach to take with regards to the traffic simulator.

If i were you I would go for an animated loop, sort out some custom drawing in a JFrame, then get a runnable interface and do a loop

http://www.gamedev.net/reference/articles/article1360.asp seems to cover all you need for a basic game loop

If i were you I would go for an animated loop, sort out some custom drawing in a JFrame, then get a runnable interface and do a loop

Ok, I'll try that. I'll get a start on it. Thanks for all your help.

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.