CSE 331 Software Design & Implementation

Similar documents
Lecture 18 Java Graphics and GUIs

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

Lecture 19 GUI Events

Announcements. Introduction. Lecture 18 Java Graphics and GUIs. Announcements. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

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

IT101. Graphical User Interface

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

Java Programming. Computer Science 112

Graphical User Interface (GUI)

CSC207 Week 4. Larry Zhang

UI Software Organization

Graphics programming. COM6516 Object Oriented Programming and Design Adam Funk (originally Kirill Bogdanov & Mark Stevenson)

Software Construction

Widgets. Widgets Widget Toolkits. User Interface Widget

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

G51PRG: Introduction to Programming Second semester Applets and graphics

Assignment 2. Application Development

Lecture 24 Wrap Up. Final Logistics CSE 331. Today. CSE 331 Software Design and Implementation. Wednesday, 8:30-10:20 AM

CSE 331 Software Design and Implementation. Lecture 24 Wrap Up

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Graphical User Interface (GUI)

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

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!

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

Graphical User Interfaces (GUIs)

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

Programming Languages and Techniques (CIS120)

Windows and Events. created originally by Brian Bailey

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Smoother Graphics Taking Control of Painting the Screen

CSE wi Final Exam 3/12/18 Sample Solution

Class 16: The Swing Event Model

Java Programming Lecture 6

Lecture 5: Java Graphics

Object-oriented programming in Java (2)

Graphic Interface Programming II Events and Threads. Uppsala universitet

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

Lecture 9. Lecture

Widget Toolkits CS MVC

Introduction to Computer Science I

(Incomplete) History of GUIs

CSE wi Final Exam 3/12/18. Name UW ID#

GUI Design for Android Applications

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Asynchronous Programming

Fall UI Design and Implementation 1

Widgets. Widgets Widget Toolkits. 2.3 Widgets 1

Object Orientated Programming in Java. Benjamin Kenwright

KF5008 Program Design & Development. Lecture 1 Usability GUI Design and Implementation

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

Strategy Pattern. What is it?

Distributed Collaboration - Assignment 1: Multi-View 1-User IM

Using the API: Introductory Graphics Java Programming 1 Lesson 8

Advanced Effects in Java Desktop Applications

CMSC434. Introduction to Human-Computer Interaction. Week 10 Lecture 17 Mar 31, 2016 Engineering Interfaces III. Jon

Bringing Life to Swing Desktop Applications

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

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

2IS45 Programming

Programming Languages and Techniques (CIS120)

Android Application Development Instructions

CS 251 Intermediate Programming GUIs: Components and Layout

Programming Languages and Techniques (CIS120)

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

Lecture 3: Java Graphics & Events

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

CSE 331 Software Design & Implementation

Jonathan Aldrich Charlie Garrod

Rendering a 3-Dimensional Cube Applet Using Light Weight Java Graphing Library (LWJGL) with Java Swing with NetBeans IDE 6.1

Introduction to the JAVA UI classes Advanced HCI IAT351

Programming graphics

Agenda. Programming Seminar. By: dr. Amal Khalifa. Coordinate systems Colors Fonts Drawing shapes Graphics2D API

CSE 331 Software Design and Implementation. Lecture 17 Events, Listeners, Callbacks

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

CS2110 Fall 2015 Assignment A5: Treemaps 1

Java Swing Introduction

Computer Science 210: Data Structures. Intro to Java Graphics

CS 4300 Computer Graphics

Core Java Syllabus. Pre-requisite / Target Audience: C language skills (Good to Have)

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

GUI Output. Adapted from slides by Michelle Strout with some slides from Rick Mercer. CSc 210

Quick Tips to Using I-DEAS. Learn about:

Page 1. Human-computer interaction. Lecture 2: Design & Implementation. Building user interfaces. Users and limitations

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

HOUR 4 Understanding Events

It s possible to get your inbox to zero and keep it there, even if you get hundreds of s a day.

CSE 331 Software Design & Implementation

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

CS11 Java. Fall Lecture 4

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

Design patterns for graphical user interface applications

Custom Views in Android Tutorial

PIC 20A GUI with swing

Lab 1 - Setting up the User s Profile UI

Transcription:

CSE 331 Software Design & Implementation Kevin Zatloukal Summer 2017 Java Graphics and GUIs (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal Perkins, Zach Tatlock)

Review: how to create a GUI 1. Create a JFrame (window) 2. Add components to it organize them on the screen using a layout manager 3. Add handlers on the components one for each event you want to respond to 2

JPanel a general-purpose container In addition to all the uses we saw in lecture: Commonly used as a place for graphics A particularly useful method: setpreferredsize(dimension d) you may want to call this when using JPanel as a canvas (don t usually want to otherwise) 3

