run by Eclipse) Rename the project to P4. Your project will contain a package ca
ID: 669806 • Letter: R
Question
run by Eclipse) Rename the project to P4. Your project will contain a package called animation that contains three classes.
Animation
Your job is to create a more interesting animation. At a minimum, I want an animation that:
Tells a story to us, amuses us, or impresses us.
Uses at least two different shapes, neither of which is a house or envelope.
Lasts at least five seconds. Interesting things should happen throughout the animation.
Has multiple independent objects moving in different directions simultaneously.
Has multiple independent objects that change direction without first jumping.
Has multiple independent objects that change color.
Has an object that smoothly expands or shrinks.
I will only look at the first ten seconds when grading. You can make the animation longer if you like, but be sure that all of the required elements appear in the first ten seconds.
To create an animation, you will need to replace the implementation of the paintFrame method in the Animation class. The paintFrame method takes as parameters a Graphics object (on which you can draw, using its methods) and the number of milliseconds that have passed since the animation started.
An animation is made up of many different frames. Each frame is a static image. The illusion of motion occurs because the images are displayed in rapid succession. The paintFrame method is called over and over again to obtain a sequence of static images. You can use the second parameter to determine how long the animation has been running, which will let you control the placement and timing of visual effects.
Your job will be much easier if you create helper methods and call them from paintFrame. For example, if you have two objects that move independently, you might want to have one method to draw the first object and another to draw the second object. (Those method should take parameters that control the positioning and/or size of the object.) Having helper methods will simplify your life, not complicate it!
Explanation / Answer
(moving cars) On execute we will find illusion of driving cars on four roud
Please find the required solution:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Animation extends Applet implements Runnable {
Thread t;
// Variables for positioning
int x1 = 0, x2 = 380, y1 = 50, y2 = 250;
boolean changeColor = true;
public void start() {
if (t == null) {
t = new Thread(this, "New Thread");//New Thread
// start of applet.
t.start();
}
}
public void stop() {
if (t != null) {
t = null;// On close of applet frame the created thread to be destroyed.
}
}
// Implementingof method run() of Runnable interface.
public void run() {
Thread t1 = Thread.currentThread();
while (t == t1) {
if (changeColor)
changeColor = false;
else
changeColor = true;
repaint();
try {
Thread.sleep(100);
} catch (Exception e) {
}
}
}
public void paint(Graphics g) {
setBackground(Color.cyan); setSize(500, 500);
g.setColor(Color.BLACK);
x1 = (x1 + 16) % 400;
x2 = x2 - 16;
y1 = (y1 + 12) % 300;
y2 = y2 - 12;
if (y2 < 0)
y2 = 288;
if (x2 < 0)
x2 = 384;
// Draw the roads using 2 filled rectangles using black color.
g.fillRect(0, 130, 400, 40);
g.fillRect(180, 0, 40, 305);
// Draw the white colored lines.
g.setColor(Color.white);
for (int i = 0; i < 20; i++) {
if (i != 9 && i != 10)
g.drawLine(i * 20, 150, i * 20 + 10, 150);
}
for (int j = 0; j < 15; j++) {
if (j != 7 && j != 8)
g.drawLine(200, j * 20, 200, j * 20 + 10);
}
// Draw 4 colored cars using filled round rectangles.
if(changeColor)
g.setColor(Color.red);
else
g.setColor(Color.green);
g.fillRoundRect(x2, 152, 20, 8, 2, 2);
g.fillRoundRect(x1, 140, 20, 8, 2, 2);
g.fillRoundRect(190, y1, 8, 20, 2, 2);
g.fillRoundRect(202, y2, 8, 20, 2, 2);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.