Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Declaring a winner in a Race Car Program

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Declaring a winner in a Race Car Program

    Hello All!

    I have the following 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 < 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 = 150;
            protected int z = 300;
            protected int c = 0;
            protected int k = 0;
     
            public CarImage() {
                int j = (int)(Math.random() * 1000) + 2;
                    Timer timer1 = new Timer(j, new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                    x += 10;
                    c ++;
                    if (k < 341) {
                    repaint();
                    k++;
                    }
                    }
                });
                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 < 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;
     
                    }
            }
     
        }
    }

    I'm stuck for the time being. How would I declare a winner? What I mean is, there are 2 - 4 random cars being "generated." Each cars speed is randomly "generated." I am trying to figure out a way for a winner to be declared after 3 "laps."

    Would someone please show me what I can do? I appreciate it and thank you in advance?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    Where do you count the laps each car has made?
    Where do you record the x location of each car so that you know which car is in front?


    Most of the variables have poor non-descriptive names making the code hard to read and understand. j, c, k, y , z ???
    Last edited by Norm; September 28th, 2012 at 12:13 PM.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Quote Originally Posted by Norm View Post
    Where do you count the laps each car has made?
     
    if (k < 341) {
                            repaint();
                            k++;
    Is where I will get 3 "laps" based on the panel width of 1200.

    [QUOTE=Where do you record the x location of each car so that you know which car is in front?[/QUOTE]

    I do not


    [QUOTE=Most of the variables have poor non-descriptive names making the code hard to read and understand. j, c, k, y , z ???[/QUOTE]
    Thanks

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    Your code is very hard to read and understand. There are no comments and the variable names are not descriptive of what data they hold. The 341 is a complete mystery and will cause problems when the size of the JFrame is set to a different size to make for faster testing by making the laps shorter.
    If you want to stop the race after so many laps, you need to count the number of laps that the car has made so you know when the race is over.


    I suggest that you fix the code so that it is more easily understood.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Quote Originally Posted by Norm View Post
    Your code is very hard to read and understand. There are no comments and the variable names are not descriptive of what data they hold. The 341 is a complete mystery and will cause problems when the size of the JFrame is set to a different size to make for faster testing by making the laps shorter.
    If you want to stop the race after so many laps, you need to count the number of laps that the car has made so you know when the race is over.


    I suggest that you fix the code so that it is more easily understood.
    Thank You.

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Ok so here is some updated code But I am not see the flaw. It doesn't seem to stop after 3 laps. Can someone point out what might be wrong? I really appreciate it.
    Here is the updated code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.GridLayout;
    import java.awt.Polygon;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.Timer;
     
    public class RacingCar extends JFrame {
     
        private static boolean stopTheRace = false;
     
        public static void stopRace(){
            stopTheRace = true;
        }
     
        public static void startRace(){
            stopTheRace = false;
        }
     
        public static boolean isStopTheRace(){
            return stopTheRace;
        }
     
        public RacingCar() {
            int x = (int)(Math.random() * 3) + 2;
            setLayout(new GridLayout(x, 1, 5,5));
            for (int i = 0; i < x; i++){
                    add(new CarImage(i));
            }
        }
     
        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;
            protected int carId;
            protected int laps = 0;
     
            public CarImage(int id) {
                int j = (int)(Math.random() * 70) + 2;
                    this.carId = id;
                    final Timer timer1 = new Timer(j, new ActionListener(){
     
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            x += 10;
                            c ++;
                            String CarLane = (carId + 1)+"";
                            if (laps == 3){
                                RacingCar.stopRace();
                                JOptionPane.showMessageDialog(null, "Winner in Lane "
                                 + CarLane + " ", "Winner winner chicken dinner", 
                                 JOptionPane.INFORMATION_MESSAGE);
                            }
                            if (RacingCar.isStopTheRace()){
                                ((Timer)e.getSource()).stop();
                            }
                            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 < 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;
     
                    }
            }
     
        }
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    It doesn't seem to stop after 3 laps.
    Where are the laps counted?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Quote Originally Posted by Norm View Post
    Where are the laps counted?
    Yah good call. Gotta figure out how to count the laps. Not really sure how to go about it efficiently. I did have a loop with repaint() in it and it really did not seem efficient enough.
    It was:
    if (k < 341) {
         repaint();
         k++;
    }

    but that did not take into account if the window was resized. Any ideas?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    Where is the car when it completes a lap? Can you use its position to determine when it has completed a lap?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Quote Originally Posted by Norm View Post
    Where is the car when it completes a lap? Can you use its position to determine when it has completed a lap?
    Yes. Since the panel width is "z = 1200", I assume one lap would roughly be less than a panel width. Is that not correct?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    "z = 1200"
    Sorry, I don't know what the variable z contains or what the meaning of 1200 is.
    A better name for the variable would explain what data z contains.
    The length of the race track should be held in a variable and not hardcoded as 1200.

    Perhaps something like this:
    carsPosition >= trackLength
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Declaring a winner in a Race Car Program

    Quote Originally Posted by Norm View Post
    Sorry, I don't know what the variable z contains or what the meaning of 1200 is.
    A better name for the variable would explain what data z contains.
    The length of the race track should be held in a variable and not hardcoded as 1200.

    Perhaps something like this:
    carsPosition >= trackLength
    Ok so we can go along those lines, but then that begs the question, if a user desides to change the size of the frame, how does one know the size of the frame? What would be a value of that or better yet, how do I determine the value of that?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Declaring a winner in a Race Car Program

    how does one know the size of the frame?
    The JFrame class (and other classes) has methods that give its current size.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Declaring your own exceptions
    By weakprogrammer in forum Exceptions
    Replies: 4
    Last Post: June 30th, 2011, 08:19 AM
  2. Declaring a date
    By Akim827 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 12th, 2010, 11:47 AM
  3. <..> when declaring a class
    By Asido in forum Java Theory & Questions
    Replies: 1
    Last Post: September 8th, 2010, 08:47 AM
  4. Need help with Assignment. [Car race]
    By jeanfrg in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 17th, 2010, 07:02 AM
  5. Replies: 4
    Last Post: June 5th, 2009, 06:11 AM