Liveliness.


59 views
Uploaded on:
Description
Liveliness making protests that seem to proceed onward the screen is finished by showing a progression of still pictures, in a steady progression, in fast progression ...
Transcripts
Slide 1

Animation

Slide 2

Moving pictures Animation—production questions that seem to proceed onward the screen—is finished by showing a progression of still pictures, in a steady progression, in fast progression Generally you ought to strive for no less than 20 pictures/second 20 pictures/second is repainting each 50 milliseconds

Slide 3

"Ricocheting ball" is the "Welcome World" of movement We will build up this system utilizing: Model-View-Controller Observer-Observable Threads Timers The bobbing ball

Slide 4

Review of MVC remains for Model-View-Controller The Model is the real interior representation It ought to be free of alternate classes It\'s convenient on the off chance that this class can expand Observable The View (or a View) is a method for taking a gander at or showing the model It\'s convenient on the off chance that this class actualizes Observer The Controller accommodates client information and adjustment These three segments are normally executed as particular classes (or sets of classes)

Slide 5

Review of Observer and Observable java.util.Observable is a class When it accomplishes something that ought to be watched, it says: setChanged(); notifyObservers();/* or */notifyObservers( arg ); java.util.Observer is an interface It needs to enlist itself with (subscribe to) an Observable: myObservable .addObserver( myObserver ); It needs to actualize: open void update(Observable obs , Object arg ) This strategy is consequently called when spectators are advised obs is the item being watched If the Observable did notifyObservers() , arg is invalid

Slide 6

Review of Thread s You can amplify the Thread class: class Animation develops Thread {… } Limiting, since you can just augment one class You should override open void run( ) To make it "go": Animation anim = new Animation( ); anim.start( ); Or you can execute the Runnable interface: class Animation executes Runnable {… } You should actualize open void run( ) To make it "go": Animation anim = new Animation( ); Thread myThread = new Thread(anim); myThread.start( );

Slide 7

Timer s A java.util.Timer is utilized to calendar code for future execution A Timer may: Schedule a one-time execution, or Schedule rehashed executions at customary interims Timer constructors: Timer() Timer(boolean isDaemon ) Timer(String name ) Timer(String name , boolean isDaemon ) A Timer can keep an application from ending, unless it is determined as a daemon string A daemon string bites the dust if there are no-daemon strings running Create daemon Timer strings with new Timer(true) or new Timer( name , genuine)

Slide 8

Using a Timer for activity open void schedule(TimerTask errand , long postpone , long stretch ) Schedules the predetermined assignment for rehashed settled deferral execution, starting after the predefined delay (which might be zero) Subsequent executions happen at roughly consistent interims isolated by the predetermined period Times are indicated in milliseconds (1/1000s of a second) Notice that timetable requires a TimerTask as a contention TimerTask is a dynamic class you should broaden and give an open void run() technique TimerTask gives an (actualized) open boolean cross out() technique Returns false if there were no planned executions to scratch off

Slide 9

The Model class, I class Model amplifies Observable { open last int BALL_SIZE = 20; private int xPosition = 0; private int yPosition = 0; private int xLimit, yLimit; private int xDelta = 6; private int yDelta = 4;/strategies (on next slide) }

Slide 10

The Model class, II open void setLimits(int xLimit, int yLimit) { this.xLimit = xLimit - BALL_SIZE; this.yLimit = yLimit - BALL_SIZE; } open int getX() { return xPosition; } open int getY() { return yPosition; } open void makeOneStep() {/code for making one stage (on next slide) }

Slide 11

The Model class, III open void makeOneStep() {/Do the work xPosition += xDelta; if (xPosition < 0 || xPosition >= xLimit) { xDelta = - xDelta; xPosition += xDelta; } yPosition += yDelta; if (yPosition < 0 || yPosition >= yLimit) { yDelta = - yDelta; yPosition += yDelta; }/Notify onlookers setChanged(); notifyObservers(); }

Slide 12

The View class import java.awt.*; import java.util.*; class View augments Panel executes Observer { model; View(Model model) { this.model = model; } open void paint(Graphics g) { g.setColor(Color.red); g.fillOval(model.getX(), model.getY(), model.BALL_SIZE, model.BALL_SIZE); } open void update(Observable obs, Object arg) { repaint(); }

Slide 13

The Controller class, I import java.applet.*; import java.awt.*; import java.awt.event.*; import java.util.Timer; import java.util.TimerTask; import javax.swing.*; open class Controller augments JApplet { JPanel buttonPanel = new JPanel(); JButton runButton = new JButton("Run"); JButton stopButton = new JButton("Stop"); Timer clock; model = new Model(); view = new View(model);/View must think about Model

Slide 14

The Controller class, II open void init() { layOutComponents(); attachListenersToComponents();/Connect model and view model.addObserver(view); } private void layOutComponents() { setLayout(new BorderLayout()); this.add(BorderLayout.SOUTH, buttonPanel); buttonPanel.add(runButton); buttonPanel.add(stopButton); stopButton.setEnabled(false); this.add(BorderLayout.CENTER, view); }

Slide 15

The Controller class, III private void attachListenersToComponents() { runButton .addActionListener(new ActionListener() { open void actionPerformed(ActionEvent occasion) { runButton.setEnabled(false); stopButton.setEnabled(true); clock = new Timer(true); timer.schedule(new Strobe (), 0, 40);/25 times each second }); stopButton .addActionListener(new ActionListener() { open void actionPerformed(ActionEvent occasion) { runButton.setEnabled(true); stopButton.setEnabled(false); timer.cancel(); }); }

Slide 16

The Controller class, IV private class Strobe augments TimerTask { open void run() { model.setLimits(view.getWidth(), view.getHeight()); model.makeOneStep(); }

Slide 17

Summary In this project I utilized: Model-View-Controller This is a decent outline design for some uses; it isolates the "business rationale" (the Model) from the classes that are fundamentally I/O Observer-Observable This is a decent outline design for disengaging the Model from the View Threads If you need to have a controllable liveliness, Threads are vital The activity keeps running in one Thread, the controls in another Timers are a helpful approach to plan frequently rehashing undertakings With a somewhat diverse configuration, you could utilize Thread.sleep( ms ) rather

Slide 18

The End

Recommended
View more...