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.

Page 1 of 5 123 ... LastLast
Results 1 to 25 of 106

Thread: Car racing

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Car racing

    I was working on this project of car race but I can't display the track eventhough I have declared it..anybody know what is the problem??And did anyone know how to do laps setting and time remaining for car race..??I really need a help..
    Here is the code:
    import javax.swing.*;
    import java.awt.geom.*;
    import java.awt.*;
    public class RaceTrack extends JFrame
    {
    private RaceTrackPanel animation;
    private JLabel betLabel, potLabel, bankLabel, carLabel;
    private JTextField betField, carField, potField, bankField, winnerField;
    private JButton goButton;
    private JPanel p;
    public RaceTrack( )
    {
    Image Track = new ImageIcon("Track.gif").getImage();
    Image Car1 = new ImageIcon("Redcar.gif").getImage();
    Image Car2 = new ImageIcon("Bluecar.gif").getImage();
    animation = new RaceTrackPanel(this, Track, Car1, Car2);
    Container c = getContentPane();
    c.setLayout(new BorderLayout( ));
    p = new JPanel( );
    p.setBackground(Color.lightGray);
    GridBagLayout grid = new GridBagLayout();
    p.setLayout(grid);
    betLabel = new JLabel("Enter Bet");
    carLabel = new JLabel("Choose Car");
    potLabel = new JLabel("Pot");
    bankLabel = new JLabel("Bank");
    betField = new JTextField(5);
    carField = new JTextField(2);
    potField = new JTextField(5);
    bankField = new JTextField(7);
    winnerField = new JTextField(20);
    potField.setEditable(false);
    bankField.setEditable(false);
    goButton = new JButton("Start Race");
    GridBagConstraints gbc = new GridBagConstraints( );
    gbc.fill = GridBagConstraints.BOTH;
    gbc.insets = new Insets(5,5,5,5);
    gblAdd(goButton,grid,gbc,0,0,4,1);
    gbc.fill = GridBagConstraints.NONE;
    gbc.anchor = GridBagConstraints.EAST;
    gblAdd(betLabel,grid,gbc,0,1,1,1);
    gbc.anchor = GridBagConstraints.WEST;
    gblAdd(betField,grid,gbc,1,1,1,1);
    gbc.anchor = GridBagConstraints.EAST;
    gblAdd(carLabel,grid,gbc,2,1,1,1);
    gbc.anchor = GridBagConstraints.WEST;
    gblAdd(carField,grid,gbc,3,1,1,1);
    gbc.anchor = GridBagConstraints.EAST;
    gblAdd(potLabel,grid,gbc,0,2,1,1);
    gbc.anchor = GridBagConstraints.WEST;
    gblAdd(potField,grid,gbc,1,2,1,1);
    gbc.anchor = GridBagConstraints.EAST;
    gblAdd(bankLabel,grid,gbc,2,2,1,1);
    gbc.anchor = GridBagConstraints.WEST;
    gblAdd(bankField,grid,gbc,3,2,1,1);
    gbc.anchor = GridBagConstraints.WEST;
    gblAdd(winnerField,grid,gbc,0,3,4,1);
    betField.setText("");
    potField.setText("100");
    bankField.setText("1000");
    c.add("Center", animation);
    c.add("South", p);
    }
    private void gblAdd(Component c, GridBagLayout
    grid,GridBagConstraints gbc, int x, int y, int w, int h)
    {
    gbc.gridx = x;
    gbc.gridy = y;
    gbc.gridwidth = w;
    gbc.gridheight = h;
    p.add(c);
    }
    public static void main(String[] args)
    {
    JFrame f = new RaceTrack( );
    f.setSize(700, 400);
    f.show();
    }}
    class RaceTrackPanel extends JPanel
    {
    RaceTrack holder;
    Image Track, Car1, Car2;
    int Xpos, Ypos;
    public RaceTrackPanel(RaceTrack app, Image Track,
    Image Car1, Image Car2)
    {
    this.Track = Track;
    this.Car1 = Car1;
    this.Car2 = Car2;
    setSize(600,270);
    setBackground(Color.darkGray);
    holder = app;
    Xpos = 30;
    Ypos = 30;
    }
    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    int w;
    int h;
    w = Track.getWidth(this);
    h = Track.getHeight(this);
    if ((w>0) && (h>0))
    {
    g.drawImage(Track, Xpos, Ypos, this);
    }
    w = Car1.getWidth(this);
    h = Car2.getHeight(this);
    if ((w>0) && (h>0))
    {
    g.drawImage(Car1, Xpos, Ypos +25, this);
    g.drawImage(Car2, Ypos, Ypos +130, this);
    }}}


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

    Default Re: Car racing

    I can't display the track eventhough I have declared it
    To see a component in a GUI display you need to add that component to a container that is being displayed with properties that the layout manager can use to position it and display it.
    Just declaring it will not display it.

    Please wrap your posted code in code tags. See: BB Code List - Java Forums

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Mind to help me identify which part is the error..??Beacuse I'm just a begineer to java..So I need your help to clarify me on which part..

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

    Default Re: Car racing

    Please explain what the error is.
    When I compile and execute the program (using some of my images) the 3 images are displayed.

  5. #5
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    I compiled and executed the program..but it can't display the track eventhough I have save it to my build folder..

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    it can't display the track
    What is "the track"?
    There are three images referred to in the program. Is it one of those?
    Do any of the images display? What is different between the ones that display and the ones that do not display?

  7. #7
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Or would you mind to send the 3 images for me..??

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    The images I use are some I have. One is a picture of a field, one is left button and one is a right button.

  9. #9
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    The images can't be displayed..but my Track.gif was in the correct format....

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Can you display any of the images with any program? Which ones display and which ones do not?

    Do any of the images display in your program?

  11. #11
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    I see..because my 3 images consist of a track and 2 other cars..

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Can you display any of the images with any program? Which ones display and which ones do not?

    Do any of the images display in your program?

  13. #13
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    The track can't be displayed..for the cars,I'm still searching but it's hard to find the cars that suit the track..

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Sorry, I can not help you find the images you need.
    When you get the images you need for your program, then we can help you fix your program to do what you want.

  15. #15
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Track.jpg

    This the track image..

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Why do I need the track image?

  17. #17
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Blue Car.pngGreen Car.pngRed Car.png

    These are the 3 cars images..
    My program is 3 cars racing on the track with how many laps it was selected then their score are stored to the client/server created..
    By the way, thanks for helping me..really hope for your early reply once the code was done..really thanks so much..

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    If you have all three images, please explain to me what your problem is.

  19. #19
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    Because I taught that I want to used the 3 cars to run on the track..

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    Can you explain what the problem with your current program is?

  21. #21
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    After I executed the program none of the images was displayed..

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    none of the images was displayed..
    Then the images are not in the correct folder.
    If you are using an IDE I can't help you.

  23. #23
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Car racing

    This is like some kind of puzzle game where you have to figure out the OPs problem from a few vague answers...

    Am I right in thinking that you actually have three images, of a track and two cars (which you don't like), and your program displays the track image, but it doesn't display the two car images, and if your code was written properly it would display them?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Car racing

    I think it is an IDE configuration problem. The OP mentioned a "build" folder.
    He needs to find out where the IDE has the current directory when it executes and put the images there.

  25. #25
    Member
    Join Date
    Jul 2011
    Posts
    54
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Car racing

    To Norm : So what software are you using to did for this program..??
    To dlorde : Can you help me to look into the code thoroughly to see what is the problem..??

Page 1 of 5 123 ... LastLast