80 likes | 94 Views
Learn how to animate Line's properties using threads in Java. Create, execute, and modify command classes for smooth animations. Set up and submit projects with a partner.
E N D
Recitation 12 November 19, 2010
Today’s Goals: • Use Threads to animate changes to a Line’s properties
Creating a thread Creates a new Command Creates a new Java thread publicvoidanimateFromOrigin() { RunnableanimateCommand = newAnimationCommand(…); Thread thread = new Thread(animateCommand); thread.setName(“Shuttle Animation”); thread.start(); } Invokes the run method on the command object passed to constructor.
Pausing a thread void sleep(int pauseTime) { try { // OS suspends program for pauseTime Thread.sleep(pauseTime); } catch (Exception e) { // program may be forcibly interrupted while sleeping e.printStackTrace(); }; }
java.lang.Runnable Interface Command Object run () package java.lang; publicinterface Runnable { public void run(); }
Runnable Implementation publicclassAShuttleAnimationCommandimplementsRunnable{ Shuttle shuttle; intanimationStep, animationPauseTime; publicAShuttleAnimationCommand (Shuttle shuttle, int step, int pause) { this.shuttle = shuttle; animationStep = step; animationPauseTime = pause; } publicvoid run() {shuttle.animateFromOrigin(animationStep,animationPauseTime); } } Note: Will likely need to specify additional parameters (e.g. where to move to!) Parameters Target Object
Recitation Specification • Goal: Animate the changes to a Line’s width and height • Tasks: • 1 – create and execute new animate thread from ALine • 2 – modify command classes to call ALine’s sync. animate method • 2 – write the animation code in ALine • Create two ObjectEditor windows: • 1 – Display the instance of Line • 1 – Display an instance of LineEditor (should have a reference to the Line) • Note: we’ve provided copies of the code to begin with on the recitation website. You’ll need to modify code in ALine, ASetWidthCommand, and ASetHeightCommand.
Setup and Submission • Please follow the instructions on the course website to set up and submit your projects! • Set up your project: • http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/makeproject/ • Submit an assignment: • http://www.cs.unc.edu/~dewan/comp114/f10/Recitations/submit/ • Work with a single partner!