CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2010
    Posts
    14

    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
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2011
    Location
    Tacoma, Washington
    Posts
    31

    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?

  3. #3
    Join Date
    Sep 2010
    Posts
    14

    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

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured