Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming Languages and Techniques (CIS120)

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

Programming Languages and Techniques (CIS120)

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

CIS 120 Programming Languages and Techniques. Final Exam, May 3, 2011

Handling Mouse and Keyboard Events

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

CSSE 220. Event Based Programming. Check out EventBasedProgramming from SVN

Programming Languages and Techniques (CIS120)

CIS 120 Final Exam May 7, Name (printed): Pennkey (login id):

GUI in Java TalentHome Solutions

Programming Languages and Techniques (CIS120)

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

AP Computer Science Unit 13. Still More Graphics and Animation.

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

Java Mouse Actions. C&G criteria: 5.2.1, 5.4.1, 5.4.2,

Programming Languages and Techniques (CIS120)

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Appendix G Navigation and Collision Detection for Web-Based 3D Experiment

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CS 134 Test Program #2

Computer Science 210: Data Structures. Intro to Java Graphics

Graphical User Interfaces 2

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

class BankFilter implements Filter { public boolean accept(object x) { BankAccount ba = (BankAccount) x; return ba.getbalance() > 1000; } }

Lecture 19 GUI Events

CIS 120 Final Exam 16 December Name (printed): Pennkey (login id):

CSE 331 Software Design & Implementation

The AWT Event Model 9

H212 Introduction to Software Systems Honors

THE UNIVERSITY OF AUCKLAND

User interfaces and Swing

Advanced Java Programming (17625) Event Handling. 20 Marks

Unit 7: Event driven programming

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

IT101. Graphical User Interface

Exam: Applet, Graphics, Events: Mouse, Key, and Focus

G51PRG: Introduction to Programming Second semester Applets and graphics

SAMPLE EXAM Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

GUI Event Handlers (Part I)

CSIS 10A Practice Final Exam Solutions

Lecture 5: Java Graphics

Design Patterns: Prototype, State, Composite, Memento

(listener)... MouseListener, ActionLister. (adapter)... MouseAdapter, ActionAdapter. java.awt AWT Abstract Window Toolkit GUI

Programming Languages 2nd edition Tucker and Noonan"

Final Exam. Name: Student ID Number: Signature:

PESIT Bangalore South Campus

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

ISO-2022-JP (JIS ) 1 2. (Windows95/98 MacOS ) Java UNICODE UNICODE. Java. .java.java.txt native2ascii. .java

BASICS OF GRAPHICAL APPS

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

Java for Interfaces and Networks (DT3010, HT10)

GUI Event Handlers (Part II)

Chapter 14: Applets and More

8/23/2014. Chapter Topics. Introduction to Applets. Introduction to Applets. Introduction to Applets. Applet Limitations. Chapter 14: Applets and More

Chapter 1 GUI Applications

Event Binding. Different Approaches Global Hooks. 2.5 Event Binding 1

Cheng, CSE870. More Frameworks. Overview. Recap on OOP. Acknowledgements:

The init() Method. Browser Calling Applet Methods

UCLA PIC 20A Java Programming

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Programming Languages and Techniques (CIS120)

Component-Based Behavioural Modelling with High-Level Petri Nets

CS2110. GUIS: Listening to Events Also anonymous classes versus Java 8 functions. Anonymous functions. Anonymous functions. Anonymous functions

Lecture 3: Java Graphics & Events

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

Chapter 14: Applets and More

CS 180 Problem Solving and Object Oriented Programming Fall 2011

I/O Framework and Case Study. CS151 Chris Pollett Nov. 2, 2005.

1.00/1.001 Introduction to Computers and Engineering Problem Solving Spring Quiz 2

EINFÜHRUNG IN DIE PROGRAMMIERUNG

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Events. Dispatch, event-to-code binding. Review: Events Defined 1/17/2014. occurrence.

GUI (Graphic User Interface) Programming. Part 2 (Chapter 8) Chapter Goals. Events, Event Sources, and Event Listeners. Listeners

javax.swing Swing Timer

CS 2113 Software Engineering

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Class 16: The Swing Event Model

Widgets. Overview. Widget. Widgets Widget toolkits Lightweight vs. heavyweight widgets Swing Widget Demo

Come & Join Us at VUSTUDENTS.net

CS2110. GUIS: Listening to Events

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

Windows and Events. created originally by Brian Bailey

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

UML and OOD. Some terms related to OOD. Dependency. Coupling. Interfaces. Multiple Interfaces

Transcription:

Programming Languages and Techniques (CIS120) Lecture 36 November 30, 2018 Mushroom of Doom Model / View / Controller Chapter 31

Announcements Game Project Complete Code Due: Monday, December 10th NO LATE SUBMISSIONS PERMITTED Final Exam Thursday, December 20 th, 6:00-8:00PM Locations: TBA makeup exam form available on the course web site

Paint Revisited (thoroughly discussed in Chap 31) Using Anonymous Inner Classes Refactoring for OO Design (See PaintA.java PaintE.java)

Adapters MouseAdapter KeyAdapter

Mouse Interaction in Paint Point Mode Mouse Released (in the canvas) [add new point] Line Button press Point Button press Mouse Pressed [store point, set preview shape] Mouse Dragged [update preview] LineStart Mode LineEnd Mode Mouse Released [add new line, set preview to null]

