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)

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

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Computer Science 210: Data Structures. Intro to Java Graphics

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

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

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

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

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

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

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

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Graphical User Interfaces 2

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

Programming Languages and Techniques (CIS120)

User interfaces and Swing

Graphical User Interfaces 2

Programming Languages and Techniques (CIS120)

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

Handling Mouse and Keyboard Events

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

Component-Based Behavioural Modelling with High-Level Petri Nets

Java for Interfaces and Networks (DT3010, HT10)

GUI in Java TalentHome Solutions

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

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

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

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

Unit 7: Event driven programming

Chapter 14: Applets and More

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

UCLA PIC 20A Java Programming

Programming Languages and Techniques (CIS120)

Object Orientated Programming in Java. Benjamin Kenwright

Design Patterns: Prototype, State, Composite, Memento

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

Chapter 1 GUI Applications

G51PRG: Introduction to Programming Second semester Applets and graphics

PESIT Bangalore South Campus

H212 Introduction to Software Systems Honors

The AWT Event Model 9

IT101. Graphical User Interface

CS 180 Problem Solving and Object Oriented Programming Fall 2011

Lecture 5: Java Graphics

CSIS 10A Practice Final Exam Solutions

Lecture 3: Java Graphics & Events

Programming Languages and Techniques (CIS120)

Windows and Events. created originally by Brian Bailey

Advanced Java Programming

(Incomplete) History of GUIs

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

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

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

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

CS 2113 Software Engineering

Programming Languages 2nd edition Tucker and Noonan"

CS 106A, Lecture 14 Events and Instance Variables

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

L4,5: Java Overview II

2IS45 Programming

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

CS 180 Problem Solving and Object Oriented Programming Fall 2011

J.73 J.74 THE I/O PACKAGE. Java I/O is defined in terms of streams. Streams are ordered sequences of data that have a source and a destination.

CS2110. GUIS: Listening to Events. Anonymous functions. Anonymous functions. Anonymous functions. Checkers.java. mainbox

Swing Rocks - A Tribute to Filthy Rich Clients

CS2110. GUIS: Listening to Events

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

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

Lecture 19 GUI Events

Class Hierarchy and Interfaces. David Greenstein Monta Vista High School

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

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

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

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Java Programming Lecture 6

H212 Introduction to Software Systems Honors

Dartmouth College Computer Science 10, Winter 2012 Solution to Midterm Exam

CSE 331 Software Design & Implementation

Dartmouth College Computer Science 10, Fall 2015 Midterm Exam

CIS 120 Midterm II November 16, 2012 SOLUTIONS

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Information Science and Engineering

Programming Languages and Techniques (CIS120)

FirstSwingFrame.java Page 1 of 1

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

CIS 120 Final Exam 19 December 2014 SOLUTIONS

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

Lab 4. D0010E Object-Oriented Programming and Design. Today s lecture. GUI programming in

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

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

javax.swing Swing Timer

Transcription:

Programming Languages and Techniques (CIS120) Lecture 35 April 15, 2013 Swing III: OO Design, Mouse InteracGon

Announcements HW10: Game Project is out, due Tuesday, April 23 rd at midnight If you want to do a game other than one of the ones listed, send email to tas120@seas.upenn.edu (or check on Piazza)

Java Paint

Basic structure Main frame for applicagon (class Paint) the MODEL Drawing panel (class Canvas, inner class of Paint) the VIEW Control panel (class JPanel) the CONTROLLER Contains radio buvons for selecgng shape to draw Line thickness checkbox, undo and quit buvons Paint class contains the state of the applicagon List of shapes to draw Preview shape (if any ) The current color (will always be BLACK today) The current line thickness References to UI components: canvas, modetoolbar

Program Design How does our treatment of shape drawing in Java compare with the OCaml GUI project?

Java Version of Paint public interface Shape { } public void draw(graphics2d gc); Interface describes what shapes can do public class PointShape implements Shape { } public class LineShape implements Shape { } Classes describe how to draw themselves private class Canvas extends JPanel { public void paintcomponent(graphics gc) { super.paintcomponent(gc); for (Shape s : actions) s.draw((graphics2d)gc); if (preview!= null) preview.draw((graphics2d)gc); } } Canvas uses dynamic dispatch to draw the shapes 6

OCaml Version of Paint type shape = Points of Gctx.color * int * point list Line of Gctx.color * int * point * point Datatypes define the structure of informagon. let repaint (g:gctx.t) : unit = let draw_shape (s:shape) : unit = begin match s with Points (c,t,ps) -> Line (c,t,p1,p2) -> end in Deque.iterate draw_shape paint.shapes; begin match paint.preview with None -> () Some d -> draw_shape d end Drawing operagon is defined externally to the datatype and uses case analysis to dispatch. The main loop looks very similar. 7

Comparison with OCaml How does our treatment of shape drawing in the Java Paint example compare with the OCaml GUI project? Java: Interface Shape for drawable objects Classes implement that interface Canvas uses dynamic dispatch to draw the shapes Add more shapes by adding more implementagons of "Shape" OCaml Datatype specifies variants of drawable objects Canvas uses pavern matching to draw the shapes Add more shapes by adding more variants, and modifying drawit

Datatypes vs. Objects Datatypes Focus on how the data is stored Easy to add new operagons Hard to add new variants Objects Focus on what to do with the data Easy to add new variants Hard to add new operagons Best for: situagons where the structure of the data is fixed (i.e. BSTs) Best for: situagons where the interface with the data is fixed (i.e. Shapes) 9

Mouse InteracGon How do we draw shapes on the canvas?

Mouse InteracGon One OpGon: Copy OCaml structure public enum Mode {! PointMode, LineStartMode, LineEndMode! }! private Mode mode = Mode.PointMode; BuVon press switches between PointMode and LineStartMode Mouse click in PointMode add a new point to the list of shapes Mouse press in LineStartMode remember locagon, switch to LineEndMode, remember preview shape Mouse movement in LineEndMode update preview shape Mouse release in LineEndMode add a new line to list of shapes, switch to LineStartMode, 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 interesgng for three of them. Need "trivial" implementagons 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; } SoluGon: MouseAdapter class

Adapter classes: Swing provides a collecgon 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) { } }!

OO Mouse InteracGon What about OO version