In this assignment you will crcatc your own interactive animation using Processi
ID: 655309 • Letter: I
Question
In this assignment you will crcatc your own interactive animation using Processing. While the technical focus of the task is to gain experience using inheritance and Processing, your grade will also be determined by the creative and artistic merit of your submission. In other words, merely providing a program that is similar in scope to the provided demos from lecture will receive a below average grade. There are several requirements: There must be animation; a static scene is not acceptable. There must be some form of interactivity via mouse input (e.g. clicking some where specific on screen, click and drag motion, etc). There must be some form of interactivity via keyboard input. You must define some hasc class that provides common functionality for any item drawn on the screen. For example, all items will need their color specified, so for simple one-color objects, a piece of common functionality could be for the hasc class to store the color parameters. However, you instead might want to make more complicated objects, such as a car which would be drawn via multiple shapes and colors: black circles for tires and a red triangle and rectangle to form the body. Thus, you will first have to consider what high-level objects you want to draw in your animation and then figure out what common functionality would be most useful to share via a hasc superclass. You must define at least three subclasses for three different high-level objects in your animation. These must all extend your base class but they don't have to directly extend it. Fbr example, they can extend a class which extends your base class. In other words, your hasc class must be a superclass of all your high-level objects to draw but the base class doesn' t have to be the immediate parent class; it can be a grandparent, a great grandparent, a great-great grandparents, etc. You must make use of the instance of keyword in a useful way. You must use at least one ArrayList to hold/manage all the objects that your program draws on the screen. You can use multiple lists if you want to group certain items together or establish layers, which could allow you to ensure that one set items always gets drawn on top of another set of items drawn on the screen. Your program should add/remove items from the above ArrayList as the program runs, either in response to something that happens (user input or programmed response). That is, in addition to motion, you must have some objects that arc removed from the animation and similarly, some that arc added. For example, if an object goes off the screen, you might remove it from ArrayList so that it is no longer drawn and it is garbage collected.Explanation / Answer
package com.tutai;
import java.util.ArrayList;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.stage.Stage;
class Animation{ //base class animation
public Animation() {
super();
this.name = "";
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Sprite extends Animation{ //class sprite
private int height;
private int width;
public Sprite(){
super();
this.height=0;
this.width=0;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
class Car extends Sprite{ //class car
private int noOfWheels;
private String color;
public Car(){
this.noOfWheels=0;
this.color="";
}
public int getNoOfWheels() {
return noOfWheels;
}
public void setNoOfWheels(int noOfWheels) {
this.noOfWheels = noOfWheels;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
class Ford extends Car{ //class ford
private String model;
public Ford(){
this.model="";
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
}
public class Main extends Application {
public static void main(String[] args) { //main method
Application.launch(args); //launch the application
}
@Override
public void start(Stage primaryStage) {
Group root = new Group(); //create group
Scene scene = new Scene(root, 300, 250); //create scene
TextField textBox = new TextField(); //create textbox
textBox.setPromptText("Write here");
/*keybord event handlers*/
textBox.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
System.out.println("Key Pressed: " + ke.getText());
}
});
textBox.setOnKeyReleased(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
System.out.println("Key Released: " + ke.getText());
}
});
/*keybord event handlers*/
Button btn = new Button(); //create button
btn.setText("Hello World");
/*mouse event handlers*/
btn.setOnMouseEntered(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
System.out.println("Mouse entered");
}
});
btn.setOnMouseExited(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
System.out.println("Mouse exited");
}
});
btn.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
System.out.println("Mouse pressed");
}
});
/*mouse vent handlers*/
root.getChildren().add(btn); //add button and textbox to stage
root.getChildren().add(textBox);
ArrayList<Animation> a=new ArrayList<Animation>(); //create arraylist of base class
Sprite s=new Sprite(); //create sprite object
Car c=new Car(); //create car object
Ford f=new Ford(); //create ford object
s.setName("car sprite");
s.setHeight(10);
s.setWeight(20);
c.setColor("RED");
c.setNoOfWheels(4);
f.setModel("A20");
a.add(s); //add object to arraylist
a.add(c);
a.add(f);
root.getChildren().add((Node)s); //add animation objects to stage
root.getChildren().add((Node)c);
root.getChildren().add((Node)f);
if(f instanceof Animation){ //check instanceof Ford object
System.out.println("Ford is an instance of Animation class");
}
a.remove(f); //remove object from arraylist
primaryStage.setScene(scene);
primaryStage.show(); //show the stage
}
}