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 2 of 5 FirstFirst 1234 ... LastLast
Results 26 to 50 of 106

Thread: Car racing

  1. #26
    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 use javac to compile and java to execute my programs.
    For simple testing I put all the files in one or two folders.
    For forum programs the .java and .class files are in the current folder and the images are in a subfolder: images.

    Image car1 = new ImageIcon(" images/right.gif").getImage();

    Ignore the leading space, I had to add because of the forums formatting
    Last edited by Norm; July 17th, 2011 at 11:59 AM.

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

    Default Re: Car racing

    Norm, according what you have stated just now, all the images I put in the build folder of IDE..for what I know is that all the images all placed in the build folder of IDE then it can display all of the images..

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

    Default Re: Car racing

    Norm, I do have another program of 2D racing that I just did..How to program the number of laps and time remaining?
    Here is the code :
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    import java.util.*;
     
    class Car{
        Image img;
        int x, y;
        Dimension dim;
        Car(Image img){
        this.img = img;
      }
      Car(Image img, int x, int y){
        this.img = img; 
        this.x = x;
        this.y = y;
      }
      Car(Image img, int x, int y, Dimension dim){
        this(img, x, y);
        this.dim = dim;
      }
      void draw(Graphics g, ImageObserver observer){
        g.drawImage(img, x, y, observer);
      }
      int getX(){ return x;}
      int getY(){ return y;}
      void setX(int x){this.x = x;}
      void setY(int y){this.y = y;}
      void setLocation(int x, int y){
        this.x = x; this.y = y;
      }
      int getWidth(){
          return img.getWidth(null);
      }
      int getHeight(){
          return img.getHeight(null);
      }
      Rectangle2D getRectangle(){
        return new Rectangle2D.Float(x, y, getWidth(), getHeight());
      }
      void move(int dx, int dy){
        x += dx;
        y += dy;
        if(dim != null){
          if(x < 0)x = 0;
          if(x + getWidth() > dim.getWidth())
            x=(int)dim.getWidth() - getWidth();
        }
      }
      boolean intersects(Car car){
        return getRectangle().intersects(car.getRectangle());
      }
      boolean intersects(int x, int y){
        return getRectangle().intersects(x, y, getWidth(), getHeight());
      }
    }
    public class CarRace extends Applet implements KeyListener, Runnable{
            Image buff;
            Canvas screen;
            Graphics2D gs, gb;
            Car redCar;
            Car[] enemy = new Car[20];
            Button bStart;
            Button bFinish;
            Thread game;
            boolean loop = true;
            Dimension dim = new Dimension(200, 300);
            int road;
            Random rnd = new Random();
            public void init(){
                prepareResource();
                setBackground(Color.cyan);
                initScreen();
                add(screen);
                bStart = new Button("Start Game");
                add(bStart);
                bStart.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ae){
                        screen.requestFocus();
                        if(!game.isAlive())
                            game.start();
                        }
                    });
                bFinish = new Button("End Game");
                add(bFinish);
                bFinish.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent ae){
                        screen.requestFocus();
                        if(!game.isAlive())
                            game.stop();
                        }
                    });
      }
      void prepareResource(){
        Image imgRed = getImage(getCodeBase(),"Red Car.png");
        Image imgBlue = getImage(getCodeBase(),"Blue Car.png");
        Image imgGreen = getImage(getCodeBase(),"Green Car.png");
        MediaTracker mt = new MediaTracker(this);
        try{
          mt.addImage(imgRed, 0);
          mt.addImage(imgBlue, 1);
          mt.addImage(imgGreen, 2);
          mt.waitForAll();
        }catch(Exception ex){
        }
        buff = createImage((int)dim.getWidth(), (int)dim.getHeight());
        gb = (Graphics2D)buff.getGraphics();
        redCar = new Car(imgRed, 80,250, dim);
     
        for(int i = 0;i < 10;i++){
           enemy[i] = new Car(imgBlue, 0, 0);
        }
        for(int i = 10;i < enemy.length;i++){
           enemy[i]=new Car(imgGreen, 0, 0);
        }
        for(int i = 0;i < enemy.length;i++){
           setEnemy(i);
        }
        game = new Thread(this);
      }
      public void stop(){
        loop = false;
      }
      public void run(){
        while(loop){
           drawScreen();
           try{
               Thread.sleep(50);
           }catch(Exception e){
           }
        }
      }
      void initScreen(){
        screen = new Canvas(){
           public void paint(Graphics g){
             if(gs == null){
               gs = (Graphics2D)screen.getGraphics();
             }
             drawScreen();
           }
        };
        screen.setSize(dim);
        screen.addKeyListener(this);
      }
      void setEnemy(int en){
        int x, y;
    next:while(true){
           x = rnd.nextInt((int)dim.getWidth() - enemy[en].getWidth());
           y =- rnd.nextInt(5000) - 200;
           for(int j = 0;j < enemy.length;j++){
             if(j != en && enemy[j].intersects(x, y))continue next;
           }
           enemy[en].setLocation(x, y);
           break;
        }
      }
      void check(Car en){
          if(redCar.intersects(en)){
             if(redCar.getX() > en.getX()){
               en.move(-20, 0);
               redCar.move(20, 0);
             }
             else{
               en.move(20,0);
               redCar.move(-20, 0);
             }
          }
      }
      synchronized void drawScreen(){
        gb.clearRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
        gb.setPaint(new Color(100, 100, 100));
        gb.fillRect(0, 0, (int)dim.getWidth(), (int)dim.getHeight());
        drawRoad();
        for(int i = 0;i < enemy.length;i++){
           enemy[i].move(0, 15);
           enemy[i].draw(gb, screen);
           if(enemy[i].getY() > dim.getHeight())
             setEnemy(i);
           check(enemy[i]);
        }
        redCar.draw(gb, screen);
        gs.drawImage(buff, 0,0, screen);
      }
      void drawRoad(){
        road += 80;
        gb.setPaint(Color.yellow);
        gb.fillRect((int)dim.getWidth()/2, road,10,150);
        if(road >= dim.getHeight())road =- 150;
      }
      public void keyPressed(KeyEvent ke){
        if(ke.getKeyCode()==KeyEvent.VK_LEFT){
           redCar.move(-10,0);
        }
        else if(ke.getKeyCode()==KeyEvent.VK_RIGHT){
           redCar.move(10,0);
        }
      }
      public void keyReleased(KeyEvent ke){
      }
      public void keyTyped(KeyEvent ke){
      }
    }

  4. #29
    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 have no idea what your IDE does. I don't have an IDE with a build folder. I have a folder where the .java file is located and where I execute the javac and java commands.
    If you open a command prompt window, move to the folder with the .class files and the image files and issue the command: java RaceTrack
    What happens?

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

    Default Re: Car racing

    Wait a sec..do you mean that you use a notepad to create the files including the compiler batch..??

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

    Default Re: Car racing

    Norm, mind to help me solve the problem for my 2nd car race program that I just only posted..??
    Because I was planning to set the laps and time remaining and then stores the scores in the client/server..

  7. #32
    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

    No, I use a mini-IDE that has syntax coloring and commandline tools that I can set to issue any commandline I want.
    The most used ones use javac and java. I have 4 different versions of javac and java that I can use: 1.4, 1.5, 1.6 and 1.7.

  8. #33
    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

    Quote Originally Posted by DanielVkc View Post
    To dlorde : Can you help me to look into the code thoroughly to see what is the problem..??
    Well I asked a question earlier to try to clarify what your problem is, and you didn't answer it, so I don't really think I know enough to be able to help.

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

    Default Re: Car racing

    I see.
    Because I was instructed to use Netbeans IDE 6.9.1 for my project.
    Can you help me take a look at the program that I have just posted just now?

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

    Default Re: Car racing

    Quote Originally Posted by dlorde View Post
    Well I asked a question earlier to try to clarify what your problem is, and you didn't answer it, so I don't really think I know enough to be able to help.
    The problem earlier is that all my images including the track and my cars can't be displayed..it just displayed the background color and the labels with textfields below it..

  11. #36
    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

    One observation:
    The image file extensions in the code are .gif
    The images posted on the site were .png


    You should start another thread with the new problem you posted in post#28. It will be too confusing to work on two programs in the same thread.
    Last edited by Norm; July 17th, 2011 at 12:48 PM.

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

    Default Re: Car racing

    I see. So if I follow the comments that you stated how's my previous code going to be?
    Mind to help me make some modification of my old code in post#1?

  13. #38
    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

    What are the modifications? Describe in detail what you want and how you think it can be done.

  14. #39
    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
    One observation:
    The image file extensions in the code are .gif
    The images posted on the site were .png


    You should start another thread with the new problem you posted in post#28. It will be too confusing to work on two programs in the same thread.
    Ok.Just like you mentioned here..after the modification was made, the track and the cars will be displayed after hitting the "Start Game" button at below.
    Then the 3 cars will be move at random speed along the track.
    The number of rounds along the track is how many laps that you want the race to be displayed for example, 3 laps,4 laps, along with their time of completion of the race.
    After the race is completed, their time are stored in a client/server to display their rankings through it.
    This is the overview of my program.

  15. #40
    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

    This is the overview of my program.
    Sounds like an ambitious project. How's it going?
    Are you able to move the cars on the track?

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

    Default Re: Car racing

    The cars still can't run and they still can't display including the track.
    Mind to help me..??
    I really need help to complete my program.
    Because I'm not really good in doing further programming techniques..
    Furthermore, my project need to handed in by this week..

  17. #42
    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

    The cars still can't run and they still can't display including the track.
    Are the image files being loaded? Do any of them display?
    Are the files correctly named and in the correct location?
    You are the only one that can check that. I can't see where any thing is on your computer.

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

    Default Re: Car racing

    Yup..my track was in gif format..
    And all of my cars are in png format..

  19. #44
    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

    they still can't display including the track.
    So this is NOT a problem now. Do All of the images display ok?

  20. #45
    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

    If you want help, you'll have to provide an SSCCE that demonstrates the problem, and you're going to want to answer the questions you've already been asked.
    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!

  21. #46
    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
    So this is NOT a problem now. Do All of the images display ok?
    Nope..they still can't be display..

  22. #47
    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
    If you want help, you'll have to provide an SSCCE that demonstrates the problem, and you're going to want to answer the questions you've already been asked.
    How does it works..??
    Because I'm still a newbie and beginner around here..

  23. #48
    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

    Do you file names in the program match the file names of the images?

  24. #49
    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
    Do you file names in the program match the file names of the images?
    Yup..exactly the same..

  25. #50
    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

    To show me, Copy the few lines of code from your program that load the images and that show the file names and paste them here
    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.

Page 2 of 5 FirstFirst 1234 ... LastLast