Two interfaces for mouse listeners interface MouseListener extends EventListener { public void mouseclicked(mouseevent e); public void mouseentered(mouseevent e); public void mouseexited(mouseevent e); public void mousepressed(mouseevent e); public void mousereleased(mouseevent e); } interface MouseMotionListener extends EventListener { public void mousedragged(mouseevent e); } public void mousemoved(mouseevent e);

Lots of boilerplate There are seven methods in the two interfaces. We only want to do something interesting for three of them. Need "trivial" implementations of the other four to implement the interface public void mousemoved(mouseevent e) { return; } public void mouseclicked(mouseevent e) { return; } public void mouseentered(mouseevent e) { return; } public void mouseexited(mouseevent e) { return; } Solution: MouseAdapter class

Adapter classes: Swing provides a collection of abstract event adapter classes These adapter classes implement listener interfaces with empty, do-nothing methods To implement a listener class, we extend an adapter class and override just the methods we need private class Mouse extends MouseAdapter { public void mousepressed(mouseevent e) { } public void mousereleased(mouseevent e) { } public void mousedragged(mouseevent e) { } }

Mushroom of Doom How do we put Swing components together to make a complete game?

Game State GameCourt snitch poison square playing true Circle pos_x 170 pos_y 170 v_x 2 v_y 3 Square pos_x 0 pos_y 0 v_x 0 v_y 0 Poison pos_x 130 pos_y 130 v_x 0 v_y 0

How can we share code between the game objects, but show them differently?

Updating the Game State: timer void tick() { if (playing) { square.move(); snitch.move(); snitch.bounce(snitch.hitwall()); // bounce off walls... snitch.bounce(snitch.hitobj(poison)); //...and the mushroom } } if (square.intersects(poison)) { playing = false; status.settext("you lose!"); } else if (square.intersects(snitch)) { playing = false; status.settext("you win!"); } repaint();

Updating the Game State: keyboard setfocusable(true); addkeylistener(new KeyAdapter() { public void keypressed(keyevent e) { if (e.getkeycode() == KeyEvent.VK_LEFT) square.v_x = -SQUARE_VELOCITY; else if (e.getkeycode() == KeyEvent.VK_RIGHT) square.v_x = SQUARE_VELOCITY; else if (e.getkeycode() == KeyEvent.VK_DOWN) square.v_y = SQUARE_VELOCITY; else if (e.getkeycode() == KeyEvent.VK_UP) square.v_y = -SQUARE_VELOCITY; } Make square's velocity nonzero when a key is pressed }); public void keyreleased(keyevent e) { square.v_x = 0; square.v_y = 0; } Make square's velocity zero when a key is released

How does the user interact with the game? 1. Clicking Reset button restarts the game 2. Holding arrow key makes square move 3. Releasing key makes square stop

Model View Controller Design Pattern

Presented by MVC Pattern Manipulates Model Adjusts View(s) Controller Sees Uses User

Example 1: Mushroom of Doom

Example: MOD Program Structure GameCourt, GameObj + subclass local state object location & velocity status of the game (playing, win, loss) how the objects interact with eachother (tick) Model Draw methods paintcomponent in GameCourt draw methods in GameObj subclasses status label View Game / GameCourt Reset button (updates model) Keyboard control (updates square velocity) Controller

Example: Paint Program Structure Main frame for application (class Paint) List of shapes to draw The current color The current line thickness Drawing panel (class Canvas, inner class of Paint) Model View Control panel (class JPanel Contains radio buttons for selecting shape to draw Line thickness checkbox, undo and quit buttons Controller Connections between Preview shape (if any ) Preview Shape: View <-> Controller MouseAdapter: Controller <-> Model

Example: CheckBox true or false Views Selected? Pressed? Model setselected mouselistener keylistener Controllers Class JToggleButton.ToggleButtonModel boolean isselected() Checks if the button is selected. void setpressed(boolean b) Sets the pressed state of the button. void setselected(boolean b) Sets the selected state of the button.

Example: Chat Server getchannels getusers getowner Views Internal Representation owners: Map<Channel, Users> users: Map<Channel, Set<Users>> Model createchannel joinchannel invite kick Controllers ServerModel

Example: Web Pages Internal Representation: DOM (Document Object Model) Model JavaScript API document. addeventlistener() Controllers Views

MVC Pattern Updates Manipulates Model Adjusts View(s) Controller Sees Uses User

MVC Benefits? Decouples important "model state" from how that state is presented and manipulated Suggests where to insert interfaces in the design Makes the model testable independent of the GUI Multiple views e.g. from two different angles, or for multiple different users Multiple controllers e.g. mouse vs. keyboard interaction

MVC Variations Many variations on MVC pattern Hierarchical / Nested As in the Swing libraries, in which JComponents often have a "model" and a "controller" part Coupling between Model / View or View / Controller e.g. in MOD the Model and the View are coupled because the model carries most of the information about the view

Design Patterns Design Patterns Influential OO design book published in 1994 (so a bit dated) Identifies many common situations and "patterns" for implementing them in OO languages Some we have seen explicitly: e.g. Iterator pattern Some we've used but not explicitly described: e.g. The Broadcast class from the Chat HW uses the Factory pattern Some are workarounds for OO's lack of some features: e.g. The Visitor pattern is like OCaml's fold + pattern matching