Programming Languages and Techniques (CIS120)

Similar documents
Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Name: Pennkey: CIS 120 Midterm II November 18, 2011

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Graphical User Interface (GUI)

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

Lecture 15. For-Loops

CSC207H: Software Design Lecture 11

Graphical User Interface (GUI)

CISC 1600 Lecture 3.1 Introduction to Processing

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. Event Architecture. A pipeline: Event Capture

Event Dispatch. Dispatching events to windows and widgets.

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. 2.4 Event Dispatch 1

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 16, 2012 SOLUTIONS

Programming Languages and Techniques (CIS120)

Tables Part I. Session 45: Creating Tables Session 46: Modifying the Layout of Tables Session 47: Formatting the Design of Tables. Unit.

Model-view-controller View hierarchy Observer

Programming Training. This Week: Tkinter for GUI Interfaces. Some examples

TWO-DIMENSIONAL FIGURES

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

2IS45 Programming

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1


Output in Window Systems and Toolkits

Chapter 7 Applets. Answers

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

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Programming Languages and Techniques (CIS120)

Object-Oriented Programming Design. Topic : Graphics Programming GUI Part I

Programming Languages and Techniques (CIS120)

Lecture 13 CIS 341: COMPILERS

IT101. Graphical User Interface

Layout. Dynamic layout, Swing and general layout strategies

CSCI 053. Week 5 Java is like Alice not based on Joel Adams you may want to take notes. Rhys Price Jones. Introduction to Software Development

What is Widget Layout? Laying Out Components. Resizing a Window. Hierarchical Widget Layout. Interior Design for GUIs

Programming Languages and Techniques (CIS120)

Tcl/Tk lecture. What is the Wish Interpreter? CIS 410/510 User Interface Programming

Visual C# Program: Simple Game 3

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120)

P3e REPORT WRITER CREATING A BLANK REPORT

Programming Languages and Techniques (CIS120)

CIS 120 Midterm II November 16, Name (printed): Pennkey (login id):

The Racket Graphical Interface Toolkit

CIS 120 Midterm II March 31, 2017 SOLUTIONS

Programming Languages and Techniques (CIS120)

MS Word 2007: Graphics. Lesson Notes Author: Pamela Schmidt. The Drawing Tools Format Ribbon appears when the object is selected.

Sri Vidya College of Engineering & Technology

Java Programming Lecture 6

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Lecture 11: Output 2. Fall UI Design and Implementation 1

Lecture 11: Toolkits. Fall UI Design and Implementation 1

Dell Canvas Layout. Version 1.0 User s Guide

CSE 438: Mobile Application Development Lab 2: Virtual Pet App

Basic Excel 2010 Workshop 101

MOBILE COMPUTING 1/20/18. How many of you. CSE 40814/60814 Spring have implemented a command-line user interface?

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!

User interfaces and Swing

CS153: Compilers Lecture 15: Local Optimization

9 Tables Part I. Session 45: Creating Tables Session 46: Modifying the Layout of Tables Session 47: Formatting the Design of Tables. Unit.

Labels and Envelopes in Word 2013

CIS 120 Midterm II November 8, 2013 SOLUTIONS

Problem Set 1 Due: 11:59pm Wednesday, February 7

Which of the following expressions has the type int list?

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

Forward recursion. CS 321 Programming Languages. Functions calls and the stack. Functions calls and the stack

Warping & Blending AP

Platform Games Drawing Sprites & Detecting Collisions

GUI Basics and Windowing Systems

Programming Languages and Techniques (CIS120)

CISC 1600, Lab 3.1: Processing

EZ- ASCII: Language Reference Manual

More Skills 11 Draw Tables and Convert Tables to Text. To complete this document, you will need the following file:

CIS 121 Data Structures and Algorithms with Java Spring 2018

1 Getting started with Processing

Assignment 3: Inheritance

CISC 1600, Lab 2.1: Processing

Programming Languages and Techniques (CIS120)

1 Getting started with Processing

Painting your window

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

Programming Assignment 7 (100. Points)

Client-Side Web Technologies. CSS Part II

Announcements. Android: n-puzzle Walkthrough. Tommy MacWilliam. Dynamic GUIs. ListViews. Bitmaps. Gameplay. Saving State. Menus

