7-dec-15 animation. hw 10 last hw (i messed up some of the scheduling) extension until monday night...

21
Jun 23, 2 022 Animation

Upload: randolph-perkins

Post on 17-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

Apr 21, 2023

Animation

Page 2: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

HW 10

Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for this one The main goal of this assignment is to get the game to

work with your own design. We will not be strict about design. It is too subjective to judge

anyway. We recognize this is a tough assignment. Grading will be fairly

lenient Please show up on Friday. We will do some combination

of review + office hours

2

Page 3: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

HW 10 Unit tests are definitely needed. Especially for complicated

things like checking for a win. Javadoc

This includes javadoc for Dr Dave’s code. By editing the javadoc of his code you demonstrate you understand it at least at a high level.

Javadoc has to be placed above the method name. Please fill out all the information.

Please put your partner’s information Canvas comment In the author section of any class you make Not putting your partner’s info will result in both of you losing 0.5 pts Even if your partner has done 0 work, not putting their name down is not

the way to go about ‘complaining’ (talking to me is what you need to do)

3

Page 4: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

A bit more on object oriented design

What to put where in HW 10? Do not force everything into MVC Rather think of this as different classes interacting with

each other LinesOfAction (main class) A board 2 players RoundPieces An array/arraylist/vector of pieces

4

Page 5: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

Example of design in HW 10

Who should check for a win? What information do we need to determine a victory? What decision is a player supposed to make? Can we make more classes? Can we make changes in the provided classes?

5

Page 6: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

6

Moving pictures

Animation—making objects that appear to move on the screen—is done by displaying a series of still pictures, one after the other, in rapid succession Generally you should try for at least 20 pictures/second 20 pictures/second is repainting every 50 milliseconds

Page 7: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

7

The bouncing ball

“Bouncing ball” is the “Hello World” of animation

We will develop this program using: Model-View-Controller Observer-Observable Threads Timers

Page 8: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

8

Review of MVC

MVC stands for Model-View-Controller The Model is the actual internal representation

It should be independent of the other classes It’s handy if this class can extend Observable

The View (or a View) is a way of looking at or displaying the model

It’s handy if this class implements Observer The Controller provides for user input and modification

These three components are usually implemented as separate classes (or sets of classes)

Page 9: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

9

Review of Observer and Observable

java.util.Observable is a class When it does something that should be observed, it says:

setChanged(); notifyObservers(); /* or */ notifyObservers(arg);

java.util.Observer is an interface It has to register itself with (subscribe to) an Observable:

myObservable.addObserver(myObserver); It has to implement:

public void update(Observable obs, Object arg) This method is automatically called when observers are notified obs is the object being observed If the Observable did notifyObservers(), arg is null

Page 10: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

10

Using the Runnable interface Implementing the Runnable interface:

class Animation implements Runnable {…} You must implement public void run( ) To make it “go”:

Animation anim = new Animation( ); Thread myThread = new Thread(anim);myThread.start( );

A Thread is one method of implementing concurrency in your programs (imprecise very high level view)

 It is a program's path of execution

Page 11: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

11

Timers A java.util.Timer is used to schedule code for future execution A Timer may:

Schedule a one-time execution, or Schedule repeated executions at regular intervals

Timer constructors: Timer() Timer(boolean isDaemon) Timer(String name) Timer(String name, boolean isDaemon)

A Timer can keep an application from terminating, unless it is specified as a daemon thread

A daemon thread dies if there are no no-daemon threads running Create daemon Timer threads with new Timer(true) or

new Timer(name, true)

Page 12: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

12

Using a Timer for animation public void schedule(TimerTask task, long delay, long period)

Schedules the specified task for repeated fixed-delay execution, beginning after the specified delay (which may be zero)

Subsequent executions take place at approximately regular intervals separated by the specified period

Times are specified in milliseconds (1/1000s of a second)

Notice that schedule requires a TimerTask as an argument TimerTask is an abstract class you must extend and provide a

public void run() method TimerTask provides an (implemented) public boolean cancel()

method Returns false if there were no scheduled executions to cancel

Page 13: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

13

The Model class, I

import java.util.Observable;

class Model extends Observable { public final int BALL_SIZE = 20; private int xPosition = 0; private int yPosition = 0; private int xLimit, yLimit; private int xDelta = 6; private int yDelta = 4;

// methods (on next slide)

}

Page 14: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

14

The Model class, II public void setLimits(int xLimit, int yLimit) {

this.xLimit = xLimit - BALL_SIZE; this.yLimit = yLimit - BALL_SIZE; } public int getX() { return xPosition; } public int getY() { return yPosition; } public void makeOneStep() { // code for making one step (on next slide) }

Page 15: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

15

The Model class, III public 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 observers setChanged(); notifyObservers(); }

Page 16: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

16

The View class import java.awt.*;

import java.util.*; import javax.swing.JPanel;

class View extends JPanel implements Observer { Model model;

View(Model model) { this.model = model; } @Override public void paint(Graphics g) { g.setColor(Color.WHITE); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.RED); g.fillOval(model.getX(), model.getY(), model.BALL_SIZE, model.BALL_SIZE); } public void update(Observable obs, Object arg) { repaint(); }}

Page 17: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

17

The Controller class, I import java.awt.*;

import java.awt.event.*;import java.util.Timer;import java.util.TimerTask;import javax.swing.*;

public class Controller extends JFrame { JPanel buttonPanel = new JPanel(); JButton runButton = new JButton("Run"); JButton stopButton = new JButton("Stop"); Timer timer; Model model = new Model();

View view = new View(model); // View must know about Model

Page 18: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

18

The Controller class, II public 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); }

Page 19: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

19

The Controller class, III private void attachListenersToComponents() {

runButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { runButton.setEnabled(false); stopButton.setEnabled(true); timer = new Timer(true); timer.schedule(new Strobe(), 0, 40); // 25 times a second } }); stopButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { runButton.setEnabled(true); stopButton.setEnabled(false); timer.cancel(); } }); }

Page 20: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

20

The Controller class, IV @Override

private class Strobe extends TimerTask { public void run() { model.setLimits(view.getWidth(), view.getHeight()); model.makeOneStep(); } }

public static void main(String[] args) { ControllerApp controllerApp = new ControllerApp(); controllerApp.init(); controllerApp.setSize(300, 300); controllerApp.setVisible(true); controllerApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}

Page 21: 7-Dec-15 Animation. HW 10 Last HW (I messed up some of the scheduling) Extension until Monday night 11:30 pm Late penalty will be strictly enforced for

Doing something else while the ball is bouncing

Just making the ball change colour when the up/down arrow key is pressed

Also drawing a horizontal line. Shows you how to do some usage of key listener

21