Question

import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import javafx.animation

import javafx.application.Application;

import javafx.stage.Stage;

import javafx.scene.Scene;

import javafx.scene.Group;

import javafx.scene.canvas.Canvas;

import javafx.scene.canvas.GraphicsContext;

import javafx.scene.image.Image;

import javafx.animation.AnimationTimer;

import java.io.IOException;

import java.io.File;

// Animation of Orc walking

public class Animation extends Application {

final int canvasCount = 10;

int picInd = 0;

double xloc = 0;

double yloc = 0;

final double xIncr = 8;

final double yIncr = 2;

final static int canvasWidth = 500;

final static int canvasHeight = 300;

final static int imgWidth = 165;

final static int imgHeight = 165;

// TODO: Change the code so that at least eight orc animation pngs are loaded

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import javafx.application.Application;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.animation.AnimationTimer;

import java.io.IOException;
import java.util.ArrayList;
import java.io.File;

// Animation of Orc walking
public class Animation extends Application {
    final int canvasCount = 10;
    int picInd = 0;
    double xloc = 0;
    double yloc = 0;
    final double xIncr = 8;
    final double yIncr = 2;
    final static int canvasWidth = 500;
    final static int canvasHeight = 300;
    final static int imgWidth = 165;
    final static int imgHeight = 165;

    // TODO: Change the code so that at least eight orc animation pngs are loaded

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage theStage) {
        theStage.setTitle("Orc");
        
        boolean flag1 = true;
        boolean flag2 = true;

        Group root = new Group();
        Scene theScene = new Scene(root);
        theStage.setScene(theScene);

        Canvas canvas = new Canvas(canvasWidth, canvasHeight);
        root.getChildren().add(canvas);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        ArrayList<Image> orc_img = createImage();

        final long startNanoTime = System.nanoTime();

        new AnimationTimer() {
            public void handle(long currentNanoTime)
            {
                double t = (currentNanoTime - startNanoTime) / 1e9; 
                
                if(xloc == canvasWidth) {
                        flag1 = false;
                }
                else if(xloc == 0) {
                        flag1 = true;
                }
                if(yloc == canvasHeight) {
                        yloc += yIncr;
                } else if(yloc == 0) {
                        yloc += yIncr;
                }

                if(flag1 == true) {
                        xloc += xIncr;
                } else {
                        xloc -= xIncr;
                }
                
                if(flag2 == true ) {
                        yloc += yIncr;
                } else {
                        yloc -= yIncr;
                }
                //xloc += xIncr;
                //yloc += yIncr;

                // Clear the canvas
                gc.clearRect(0, 0, canvasWidth, canvasHeight);

                // draw the subimage from the original png to animate the orc's motion
                gc.drawImage(orc_img.get(0), imgWidth*picInd, 0, imgWidth, imgHeight,
                                    xloc, yloc, imgWidth, imgHeight);
                picInd = (picInd + 1) % canvasCount;
                
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                
                // TODO: Keep the orc from walking off-screen, turn around when bouncing off walls.
                //Be sure that animation picture direction matches what is happening on screen.
            }
        }.start();
        theStage.show();
    }

