Please write a java program based on the code below. Please use api from http://
ID: 3672156 • Letter: P
Question
Please write a java program based on the code below.
Please use api from http://eventfuljava.cs.williams.edu/library/objectdrawJavadocV1.1.1/index.html
thank you
public static void main(String[] args) {
// start the animation.
new Animate().startController(SIZE,SIZE+LINUX_MENU_BAR);
}
}
import objectdraw.*;
import java.lang.InterruptedException;
/**
* @author:
* about: This program uses the objectdraw library to create an animation.
* <describe your animation>
*
* compile: javac -cp objectdraw.jar:. Animate.java
* run: java -cp objectdraw.jar:. Animate
*/
public class Animate extends WindowController {
final static int SIZE = 500; //size of the window for animation.
final static int LINUX_MENU_BAR = 50; // account for menu bar on
// linux systems
/**
* Program control automatically jumps to this method after executing
* startController(). This is the method you'll want to use to draw
* your animation or create threads.
*/
public void begin() {
int startSleep = 1000;
// The canvas variable is inherited. We'll talk about this later
// in the class. For now, whenever the API asks for a canvas
// as a parameter, use myCanvas
DrawingCanvas myCanvas = canvas;
//Example thread creation to draw a circle
Thread t1 = new DrawArc(0, 0, canvas);
catchSleep(startSleep); //make the primary thread sleep
t1.start();
}
/**
* Has the program pause for time miliseconds so animations can move
* at a speed you'd like
* @param time the time in miliseconds you want to sleep.
*/
public static void catchSleep(int time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {}
}
Creating an animation with threads (45 pts) You can find the API (Application Program Interface) for the graphics library objectdraw.jar online at http://eventfuliava.cs.williams.edu/library/objectdrawJavadocV1.1.1/index.html. Edit the program Animate.java to draw an image that has some kind of animation, e.g. sunrise/sunset, bouncing balls, a movie scene and uses threads. At the minimum, there must be some kind of object/shape that moves around the screen and makes use of at least 2 threads. You will be graded on a range of creativity, animation, and complexity. To do this, you'll need to look through the API and find which available classes and methods will help you. The starter files, Animate.java and DrawArc.java, have comments that will show you where to get started. You do not have to make use of DrawArc.java in your project, it is just there to help you get started. However, you will need to turn in Animate.java. You can compile an d run your code via: $javac -cp objectdraw.jar:. Animate.java $ java -cp objectdraw.jar:. Animate To exit the program you'll need to return focus to the terminal and hold CTRL-C. If you're working remotely and trying to visualize your animation, you'll find that it's very choppy due to network lag. You'll want to either develop on your laptop with the JDK installed or on the lab machines.Explanation / Answer
Here I have done for just scroling animation using threads:
package animation;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollingBanner extends JPanel implements Runnable
{
protected Thread banner; // animation thread
protected String text; // text to be displayed
protected Font font; // font used to display text
protected int width; // width of viewing area in pixels
protected int height; // height of viewing area in pixels
protected int x, y; // current position of text
protected int delay; // interval between frames in millisec
protected int offset; // distance moved between two frames
protected Dimension size; // size of viewing area
protected Image image; // off-screen image
protected Graphics offScreen; // off-screen graphics
public ScrollingBanner( int aWidth, int aHeight )
{
// Set the width and heigth and size
width = aWidth;
height = aHeight;
setSize ( width, height );
// Set the text
text = new String ("Java is fun");
// Set the font
font = new Font ( "Sans-serif", Font.BOLD, 24 );
// Set the time interval between frames in millisec
delay = 100;
// Set the intial values for x and y
x = width / 2;
y = height / 2;
// Set the offset
offset = 1;
// Create and start the thread
banner = new Thread ( this );
banner.start();
}
public void paintComponent ( Graphics g )
{
// Get the size of the viewing area
size = this.getSize();
// Create the off-screen image buffer if it is the first time
if ( image == null )
{
image = createImage ( size.width, size.height );
offScreen = image.getGraphics();
}
// Get the font metrics to determine the length of the text
offScreen.setFont ( font );
FontMetrics fm = g.getFontMetrics();
int length = fm.stringWidth ( text );
// Adjust the position of the text from the previous frame
x = x - offset;
// If the text is completely off to the left end move the
// position of the banner to the right edge
if ( x < -length )
x = size.width;
// Set the color and paint the background
offScreen.setColor ( Color.black );
offScreen.fillRect ( 0, 0, size.width, size.height );
// Set the pen color and draw the text
offScreen.setColor ( Color.green );
offScreen.drawString ( text, x, y );
// Copy the off-screen image to the screen
g.drawImage ( image, 0, 0, this );
}
public void update ( Graphics g )
{
paintComponent ( g );
}
public void run ()
{
while ( Thread.currentThread() == banner )
{
repaint ();
try
{
Thread.sleep ( delay );
}
catch ( InterruptedException e )
{
System.out.println ( "Exception: " + e.getMessage() );
}
}
}
public static void main ( String[] args )
{
JFrame frame = new JFrame ( "Scrolling Banner" );
ScrollingBanner panel = new ScrollingBanner ( 400, 400 );
frame.getContentPane().add ( panel );
frame.setSize ( panel.width, panel.height );
frame.setVisible ( true );
frame.addWindowListener ( new WindowAdapter() {
public void windowClosing ( WindowEvent evt ) {
System.exit ( 0 );
}
} );
}
}