Android Main Activity Layout Similar to JPanel as general-purpose container, Main Activity xml file functions as container for components and application layout Add components from list of all possible components via drag-anddrop mechanics in a graphical user interface 4

Graphics and drawing What if we want to actually draw something? A map, an image, a path,? Answer: Override method paintcomponent Components like JLabel provide a suitable paintcomponent that (in JLabel s case) draws the label text Other components like JPanel typically inherit an empty paintcomponent and can override it to draw things Note: As we ll see, we override paintcomponent but we don t call it 5

Example SimplePaintMain.java 6

Graphics methods Many methods to draw various lines, shapes, etc., Can also draw images (pictures, etc.): In the program (not in paintcomponent): Use AWT s Toolkit to load an image: Image pic = Toolkit.getDefaultToolkit().getImage(file-name (with path)); Then in paintcomponent: g.drawimage(pic, ); 7

Graphics vs Graphics2D Class Graphics was part of the original Java AWT Has a procedural interface: g.drawrect( ), g.filloval( ), Swing introduced Graphics2D (extends Graphics ) Added an object interface create instances of Shape like Line2D, Rectangle2D, etc., and add these to the Graphics2D object Actual parameter to paintcomponent is always a Graphics2D Can always cast this parameter from Graphics to Graphics2D Graphics2D supports both sets of graphics methods Use whichever you like for CSE 331 8

So who calls paintcomponent? And when?? Answer: the window manager calls paintcomponent whenever it wants!!! (a callback!) When the window is first made visible, and whenever after that some or all of it needs to be repainted Corollary: paintcomponent must always be ready to repaint regardless of what else is going on You have no control over when or how often You must store enough information to repaint on demand If you want to redraw a window, call repaint() from the program (not from paintcomponent) Tells the window manager to schedule repainting Window manager will call paintcomponent when it decides to redraw (soon, but maybe not right away) Window manager may combine several quick repaint() requests and call paintcomponent() only once 9

Android Graphics and drawing Extend AppCompatImageView class and override ondraw method Like paintcomponent in Swing, we don t call ondraw in Android Instead, use invalidate() to request the app to be redrawn Canvas parameter in ondraw like Graphics parameter from paintcomponent in Swing 10

Example FaceMain.java 11

How repainting happens program window manager (UI) repaint() It s worse than it looks! Your program and the window manager are running concurrently: paintcomponent(g) Asynchronous Callback Program thread User Interface thread Do not attempt to mess around follow the rules and nobody gets hurt! 12

Crucial rules for painting Always override paintcomponent(g) if you want to draw on a component Always call super.paintcomponent(g) first NEVER, EVER, EVER call paintcomponent yourself Always paint the entire picture, from scratch Use paintcomponent s Graphics parameter to do all the drawing. ONLY use it for that. Don t copy it, try to replace it, or mess with it. It is quick to anger. DON T create new Graphics or Graphics2D objects Fine print: Once you are a certified wizard, you may find reasons to do things differently, but that requires deeper understanding of the GUI library s structure and specification 13

What s next and not You re on your own to explore all the wonderful widgets in Swing/AWT. Have fun!! (But don t sink huge amounts of time into eye candy) 14

Reminder: UI thread Recall that sometimes the program has additional threads, e.g.: one thread is waiting for network data ( the network thread ) another thread is displaying the UI ( the UI thread ) All UI actions happen in the UI thread including callbacks like actionlistener or paintcomponent, etc. defined in your code After event handling and related work, call repaint() if paintcomponent() needs to run. Don t try to draw anything from inside the event handler itself (as in you must not do this!!!) Remember that paintcomponent must be able to do its job whenever the window manager calls it so any data it needs to render must be prepared in advance 15

Event handling and repainting program window manager (UI) actionperformed(e) repaint() paintcomponent(g) Remember: your program and the window manager are running concurrently: Program thread User Interface thread It s ok to call repaint from an event handler, but never call paintcomponent yourself from either thread. 16

Synchronization issues? Yes, there can be synchronization problems (cf. CSE332, CSE451, CSE452, ) Not generally an issue in well-behaved programs, but can happen Advice: Keep event handling short Call repaint when data is ready, not when only partially updated Don t update data in the UI and program threads at the same time (particularly for complex data) Never call paintcomponent directly (Have we mentioned you should never ever call paintcomponent? And don t create a new Graphics object either.) If you are building industrial-strength UIs, learn more about threads and Swing and how to avoid potential problems (Swing tutorial, ) 17

Larger example bouncing balls A hand-crafted MVC application. Origin is somewhere back in the CSE142/3 mists. Illustrates how some swing GUI components can be put to use. Disclaimers: Not the very best design (maybe not even particularly good) Unlikely to be directly appropriate for your project Use it for ideas and inspiration, and feel free to steal small bits if they really fit Enjoy! 18