Hi!

I'm a newbie in drawing graphics with Java and have another problem with my traffic Jam simulation:
At the end of the simulation the program should draw the finalA array as a series of filled/unfilled squares. All I get is a white window coming up:@
I will post all my code so that you can try to compile it and see what it does. The issue with the do-while loops hadn't been yet quite resolved but the program creates the road in most cases.

And if someone is really good, would you have an idea how to animate the drawing so that for each t in flowSimuator method a new array with the filled/unfilled squares would be redrawn, say every 3s....

If anyone can help me I'll be very grateful as I spent so much time on this program, got stuck at this point and don't know what to do now:'(

Here are my classes:

``````class Road {

private int n;
private int c;
private int allocated;
private int assigned;

/// constructor for road taking length and number of cars putting them down randomly
public Road(int n, int c){

this.road = new int[n];
/// if c>(n/2) then allocate 0s, if c<=n/2 allocate 1s

if(c < (n/2)){

///   do{
int allocated = 0;
for(int i = 0; ((allocated < c) && (i < n)); i++){
allocated = allocated +1;
}
}
///	System.out.println("T "+ allocated +"  " + c);
///	    }
///    while(allocated < c);

}

if(c >= (n/2)){
///    do{
int assigned = 0;
for(int i = 0; ((assigned < (n-c)) && (i < n)); i++){
if(this.road[i] == 0) {
assigned = assigned +1;

}
}
///   }
///  while(assigned < (n - c));
}
}

/// constructor for road out of array
for(int i=0; i < ROAD.length; i++){
}
}

/// method to return 0 or 1 randomly

private int binaryGenerator(){
double y = Math.floor(Math.random()*2);
int x = (int)y;

return x;
}

/// getter method for array
public int[] Array(){
}

public static int[] flowSimulator(Road A, int time, int c){

int road[] = new int[A.Array().length];
int totalmoved = 0;
double avVel = 0;
/// time - set number of iterations
for(int t = 1; t < time + 1; t++){
int movedcars = 0;
/// checking from end: if car can move forward using modulo for each space,if a car can move change its value to 2 but don't move it
for(int i=0, j = road.length-1; i < road.length; i++, j--){
movedcars = movedcars +1;
}
else {
}

}

}

totalmoved = totalmoved + movedcars;
double speed = 0;
speed = (double)movedcars/(double)c;
System.out.println("Speed for time " + t+" is "+ speed);

/// now move the cars to the final positions after the iteration
for(int i=0; i < road.length; i++){
}
}
}
/// calculate average velocity
avVel = (double)totalmoved/(double)(time*c);
System.out.println("The averege velocity of cars in this time interval was " + avVel);
}
}``````

And TrafficFlow.java

``````import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

class RoadPanel extends JPanel {

private int W;
private int H;
/// creator for Road panel
public RoadPanel(int W, int H, int[] A) {
this.W = W;
this.H = H;
setBackground(Color.white);
}

/// override  paint component to create road
public void paintComponent(Graphics a,Graphics g){
/// colour background white
g.setColor(Color.white);
super.paintComponent(g);
g.setColor(Color.black);
a.setColor(Color.yellow);
/// middle of car in the middle of the road. Car - square 20x20 pixels. Filled cars will be yellow, empty spaces black border
int x,y,w,h;
w = 20;
h = 20;
y = ((H - h)/2);
for(int i = 0; i < road.length; i++){
x = i * w;
if( road[i] == 0){
g.drawRect(x,y,w,h);
}
if( road[i] == 1){
a.fillRect(x,y,w,h);
}
}
}
}

class RoadFrame extends JFrame {

static final int W = 800;
static final int H = 120;
public RoadFrame(int[] A) {

setTitle("Traffic at end at end of time");

/// add panel to frame
/// Window closing option
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
///size of frame
setSize(W,H);
}
}

/// MAIN program start

class TrafficFlow {

public static void main (String argv[]) throws IOException {

/// try-catch block to handle exceptions
try{

System.out.println("Input length of road between 1 to 60");
double n = new Double(keyboard.readLine()).doubleValue();
if((n < 1) || (n > 60)){
System.out.println("Invalid imput, simulation terminated");
System.exit(1);
}

System.out.println("Input density of traffic from 0 to 1");
double density = new Double(keyboard.readLine()).doubleValue();
if((density < 0) || (density > 1)){
System.out.println("Invalid imput, simulation terminated");
System.exit(1);
}

double c = n*density;

System.out.println("Input the time of simulation");
int time = new Integer(keyboard.readLine()).intValue();

if(time < 1){
System.out.println("Invalid imput, simulation terminated");
System.exit(1);
}

int cram[] = cramped.Array();
// perform simulation

int finalA[] = new int[cram.length];
finalA = Road.flowSimulator(cramped, time,(int)c);

F.setVisible(true);
}
catch (Exception ex)
{
System.out.println("You typed some invalid input.");
}

}

}``````

Well, the first thing you need to fix is your paintComponent() method. If you override that method, you cannot arbitrarily change its signature. It must match the original method, which this does not

``public void paintComponent(Graphics a,Graphics g){``

It should be

``public void paintComponent(Graphics g){``

The method you have written is not getting called at all because it is not declared correctly.

Correct that method declaration and change all "a" references to "g" and you will see the result of your painting code.