CS 201 Advanced Object-Oriented Programming Lab 10 - Recursion Due: April 21/22, 11:30 PM

+! Today. Lecture 3: ArrayList & Standard Java Graphics 1/26/14! n Reading. n Objectives. n Reminders. n Standard Java Graphics (on course webpage)

SNS COLLEGE OF ENGINEERING, Coimbatore

CSE115 Lab 4 Fall 2016

Paint Tutorial (Project #14a)

SIMPLE APPLET PROGRAM

Java Programming. Computer Science 112

UI Toolkits. HCID 520 User Interface Software & Technology

Guide to WB Annotations

Graphics in Swing. Engineering 5895 Faculty of Engineering & Applied Science Memorial University of Newfoundland

Introducing Motif. Motif User s Guide 1

Transcription:

Programming Languages and Techniques (CIS120) Lecture 19 February 26, 2016 GUI Library Design Chapter 18

Are you here today? 1. Yes

Announcements HW05: GUI programming is available Due: THURSDAY March 3rd at 11:59:59pm Graded manually Submission only checks for compilation, no auto tests Won t get scores immediately Only LAST submission will be graded This project is challenging: Requires working with multiplelevels of abstraction. Managing state in the paint program is a bit tricky.

Building a GUI library & application

GUI Library Design putting objects to work

Interfaces: Project Architecture* *Note: Subsequent program snippets are color- coded according to this diagram. Application Paint GUI Library Eventloop Widget Gctx Native graphics library OCaml s Graphics Module (graphics.cma) Goal of the GUI library: provide a consistent layer of abstraction between the application (Paint) and the Graphics module. 6

GUI terminology Widget* Basic element of GUIs : buttons, checkboxes, windows, textboxes, canvases, scrollbars, labels All have a position on the screen and know how to display themselves May be composed of other widgets (for layout) Hello World *Each GUI library uses its own naming convention for what we call Widget. Java s Swing calls them Components ; ios UIKit calls them UIViews ; WINAPI, GTK+, X11 s widgets, etc. 7

Widgets Pictorially swdemo.ml (* Create some simple label widgets *) let l1 = label "Hello" let l2 = label "World" (* Compose them horizontally, adding some borders *) let h = border (hpair (border l1) (hpair (space (10,10)) (border l2))) border hpair Hello World border hpair label space border On the screen Widget tree label

GUI terminology - Eventloop Main loop of any GUI application let run (w:widget) : unit = let g = Gctx.top_level in Gctx.open_graphics (); let rec loop () : unit = Graphics.clear_graph (); w.repaint g; Graphics.synchronize (); (* force window update *) wait for user input (mouse movement, key press) inform w about the input so widgets can react to it; loop () (* tail recursion! *) in loop () Takes top- level widget w as argument. That widget contains all others in the application. 9

Drawing: Containers Container widgets propagate repaint commands to their children: border.repaint g hpair.repaint g1 Hello World border hpair.repaint g2 label space border.repaint g3 label.repaint g4 Challenge: How can we make it so that the functions that draw widgets in different places on the window are location independent?

Challenge: Widget Layout Widgets are things drawn on the screen. How to make them location independent? Idea: Use a graphics context to make drawing relative to the widget s current position Application GUI Library Native graphics library Paint.ml Eventloop.ml Widget.ml Gctx.ml OCaml s Graphics Module (graphics.cma) The graphics context isolates the widgets from the Graphics module. 12

GUI terminology Graphics Context Wraps OCaml Graphics library; puts drawing operations in context Translates coordinates Flips between OCaml and Standard coordinates so origin is top- left Translates coordinates so all widgets can pretend that they are at the origin Also aggregates information about the way things are drawn foreground color line width 13

Module: Gctx Contextualizes graphics drawing operations

Graphics Contexts let top = Gctx.top_level dx dy CIS 120 let nctx = Gctx.translate top (dx,dy) CIS 120 draw_string top (0,10) "CIS 120"; draw_string nctx (0,10) "CIS 120" repaint = fun g -> draw_rect g (0,0) (20,20); draw_string g (0,10) "CIS 120" 17

Module Gctx (** The main (abstract) type of graphics contexts. *) type gctx (** The top-level graphics context *) val top_level : gctx (** A widget-relative position *) type position = int * int (** Display text at the given position *) val draw_string : gctx -> position -> string -> unit (** Draw a line between the two specified positions *) val draw_line : gctx -> position -> position -> unit (** Produce a new gctx shifted by (dx,dy) *) val translate : gctx -> int * int -> gctx (** Produce a new gctx with a different pen color *) val with_color : gctx -> color -> gctx

Module: Widgets Building blocks of GUI applications see simplewidget.ml

Simple Widgets simplewidget.mli (* An interface for simple GUI widgets *) type widget = { repaint : Gctx.gctx -> unit; size : unit -> (int * int) } val label : string -> widget val space : int * int -> widget val border : widget -> widget val hpair : widget -> widget -> widget val canvas : int * int -> (Gctx.gctx -> unit) -> widget You can ask a simple widget to repaint itself. You can ask a simple widget to tell you its size. Both operations are relative to a graphics context

swdemo.ml 21

Widget Examples simplewidget.ml (* A simple widget that puts some text on the screen *) let label (s:string) : widget = { repaint = (fun (g:gctx.gctx) -> Gctx.draw_string g (0,0) s); size = (fun () -> Gctx.text_size s) } simplewidget.ml (* A "blank" area widget -- it just takes up space *) let space ((x,y):int*int) : widget = { repaint = (fun (_:Gctx.gctx) -> ()); size = (fun () -> (x,y)) }

The canvas Widget Region of the screen that can be drawn upon Has a fixed width and height Parameterized by a repaint method Use the Gctx drawing routines to draw on the canvas simplewidget.ml let canvas ((w,h):int*int) (repaint: Gctx.gctx -> unit) : widget = { repaint = repaint; size = (fun () -> (w,h)) }

Nested Widgets Containers and Composition

The Border Widget Container translate the Gctx 0 1 2 3 0 1 2 3 w (w s width + 4) - 1 w s height (w s height + 4) - 1 let b = border w w swidth Draws a one- pixel wide border around contained widget w b s size is slightly larger than w s(+4 pixels in each dimension) b s repaint method must call w srepaint method When b asks w to repaint, b must translate the Gctx.t to (2,2) to account for the displacement of w from b s origin

The Border Widget simplewidget.ml let border (w:widget):widget = { repaint = (fun (g:gctx.gctx) -> let (width,height) = w.size () in let x = width + 3 in let y = height + 3 in Gctx.draw_line g (0,0) (x,0); Gctx.draw_line g (0,0) (0,y); Gctx.draw_line g (x,0) (x,y); Gctx.draw_line g (0,y) (x,y); let gw = Gctx.translate g (2,2) in w.repaint gw); Draw the border Display the interior size = (fun () -> let (width,height) = w.size () in (width+4, height+4)) } 26

The hpair Widget Container translate Gctx to repaint w2 w1 w2 h s height h s width let h = hpair w1 w2 Creates a horizontally adjacent pair of widgets Aligns them by their top edges Must translate the Gctx when repainting the right widget Size is the sum of their widths and max of their heights

simplewidget.ml The hpair Widget let hpair (w1: widget) (w2: widget) : widget = { repaint = (fun (g: Gctx.gctx) -> let (x1, _) = w1.size () in begin w1.repaint g; w2.repaint (Gctx.translate g (x1,0)) (* Note translation of the Gctx *) end); } size = (fun () -> let (x1, y1) = w1.size () in let (x2, y2) = w2.size () in (x1 + x2, max y1 y2)) Translate the Gctx to shift w2 s position relative to widget- local origin. 28

Widget Hierarchy Pictorially swdemo.ml (* Create some simple label widgets *) let l1 = label "Hello" let l2 = label "World" (* Compose them horizontally, adding some borders *) let h = border (hpair (border l1) (hpair (space (10,10)) (border l2))) border hpair Hello World border hpair label space border On the screen Widget tree label

Drawing: Containers Container widgets propagate repaint commands to their children: border.repaint g hpair.repaint g1 Hello World border hpair.repaint g2 label space border.repaint g3 label.repaint g4 Widget tree g1 = Gctx.translate g (2,2) g2 = Gctx.translate g1 (hello_width,0) g3 = Gctx.translate g2 (space_width,0) g4 = Gctx.translate g3 (2,2) On the screen