1 Attachment(s)
Creating basic road for traffic simulator
Hi
I need to create a cellular automation model of traffic flow. The program will have a 1D road which will contain a user entered amount of cars which are represented on the road as a 1 or 0.
I need to know how to use the paint() method to create a rectangle with lines which seperate it into cells, I attatched an image of what I mean.
Also, how would I populate each cell with a number? I have a method which has an int array which represents the road and then a forumula which calculates each value ( 1 or 0) for the cell of the road but I do not know how to display this information using paint().
Thanks
Re: Creating basic road for traffic simulator
You can use a Graphics2D object to draw a grid. Then use a 2D array of boolean for your 1 or 0 (unless you might need more then that, perhaps different numbers represent different vehicles). Then you can use the Graphics2D object to paint what ever you want based on the value of the 2D boolean array. You can work it so that the grid and the "cars" are painted in the panel based on a math calculation using the panels Dimension. In any case you will need to override the paintComponent() method of the panel you are painting to. Seems like a fun project. =D What's it for?
Re: Creating basic road for traffic simulator
Ok, I think I know what you mean. I managed to get the grid I wanted with Graphics2D.
This is an assignment I have to complete for a university course in Computer Simulation.
Thanks for your help
Re: Creating basic road for traffic simulator
Or you could use a GridLayout with 1 row and n columns (assuming you want a horizontal display) and fill it with JLabels set to centre their text. If you add a line border to each JLabel you should be able to get the effect of a grid filled with numbers. You might want to also add a line border to the panel containing the grid just to finish things off neatly.