public void turnRight(double degrees) { rotationInDegrees + - = degrees; } publi
ID: 3921109 • Letter: P
Question
public void turnRight(double degrees) {
rotationInDegrees + - = degrees;
}
public void turnLeft(double degrees) {
rotationInDegrees - = degrees;
}
Actor
public class Actor {
private EZImage pikachu; // Member variable to store bug picture
private int x, y, startx, starty; // Member variables to store x, y, startx, starty
private int destx, desty; // Member variables to store destination values
private long starttime; // Member variable to store the start time
private long duration; // Member variable to store the duration
private boolean interpolation; // Member variable that is set to true if it is in the interpolation state
// ********* SCALE
private float currentScale = 1;
private float destScale = 1;
private float startScale = 1;
private boolean interpolationScale;
// Constructor for RCBug takes 3 parameters
public Actor(String filename,int posx, int posy){
// Set the current position of the bug
x=posx;y=posy;
// Create the image of the bug
pikachu = EZ.addImage(filename, posx, posy);
// Move it to the starting position. This is actually redundant.
pikachu.translateTo(x,y);;
// Set interpolation mode to false initially
interpolation = false;
interpolationScale = false;
}
// Set the destination by giving it 3 variables
// Dur means duration and is measured in seconds
//public void setDestination(int posx, int posy, long dur){
public void moveto(int posx, int posy, long dur){
// Set destination position and duration
// Convert seconds to miliseconds
destx = posx; desty = posy; duration = dur*1000;
// Get the startting time (i.e. time according to your computer)
starttime = System.currentTimeMillis();
// Set the starting position of your bug
startx=x; starty=y;
// Set interolation mode to true
interpolation = true;
interpolationScale = false;
}
// ********* SCALE
public void setScale(float s, float dur){
destScale = s;
starttime = System.currentTimeMillis();
startScale = currentScale;
interpolationScale = true;
}
// If you?re in interpolation state then return true, else false.
public boolean moving() {
return interpolation;
}
// ********* SCALE
public boolean scaling() {
return interpolationScale;
}
// This moves the bug based on the current time and elapsed time according to the interpolation equation
public void go(){
// If interpolation mode is true then do interpolation
if (interpolation == true) {
//linear interpolation
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime)/ (float) duration;
// Calculate the interpolated position of the bug
x = (int) (startx + ((float) (destx - startx) * normTime));
y = (int) (starty + ((float) (desty - starty) * normTime));
// If the difference between current time and start time has exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolation = false;
// Move the bug all the way to the destination
x = destx; y = desty;
}
// Don?t forget to move the image itself.
pikachu.translateTo(x,y);
}
//same code but for scale
// ********* SCALE
if (interpolationScale == true){
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime)/ (float) duration;
// Calculate the interpolated scale of the bug
currentScale = (startScale + ((float) (destScale - startScale) * normTime));
// If the difference between current time and start time has exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolationScale = false;
// Scale the bug all the way to the destination
currentScale = destScale;
}
// Don?t forget to move the image itself.
//scale command does the actual scaling
pikachu.scaleTo(currentScale);
}
}
}
Animator
import java.awt.Color;
public class Animator {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Setup EZ graphics system.
EZ.initialize (1024 , 512);
//Set background color to dark green
EZ.setBackgroundColor(Color.BLUE);
Actor pikachu=new Actor ("pickachu.png", 100, 50);
pikachu.moveto(1000, 50, 2);
//pikachu.moveto(200, 0, 2);
while(pikachu.moving()){
pikachu.go();
EZ.refreshScreen();
}
pikachu.moveto(500, 50, 2);
while(pikachu.moving()){
pikachu.go();
EZ.refreshScreen();
}
pikachu.moveto(500, 500, 2);
while(pikachu.moving()){
pikachu.go();
EZ.refreshScreen();
}
}}
The project is to create your own script-based animation program. Basically you're going to create a Java class for an "actor" that can read a script (like a movie script) and perform the actions in the script. An actor in this project is a graphical image. The script will consist of a series of two kinds of commands: MOVETO and TURNTO. For example the script might read as follows: move to position 100,100 in 5 seconds MOVETO 100 100 5 PLAY sound.wavplay a music file called sound.wav TURNTO 30 10 MOVETO 20 25 10 rotate by 30 degrees in 10 seconds move to position 20, 25 in 10 seconds Create two .java files for your program. Create a Java class file called Actor.java to contain the actor class, and create the Animator.java file to hold the main program You must develop this project using Java and EZ. Below are the points you will be awarded for the tasks you complete Milestone 1 (Due Oct 10, 1pm) [for 5 points total]: [1 pt] Write a class called Actor and in the constructor of your Actor load a picture to represent your character. [1 pt] Create a member function to perform the MOVETO function. [1 pt] Create a member function to perform the TURNTO function [1 pt] In your Animator class's main0 function, demonstrate that your Actor class works by making an Actor object that performs at least 10 movements (either moveto or turnto) [0.5 pt] Properly format and comment all code. [0.5 pts] Make sure you turn your entire project folder in as a zip file. Do not miss any files Include EZ.java as well A. B. C. D. E. F.
Explanation / Answer
Please find the code for the above problem as follows :-
Actor.java
package com.chegg.graphics;
public class Actor {
private EZImage pikachu; // Member variable to store bug picture
private int x, y, startx, starty; // Member variables to store x, y, startx,
// starty
private int destx, desty; // Member variables to store destination values
private long starttime; // Member variable to store the start time
private long duration; // Member variable to store the duration
private boolean interpolation; // Member variable that is set to true if it
// is in the interpolation state
private double destRotation;
private double currentRotation;
// ********* SCALE
private float currentScale = 1;
private float destScale = 1;
private float startScale = 1;
private boolean interpolationScale;
// Constructor for RCBug takes 3 parameters
public Actor(String filename, int posx, int posy) {
// Set the current position of the bug
x = posx;
y = posy;
// Create the image of the bug
pikachu = EZ.addImage(filename, posx, posy);
// Move it to the starting position. This is actually redundant.
pikachu.translateTo(x, y);
// Set interpolation mode to false initially
interpolation = false;
interpolationScale = false;
}
// Set the destination by giving it 3 variables
// Dur means duration and is measured in seconds
// public void setDestination(int posx, int posy, long dur){
public void moveto(int posx, int posy, long dur) {
// Set destination position and duration
// Convert seconds to milliseconds
destx = posx;
desty = posy;
duration = dur * 1000;
// Get the starting time (i.e. time according to your computer)
starttime = System.currentTimeMillis();
// Set the starting position of your bug
startx = x;
starty = y;
// Set interpolation mode to true
interpolation = true;
interpolationScale = false;
}
public void turnto(double degree, long dur) {
// Set Final Rotation and duration
destRotation = degree;
// Convert seconds to milliseconds
duration = dur * 1000;
// Get the starting time (i.e. time according to your computer)
starttime = System.currentTimeMillis();
// Set the starting position of your bug
currentRotation = pikachu.getRotation();
// Set interpolation mode to true
interpolation = true;
interpolationScale = false;
}
// ********* SCALE
public void setScale(float s, float dur) {
destScale = s;
starttime = System.currentTimeMillis();
startScale = currentScale;
interpolationScale = true;
}
// If you're in interpolation state then return true, else false.
public boolean moving() {
return interpolation;
}
// If you're in interpolation state then return true, else false.
public boolean rotating() {
return interpolation;
}
// ********* SCALE
public boolean scaling() {
return interpolationScale;
}
// This moves the bug based on the current time and elapsed time according
// to the interpolation equation
public void go() {
// If interpolation mode is true then do interpolation
if (interpolation == true) {
// linear interpolation
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime) / (float) duration;
// Calculate the interpolated position of the bug
x = (int) (startx + ((float) (destx - startx) * normTime));
y = (int) (starty + ((float) (desty - starty) * normTime));
// If the difference between current time and start time has
// exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolation = false;
// Move the bug all the way to the destination
x = destx;
y = desty;
}
// Don't forget to move the image itself.
pikachu.translateTo(x, y);
}
// same code but for scale
// ********* SCALE
if (interpolationScale == true) {
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime) / (float) duration;
// Calculate the interpolated scale of the bug
currentScale = (startScale + ((float) (destScale - startScale) * normTime));
// If the difference between current time and start time has
// exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolationScale = false;
// Scale the bug all the way to the destination
currentScale = destScale;
}
// Don't forget to move the image itself.
// scale command does the actual scaling
pikachu.scaleTo(currentScale);
}
}
// This turns/rotates the bug based on the current time and elapsed time according
// to the interpolation equation in increments of rotation angle
public void doRotation() {
double rotation = 0;
// If interpolation mode is true then do interpolation
if (interpolation == true) {
// linear interpolation
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime) / (float) duration;
// Calculate the interpolated position of the bug
rotation = (destRotation - currentRotation)*normTime;
System.out.println(rotation);
// If the difference between current time and start time has
// exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolation = false;
// Move the bug all the way to the destination
rotation = destRotation;
}
// Don't forget to move the image itself.
pikachu.rotateTo(rotation);
}
}
}
Animator.java
package com.chegg.graphics;
import java.awt.Color;
public class Animator {
public static void main(String[] args) {
// TODO Auto-generated method stub
// Setup EZ graphics system.
EZ.initialize(1024, 512);
// Set background color to dark green
EZ.setBackgroundColor(Color.BLUE);
Actor pikachu = new Actor("Pikachu.png", 100, 50);
pikachu.moveto(1000, 50, 2);
while (pikachu.moving()) {
pikachu.go();
EZ.refreshScreen();
}
pikachu.moveto(500, 50, 2);
while (pikachu.moving()) {
pikachu.go();
EZ.refreshScreen();
}
pikachu.moveto(500, 500, 2);
while (pikachu.moving()) {
pikachu.go();
EZ.refreshScreen();
}
pikachu.turnto(90, 2);
while(pikachu.rotating()){
pikachu.doRotation();
EZ.refreshScreen();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.