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 15 of 15

Thread: need help finishing my project

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need help finishing my project

    Hello i got homework that i cant complete can anybody help me

    --- Update ---

    package carracing;

    import javafx.application.Application;
    import javafx.animation.KeyFrame;
    import javafx.scene.Scene;
    import javafx.animation.Timeline;
    import static javafx.application.Application.launch;
    import javafx.scene.input.KeyCode;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Polygon;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    import javafx.util.Duration;

    /**
    *
    * @author Shak
    */
    public class CarRacing extends Application {

    @Override
    public void start(Stage primaryStage) {
    class CarPane extends Pane
    {
    private double paneWidth = 200; //Width of the pane
    private double paneHeight = 200; //Height of the pane
    private double baseX = 0; //Intial X placement of Car
    private double baseY = paneHeight; //Intial y placement of Car
    private Circle c1 = new Circle (baseX+15, baseY-5, 5); // Placement of first wheel
    private Circle c2 = new Circle (baseX+35, baseY-5, 5); //Placement of second wheel
    private Rectangle carBody = new Rectangle (baseX,baseY-20,50,10); //Car body
    private Polygon carTop = new Polygon (baseX+10,baseY-20, //Car top
    baseX+20, baseY-30, baseX+30, baseY-30,
    baseX+40, baseY-20);
    public CarPane()
    {
    carBody.setFill (Color.CYAN);
    carTop.setFill (Color.BLUE);
    this.getChildren().addAll (c1,c2,carBody,carTop );

    }
    public void setValues()
    {
    c1.setCenterX (baseX+15);
    c1.setCenterY (baseY-5);
    c2.setCenterX (baseX+35);
    c2.setCenterY (baseY-5);
    carBody.setX(baseX);
    carBody.setY(baseY-20);

    carTop.getPoints().clear();
    carTop.getPoints().addAll(baseX+10,baseY-20,
    baseX=20,baseY-30,baseX+30,baseY-30,baseX+40,baseY-20);
    }

    public void move()
    {
    if (baseX > paneWidth)
    {
    baseX=-20;
    }
    else
    {
    baseX+= 1;
    }
    setValues();
    }
    public void setW( double newWidth)
    {
    this.paneWidth =newWidth;
    setValues();
    }
    public void setH (double newHeight)
    {
    this.paneHeight = newHeight;
    setValues();
    }
    {
    //Create a new CarPane
    CarPane car=new CarPane();
    //Create a scene and place it in the stage
    Scene scene=new Scene (car,200,200);
    primaryStage.setTitle("Racing Car"); // Set the stage title
    primaryStage.setScene(scene); //Place the scene in the stage
    primaryStage.show(); // Display the stage

    Timeline animation = new Timeline ( new KeyFrame(Duration.millis(100),
    e->car.move()));
    animation.setCycleCount ( Timeline.INDEFINITE);
    animation.play(); // Start animation

    scene.widthProperty().addListener (e->car.setW( car.getWidth()));
    scene.heightProperty().addListener (e->car.setH (car.getHeight()));

    car.setOnMousePressed(e->animation.pause());
    car.setOnMouseReleased(e->animation.play());

    car.requestFocus();
    car.setOnKeyPressed(e ->

    {
    switch ( e.getCode())
    {
    case UP:
    animation.setRate( animation.getRate()+1);
    break;
    case DOWN:
    animation.setRate (animation.getRate()-1);
    break;

    }};

  2. #2
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cant Complete the Program

    I cant Complete the lab.I dont know what is wrong with the codeimage1.jpgimage1(1).jpg

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program


  4. #4
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program

    package carracing;
     
    import javafx.application.Application;
    import javafx.animation.KeyFrame;
    import javafx.scene.Scene;
    import javafx.animation.Timeline;
    import static javafx.application.Application.launch;
    import javafx.scene.input.KeyCode;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.scene.shape.Polygon;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    import javafx.util.Duration;
     
    /**
     *
     * @author Shak
     */
    public class CarRacing extends Application {
     
        @Override
        public void start(Stage primaryStage) {
           class CarPane extends Pane
            {
                private double paneWidth = 200;           //Width of the pane   
                private double paneHeight = 200;          //Height of the pane
                private double baseX = 0;                 //Intial X placement of Car
                private double baseY = paneHeight;        //Intial y placement of Car
                private Circle c1 = new Circle (baseX+15, baseY-5, 5);  // Placement of first wheel
                private Circle c2 = new Circle (baseX+35, baseY-5, 5);    //Placement of second wheel
                private Rectangle carBody = new Rectangle (baseX,baseY-20,50,10);   //Car body
                private Polygon carTop = new Polygon (baseX+10,baseY-20,       //Car top
                      baseX+20, baseY-30, baseX+30, baseY-30,
                      baseX+40, baseY-20);
                public CarPane()          
            {
                    carBody.setFill (Color.CYAN);
                    carTop.setFill (Color.BLUE);
                    this.getChildren().addAll (c1,c2,carBody,carTop );
     
            }        
                public void setValues()  
                {
                 c1.setCenterX (baseX+15);
                 c1.setCenterY (baseY-5);
                 c2.setCenterX (baseX+35);
                 c2.setCenterY (baseY-5);
                 carBody.setX(baseX);
                 carBody.setY(baseY-20);
     
                 carTop.getPoints().clear();
                 carTop.getPoints().addAll(baseX+10,baseY-20,
                         baseX=20,baseY-30,baseX+30,baseY-30,baseX+40,baseY-20);
                }
     
                 public void move()
        {
                 if (baseX > paneWidth)
                 {
                     baseX=-20;
                 }
                 else
                 {
                     baseX+= 1;
                 }
                 setValues();
        }
                 public void setW( double newWidth)
            {
                 this.paneWidth =newWidth;
             setValues();
            }
             public void setH (double newHeight)
        {
                 this.paneHeight = newHeight;
                 setValues();
        }
    {
    //Create a new CarPane
             CarPane car=new CarPane();
    //Create a scene and place it in the stage
             Scene scene=new Scene (car,200,200);
             primaryStage.setTitle("Racing Car"); // Set the stage title
             primaryStage.setScene(scene); //Place the scene in the stage
             primaryStage.show();    // Display the stage
     
             Timeline animation = new Timeline ( new KeyFrame(Duration.millis(100),
                     e->car.move()));
             animation.setCycleCount ( Timeline.INDEFINITE);
             animation.play(); // Start animation
     
            scene.widthProperty().addListener (e->car.setW( car.getWidth()));
            scene.heightProperty().addListener (e->car.setH (car.getHeight()));
     
            car.setOnMousePressed(e->animation.pause());
            car.setOnMouseReleased(e->animation.play());
     
               car.requestFocus();
            car.setOnKeyPressed(e ->
     
            {
             switch ( e.getCode())
               {
                 case UP:
                     animation.setRate( animation.getRate()+1);
                     break;
                 case DOWN:
                     animation.setRate (animation.getRate()-1);
                     break;
     
             }};
    Last edited by isom; February 9th, 2018 at 06:07 PM.

  5. #5
    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: Cant Complete the Program

    Can you explain what the problem is?
    Are there compiler errors? Please copy the full text and paste it here.
    Are there execution errors? Please copy the full text and paste it here.

    Otherwise please explain.

    Note the posted images are not readable.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program

    Yea its giving me errors when i try to run it. I follow all the directions but still no go.
    image1(2).jpgimage1.jpgimage1(1).jpg

  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: Cant Complete the Program

    Please copy and paste any output or info about the problem. Don't bother to post images.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program

    the images are coming out bad i dont know why and the problem is program is not running it

    --- Update ---

    Compiling 1 source file to C:\Users\Shak\Documents\NetBeansProjects\CarRacing \build\classes
    C:\Users\Shak\Documents\NetBeansProjects\CarRacing \src\carracing\CarRacing.java:118: error: ')' expected
    }};
    C:\Users\Shak\Documents\NetBeansProjects\CarRacing \src\carracing\CarRacing.java:126: error: reached end of file while parsing
    };
    2 errors
    C:\Users\Shak\Documents\NetBeansProjects\CarRacing \nbproject\build-impl.xml:931: The following error occurred while executing this line:
    C:\Users\Shak\Documents\NetBeansProjects\CarRacing \nbproject\build-impl.xml:271: Compile failed; see the compiler error output for details.
    BUILD FAILED (total time: 0 seconds)

  9. #9
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Cant Complete the Program

    At the end of the code you posted you have

    car.setOnKeyPressed(e ->
     
            {
             switch ( e.getCode())
               {
                 case UP:
                     animation.setRate( animation.getRate()+1);
                     break;
                 case DOWN:
                     animation.setRate (animation.getRate()-1);
                     break;
     
             }};

    Note how the "(" in the first line is not matched by any subsequent ")". This will give the sort of compiler errors you posted: ie ')' expected.

    In general it is helpful if your compiler messages refer to some particular line - the first message is talking about line 118 of CarRacing.java - then it's a good idea to say which line of your code is being referred to. People tend to be lazy and won't count through the code you post to find it.

  10. #10
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program

    Im sorry am new to java and im not lazy i just dont know. So what is my problem if you dont mind answering? and how do i make Implement a new key handler for the HOME key.The home key will reset the baseX parameter to 0 so the car starts over at the initial position.its says create resetCarW() method in class

    --- Update ---

    and note that i was following direction on paper

  11. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Cant Complete the Program

    I'm sorry about the misunderstanding: what I meant to suggest is that it is us (the people who are reading your post) who tend to be lazy. When you indicate which line is 118 or whatever, people will look at it, otherwise maybe not.

    In any case, did you understand what I said about the mismatched parentheses?

  12. #12
    Junior Member
    Join Date
    Feb 2018
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cant Complete the Program

    Yea i undestood but direction tell me to car.setOnKeyPressed(e -> leave it like that.

  13. #13
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Cant Complete the Program

    Fair enough. But what happens if you insert the missing ')' just before the end of the statement? Note that the statement ends with the very final ';' character.

  14. #14
    Member
    Join Date
    Apr 2014
    Posts
    92
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Cant Complete the Program

    Quote Originally Posted by isom View Post
    Yea i undestood but direction tell me to car.setOnKeyPressed(e -> leave it like that.
    Those directions don't appear to be very good. Here's the correct structure (and preferred convention) of a lamda expression:

    setOnKeyPressed(e -> {
     
    });
    Note where the start/end parenthesis and the start/end curly braces are.

  15. #15
    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: need help finishing my project

    Threads merged.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need help finishing class methods
    By R2B Boondocks in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 24th, 2013, 11:13 AM
  2. Complex numbers need help finishing
    By therealvasile in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 1st, 2013, 12:26 PM
  3. JAVA IN FINISHING SOMETHING
    By komal510 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 18th, 2013, 10:47 PM
  4. Need help finishing my program
    By PapaMojo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 8th, 2012, 01:21 PM
  5. Replies: 11
    Last Post: February 10th, 2012, 05:38 PM