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)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

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

Graphical User Interfaces 2

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

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

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

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

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

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

Handling Mouse and Keyboard Events

Programming Languages and Techniques (CIS120)

CS 134 Test Program #2

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

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

GUI in Java TalentHome Solutions

Graphical User Interfaces 2

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

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

Computer Science 210: Data Structures. Intro to Java Graphics

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

Graphical User Interfaces 2

Lecture 19 GUI Events

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

H212 Introduction to Software Systems Honors

CSE 331 Software Design & Implementation

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

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

Lecture 5: Java Graphics

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

User interfaces and Swing

THE UNIVERSITY OF AUCKLAND

Systems Programming Graphical User Interfaces

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

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

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

Unit 7: Event driven programming

BASICS OF GRAPHICAL APPS

Final Exam. Name: Student ID Number: Signature:

Our second exam is Thursday, November 10. Note that it will not be possible to get all the homework submissions graded before the exam.

The AWT Event Model 9

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

G51PRG: Introduction to Programming Second semester Applets and graphics

IT101. Graphical User Interface

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

Chapter 1 GUI Applications

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

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

Programming Languages and Techniques (CIS120e)

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

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

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

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Programming Languages 2nd edition Tucker and Noonan"

Phase 2: Due 11/28/18: Additional code to add, move and remove vertices in response to user mouse clicks on the screen.

Advanced Java Programming (17625) Event Handling. 20 Marks

GUI Event Handlers (Part I)

CS 251 Intermediate Programming GUIs: Components and Layout

CSIS 10A Practice Final Exam Solutions

Widgets. Widgets Widget Toolkits. User Interface Widget

Programming Languages and Techniques (CIS120)

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

Breakout YEAH hours. Brahm Capoor & Jared Wolens

Our second exam is Monday, April 3. Note that it will not be possible to get all the homework submissions graded before the exam.

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

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

Design Patterns: Prototype, State, Composite, Memento

Lecture 3: Java Graphics & Events

CS2110 Fall 2015 Assignment A5: Treemaps 1

Class 16: The Swing Event Model

Assignment 2 Team Project: Distributed System and Application

Java Programming Lecture 6

UI Software Organization

Come & Join Us at VUSTUDENTS.net

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Adapted from slides by Brahm Capoor. Breakout YEAH hours. Michael (Chung Troute)

Java for Interfaces and Networks (DT3010, HT10)

The init() Method. Browser Calling Applet Methods

Windows and Events. created originally by Brian Bailey

UCLA PIC 20A Java Programming

CS 2113 Software Engineering

PESIT Bangalore South Campus

Phase 2: Due 05/02/18 The Gui now stores and displays a graph in response to user mouse clicks on the screen.

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

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

Transcription:

Programming Languages and Techniques (CIS120) Lecture 34 April 13, 2017 Model / View / Controller Chapter 31

How is the Game Project going so far? 1. not started 2. got an idea 3. submitted design proposal 4. started coding 5. it's somewhat working 6. it's mostly working 7. debugging / polishing 8. done!

Announcements (Review) Final exam: May 2, 3-5PM If you have two finals at the same time, you can postpone one If you have 3 finals on one day, you may postpone the middle one If either case applies to you, add your name to the form on the website Must discuss all other exam conflicts with course instructors

Game project (Review) Game Design Proposal Milestone Due: (12 points) TOMORROW at NOON!!!! (Should take about 1 hour) Submit on GRADESCOPE TAs will give you feedback over the weekend Final Program Due: (88 points) Wednesday, April 25 th at 11:59pm Submit zipfile online, submission only checks if your code compiles Eclipse is STRONGLY recommended for this project May distribute your game (after the deadline) if you do not use any of our code Grade based on demo with your TA during reading days Grading rubric on the assignment website Recommendation: don t be too ambitious. NO LATE SUBMISSIONS PERMITTED

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

What layout would you use for this app? What components would you use?

Canvas subclass of JPanel (canvas) JPanel (toolbar) JRadioButton (point, line) JCheckbox (thick) JButton (quit)

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; } public void keyreleased(keyevent e) { square.v_x = 0; square.v_y = 0; } }); Make square's velocity nonzero when a key is pressed 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