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 3 of 5 FirstFirst 12345 LastLast
Results 51 to 75 of 106

Thread: Car racing

  1. #51
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Car racing

    Quote Originally Posted by DanielVkc View Post
    How does it works..??
    Because I'm still a newbie and beginner around here..
    Did you click the link? An SSCCE is a short, self-contained, compilable example. That means, narrow your problem down to just a few lines- in this case, the displaying of an image. It should be one complete file, and it should not include any extra stuff other than displaying an image.

    Also, answer the questions you've already been answered- Norm is being extremely patient with you, and you should repay him by answering his questions as accurately as you can.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: Car racing

    Image Track = new ImageIcon("images/Track.gif").getImage();
    Image Car1 = new ImageIcon("images/Red Car.png").getImage();
    Image Car2 = new ImageIcon("images/Blue Car.png").getImage();

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

    Default Re: Car racing

    Quote Originally Posted by KevinWorkman View Post
    Did you click the link? An SSCCE is a short, self-contained, compilable example. That means, narrow your problem down to just a few lines- in this case, the displaying of an image. It should be one complete file, and it should not include any extra stuff other than displaying an image.

    Also, answer the questions you've already been answered- Norm is being extremely patient with you, and you should repay him by answering his questions as accurately as you can.
    Ya..ok..I'll try my best..
    I really appreciate for the help..
    As myself is also trying to learn something to gain knwoledge from here.

  4. #54
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    That's part one. Where is this part?

    Open a command prompt window, change to the folder with the images and enter the command: dir
    Copy and paste the contents here:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

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

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    That's part one. Where is this part?

    Open a command prompt window, change to the folder with the images and enter the command: dir
    Copy and paste the contents here:
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    How to change the folder with the images..??
    I'm in the command prompt window now..

  6. #56
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Car racing

    Quote Originally Posted by DanielVkc View Post
    How to change the folder with the images..??
    I'm in the command prompt window now..
    What did google tell you? Wanting to learn and get help is one thing, but you have to put an effort into figuring things out.

    cd Change Directory
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #57
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    In the command prompt window use the cd command to change directories.
    Here is a sample of its usage:
     
    D:\JavaDevelopment\Testing>cd ..
     
    D:\JavaDevelopment>cd Testing
     
    D:\JavaDevelopment\Testing>cd Security
    The system cannot find the path specified.
     
    D:\JavaDevelopment\Testing>cd images
     
    D:\JavaDevelopment\Testing\images>dir
     Volume in drive D is Work
     Volume Serial Number is 1463-F5AE
     
     Directory of D:\JavaDevelopment\Testing\images
     
    06/28/2010  02:21 PM    <DIR>          .
    06/28/2010  02:21 PM    <DIR>          ..
    04/14/2000  08:22 AM            19,189 AppletImg.jpg
    09/17/1999  11:30 AM            76,756 ApproachToProperty.jpg
    10/07/1998  04:20 PM            67,982 AreWeFriends.jpg
    10/05/1997  11:35 PM             4,584 CoreJava1.1.gif
    10/09/1997  10:05 PM            26,849 Core_Java.gif
    10/09/1997  10:05 PM            28,038 Core_Java_rotated.gif
    07/03/1997  02:49 PM             1,929 duke.gif
    01/24/2000  05:29 PM             1,287 extendingTrail.gif
    09/23/2003  01:39 PM            48,961 foxtrot.jpg
    10/09/1998  09:52 AM            66,336 JungleWalk.jpg
    07/27/2007  02:23 PM               172 left.gif
    07/27/2007  02:23 PM               235 middle.gif
    12/25/2001  09:33 AM             1,996 OneHundredBy.jpg
    07/27/2007  02:23 PM               172 right.gif
    06/29/2001  06:37 PM            27,959 worcester.jpg
                  15 File(s)        372,445 bytes
                   2 Dir(s)   1,407,864,832 bytes free
     
    D:\JavaDevelopment\Testing\images>

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

    Default Re: Car racing

    C:\Users\Daniel>cd Pictures
     
    C:\Users\Daniel\Pictures>dir
     Volume in drive C has no label.
     Volume Serial Number is 12F1-DFEB
     
     Directory of C:\Users\Daniel\Pictures
     
    18/07/2011  22:35    <DIR>          .
    18/07/2011  22:35    <DIR>          ..
    16/08/2009  15:52             8,289 'HE' is risen...jpg
    16/07/2011  21:42             1,151 Blue Car.png
    30/06/2011  20:46            65,421 Capture_1.PNG
    12/07/2011  19:57            83,231 Capture_2(1).PNG
    12/07/2011  19:53            93,833 Capture_2(2).PNG
    16/07/2011  21:42             1,146 Green Car.png
    29/06/2011  20:52    <DIR>          My lovely bebe
    16/07/2011  21:42             1,137 Red Car.png
    17/07/2011  15:21            20,388 Track.gif
    29/06/2011  20:52    <DIR>          ♥-?All Bout Me?-♥
                   8 File(s)        274,596 bytes
                   4 Dir(s)  29,999,800,320 bytes free
     
    C:\Users\Daniel\Pictures>

  9. #59
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    Your code has: " images/Track.gif"

    the image file location: C:\Users\Daniel\Pictures>

    These are not the same!!! They must be the same.

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

    Default Re: Car racing

    My files are :
    1. Blue Car.png
    2. Green Car.png
    3. Red Car.png
    4. Track.gif

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

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    Your code has: " images/Track.gif"

    the image file location: C:\Users\Daniel\Pictures>

    These are not the same!!! They must be the same.
    So how do I need to link them so that it can be the same..??

  12. #62
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    Your program must have the path to where the image files are located on your computer.
    You can use relative paths such as: images/<theImage>

    Either move the images or change the name of the folder, or change the code.
    Whatever you want to do so that the path in the program matches the path to the files on your computer.
    If you don't understand how the folders and files are arranged on your computer, there is nothing I can do to help you. This is a very basic concept: Folders and subfolders and files.

  13. The Following User Says Thank You to Norm For This Useful Post:

    DanielVkc (July 18th, 2011)

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

    Default Re: Car racing

    I modified everything already..All the images can be displayed now..thanks so much..

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

    Default Re: Car racing

    Since now everything can display already, how to make these cars to move along the track..??

  16. #65
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    how to make these cars to move along the track
    You'll some things for the cars to move.
    The first would be a timer that will change their x,y position every small unit of time.
    The second would be a definition of where the track is. This will require a lot of x,y positions to define the boundary of the track so you know when to go around curves etc.

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

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    You'll some things for the cars to move.
    The first would be a timer that will change their x,y position every small unit of time.
    The second would be a definition of where the track is. This will require a lot of x,y positions to define the boundary of the track so you know when to go around curves etc.
    Here is my latest modified 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("C:/Users/Daniel/Pictures/Track.gif").getImage();
    Image Car1 = new ImageIcon("C:/Users/Daniel/Pictures/Red Car.png").getImage();
    Image Car2 = new ImageIcon("C:/Users/Daniel/Pictures/Blue Car.png").getImage();
    Image Car3 = new ImageIcon("C:/Users/Daniel/Pictures/Green Car.png").getImage();
    animation = new RaceTrackPanel(this, Track, Car1, Car2, Car3);
    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(600,480 );
    f.show();
    }}
    class RaceTrackPanel extends JPanel
    {
    RaceTrack holder;
    Image Track, Car1, Car2, Car3;
    int Xpos, Ypos;
    public RaceTrackPanel(RaceTrack app, Image Track,
    Image Car1, Image Car2, Image Car3)
    {
    this.Track = Track;
    this.Car1 = Car1;
    this.Car2 = Car2;
    this.Car3 = Car3;
    setSize(600,250);
    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);
    g.drawImage(Car3, Ypos, Ypos +235, this);
    }}}
    Would you mind to help me with it..??
    Because this x,y positions I was not really good in doing it to move them around the curves..

  18. #67
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    How are you changing the x,y positions of the cars as they move?
    How are you defining the x,y positions of the curves

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

    Default Re: Car racing

    Quote Originally Posted by Norm View Post
    How are you changing the x,y positions of the cars as they move?
    How are you defining the x,y positions of the curves
    I defined it as Xpos =30 and Ypos = 30..
    After that I use the drawImage method to draw out the components of the cars and its x,y positions..
    This is the part I was very confusing while I was doing this project..

  20. #69
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    I think it might be useful for you in learning how to animate your program to look at one of the many bouncing ball programs that exist. Search on this forum or google for example codes that will help you understand how to use a timer and how to change the values of x,y to do the animation.

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

    Default Re: Car racing

    Do you have the source on it..??

  22. #71
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    Search on this forum or google for example codes

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

    Default Re: Car racing

    Or would you mind to instruct me directly using the modified code dat I just posted in post#66..??

  24. #73
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Car racing

    You need to concentrate on the animation part only. The rest of your code would be in the way.
    Start with using a Swing Timer to change the x,y positions of the images and then call repaint() to show the changes.

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

    Default Re: Car racing

    Mind to show me some examples on the timers with x,y positions especially for this car race application..??

  26. #75
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Car racing

    Quote Originally Posted by DanielVkc View Post
    Mind to show me some examples on the timers with x,y positions especially for this car race application..??
    That is absolutely not how this works. Norm has been kind enough to hold your hand up until this point, but you're going to have to show some effort if you want more help. What have you tried? What did google tell you? Where are you stuck? Where's your SSCCE?

    Game programming is not exactly trivial, and it really seems like you aren't ready for it. I highly recommend you go back and work through the basic tutorials before attempting any of this.

    Please read the link in my signature on asking smart questions before you post again.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Page 3 of 5 FirstFirst 12345 LastLast