Determining a winner in java race car program
<p>I am trying to find a way to determine a winner and I am not having much luck. The program is suppose to run three laps and which ever car finish all the laps first is the winner. I can get 3 "laps" in but it is not a very good way of doing it. I am hoping someone can show me a better way and also how I can can "count" those laps for the specific winning car. The number of cars is random from 2 - 4 and the "speed" is also random. Can someone help me please. Some code would be nice.</p>

<pre><code>import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class RacingCar extends JFrame {

public RacingCar() {
    int x = (int)(Math.random() * 3) + 2;
    setLayout(new GridLayout(x, 1, 5,5));
    for (int i = 0; i &lt; x; i++){
            add(new CarImage());
    }
}

public static void main(String[] args) {
    JFrame frame = new RacingCar();
    frame.setTitle("Racing Car");
    frame.setSize(1200, 350);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

class CarImage extends JPanel {
    protected int x = 0;
    protected int y = 350;
    protected int z = 1200;
    protected int c = 0;

    public CarImage() {
        int j = (int)(Math.random() * 500) + 2;
            Timer timer1 = new Timer(j, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                x += 10;
                c ++;
                repaint();
            }
        });

        timer1.start();

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
            //x = 0;
            y = getHeight();
            z = getWidth();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, z, y);
            Polygon polygon = new Polygon();
            polygon.addPoint(x + 10, y - 21);
            polygon.addPoint(x + 20, y - 31);
            polygon.addPoint(x + 30, y - 31);
            polygon.addPoint(x + 40, y - 21);

            if (x &lt; z - 50) {
                g.setColor(Color.BLACK);
                g.fillOval(x + 10, y - 11, 10, 10);
                g.fillOval(x + 30, y - 11, 10, 10);
                g.setColor(Color.BLUE);
                g.fillRect(x, y - 21, 50, 10);
                g.setColor(Color.GRAY);
                g.fillPolygon(polygon);
                g.setColor(Color.RED);
            }
            else {
                x = 0;
                /*if (c &lt; z - 86) {
                    g.drawString("Clint's Car", c, y - 51);
                }
                else {
                    c = 0;
                }*/
            }
    }

}
}
</code></pre>

<p>What I did for the laps loop is this:</p>

<pre><code> if (k &lt; 341){
     repaint();
     k++;
 {
</code></pre>

<p>this loop was inserted at the end of:</p>

<pre><code>public void paintComponent(Graphics g) {
</code></pre>

<p>I really am stuck here. Thanks for all the help.</p>