    //Read image from file and return
    private ArrayList<Image> createImage() {
        ArrayList<Image> imgs = new ArrayList();
        imgs.add(new Image("lab_animation_javafx\\images\\orc_die_east.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_die_south.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_die_west.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_die_north.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_fire_east.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_fire_west.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_fire_northeast.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_fire_northwest.png"));
        imgs.add(new Image("lab_animation_javafx\\images\\orc_idle_ewns.png"));
       
        
        //Image  img = new Image("lab_animation_javafx\\images\\orc_die_east.png");
        return imgs;    
        // TODO: Change this method so you can load other orc animation bitmaps
    }
}
Add a comment
Know the answer?
Add Answer to:
import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.Group; import javafx.scene.canvas.Canvas; import javafx.scene.canvas.GraphicsContext; import javafx.scene.image.Image; import javafx.animation
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets;...

    Examine the following code and complete missing parts: import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.stage.Stage; public class NtoThePowerOfN extends Application{ @Override public void start(Stage primaryStage) throws Exception { TextField inputField; TextField outputField; VBox base = new VBox(10); base.setPadding(new Insets(10)); base.setAlignment(Pos.CENTER); // // A: input components - label and text field // // // B: button - to compute the value // // //...

  • (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import...

    (5 points) Analyze the following codes. b) import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Test extends Application { @Override // Override the start method in the Application class public void start(Stage primaryStage) { Button btOK = new Button("OK"); Button btCancel = new Button("Cancel"); EventHandler<ActionEvent> handler = new EventHandler<ActionEvent>() { public void handle(ActionEvent e) { System.out.println("The OK button is clicked"); } }; btOK.setOnAction(handler); btCancel.setOnAction(handler); HBox pane = new HBox(5); pane.getChildren().addAll(btOK, btCancel); Scene...

  • / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import...

    / Finish the code to make it work import static javafx.application.Application.launch; import java.io.File; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.media.AudioClip; import javafx.stage.Stage; public class JukeBox extends Application { private ChoiceBox<String> choice; private AudioClip[] tunes; private AudioClip current; private Button playButton, stopButton;    //----------------------- // presents an interface that allows the user to select and play // a tune from a drop down box //-----------------------   ...

  • # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {...

    # Java import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class Test extends Application {   @Override // Override the start method in the Application class   public void start(Stage primaryStage) {     // Create a button and place it in the scene     Button btOK = new Button("OK");     btOK.setOnAction(e -> System.out.println("OK 1"));     btOK.setOnAction(e -> System.out.println("OK 2"));     Scene scene = new Scene(btOK, 200, 250);     primaryStage.setTitle("MyJavaFX"); // Set the stage title     primaryStage.setScene(scene); // Place the scene in the stage     primaryStage.show(); // Display the stage...

  • Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random...

    Write a JavaFX application that displays 10,000 very small circles (radius of 1 pixel) in random locations within the visible area. Fill the dots on the left half of the scene red and the dots on the right half of the scene green. Use the getWidth method of the scene to help determine the halfway point. This is what I have so far: import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.shape.Circle; import javafx.scene.paint.Color; import javafx.stage.Stage; import java.util.Random; import javafx.scene.Group; public class Class615...

  • A game clock that starts at zero and begins to count up in seconds. Add buttons...

    A game clock that starts at zero and begins to count up in seconds. Add buttons to pause and resume the clock. Given this initial code: import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.stage.Stage; import javafx.util.Duration; import javafx.scene.paint.Color; public class AnimationCounter2 extends Application{ @Override public void start(Stage stage) throws Exception{ // Create the label and align its contents. Label label = new Label("0"); label.setTextFill(Color.RED); label.setStyle("-fx-font-size: 4em;"); label.setAlignment(Pos.CENTER); EventHandler<ActionEvent> handler =...

  • ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button...

    ***Please keep given code intact*** Write a JavaFX application that displays a label and a Button to a frame in the FXBookQuote2 program. When the user clicks the button, display the title of the book that contains the opening sentence or two from your favorite book in the label. FXBookQuote2.java /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the...

  • Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** *...

    Fix the todo in ball.java Ball.java package hw3; import java.awt.Color; import java.awt.Point; import edu.princeton.cs.algs4.StdDraw; /** * A class that models a bounding ball */ public class Ball {    // Minimum and maximum x and y values for the screen    private static double minX;    private static double minY;    private static double maxX;    private static double maxY;    private Point center;    private double radius;    // Every time the ball moves, its x position changes by...

  • Can someone please comment this on what the javafx do and how they are implemented? import...

    Can someone please comment this on what the javafx do and how they are implemented? import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Paint; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class TextPanel extends Application { GridPane grid = null; Text scenetitle = null; Label tDrawLabel = null; Label bsc345Label = null; HBox THBox = null; VBox mVBox = new VBox(2); Stage primaryStage = null; @Override public void start(Stage...

  • use this code of converting Km to miles , to create Temperature converter by using java...

    use this code of converting Km to miles , to create Temperature converter by using java FX import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.geometry.Pos; import javafx.geometry.Insets; import javafx.scene.control.Label; import javafx.scene.control.TextField; import javafx.scene.control.Button; import javafx.event.EventHandler; import javafx.event.ActionEvent; /** * Kilometer Converter application */ public class KiloConverter extends Application { // Fields private TextField kiloTextField; private Label resultLabel; public static void main(String[] args) { // Launch the application. launch(args); } @Override public void start(Stage primaryStage) { //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT