1 / 8

Recitation 12

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. public void animateFromOrigin () { Runnable animateCommand = new AnimationCommand ( … );

Télécharger la présentation

Recitation 12

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 12 November 19, 2010

  2. Today’s Goals: • Use Threads to animate changes to a Line’s properties

  3. 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.

  4. 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(); }; }

  5. java.lang.Runnable Interface Command Object run () package java.lang; publicinterface Runnable { public void run(); }

  6. 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

  7. 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.

  8. 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!

More Related