CSE 331 Software Design & Implementation

Similar documents
Lecture 18 Java Graphics and GUIs

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

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation

CSE 143. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event- driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

CSE 331. Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT

Introduction to the JAVA UI classes Advanced HCI IAT351

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!

Swing. By Iqtidar Ali

CS 251 Intermediate Programming GUIs: Components and Layout

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

Chapter 12 GUI Basics

Introduction p. 1 JFC Architecture p. 5 Introduction to JFC p. 7 The JFC 1.2 Extension p. 8 Swing p. 9 Drag and Drop p. 16 Accessibility p.

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

Part 3: Graphical User Interface (GUI) & Java Applets

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Introduction to Graphical Interface Programming in Java. Introduction to AWT and Swing

Graphical User Interfaces. Comp 152

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Swing UI. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

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

2110: GUIS: Graphical User Interfaces

Tool Kits, Swing. Overview. SMD158 Interactive Systems Spring Tool Kits in the Abstract. An overview of Swing/AWT

Window Interfaces Using Swing Objects

Handout 14 Graphical User Interface (GUI) with Swing, Event Handling

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

Java IDE Programming-I

GUI Programming. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies

Java Programming Lecture 6

CSE 331 Software Design & Implementation

Graphical User Interface (GUI) components in Java Applets. With Abstract Window Toolkit (AWT) we can build an applet that has the basic GUI

Window Interfaces Using Swing Objects

Graphical User Interface (GUI)

Programming graphics

Graphical User Interface (GUI)

Graphical User Interfaces. Swing. Jose Jesus García Rueda

Sri Vidya College of Engineering & Technology

Summary Chapter 25 GUI Components: Part 2

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

Lecture 19 GUI Events

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

SELF-STUDY. Glossary

Java: Graphical User Interfaces (GUI)

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

Java Swing. Lists Trees Tables Styled Text Components Progress Indicators Component Organizers

Chapter 12 Advanced GUIs and Graphics

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

Java Swing. based on slides by: Walter Milner. Java Swing Walter Milner 2005: Slide 1

Computer Science 210: Data Structures. Intro to Java Graphics

Supporting Materials

Graphical User Interfaces (GUIs)

Packages: Putting Classes Together

Bringing Life to Swing Desktop Applications

Event Driven Programming

BASICS OF GRAPHICAL APPS

CONTENTS. Chapter 1 Getting Started with Java SE 6 1. Chapter 2 Exploring Variables, Data Types, Operators and Arrays 13

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

PIC 20A GUI with swing

The Abstract Windowing Toolkit. Java Foundation Classes. Swing. In April 1997, JavaSoft announced the Java Foundation Classes (JFC).

UI Software Organization

Graphical User Interfaces

Assignment 2. Application Development

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Laying Out Components. What is Widget Layout?

This page intentionally left blank

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

Unit 6: Graphical User Interface

JTcl and Swank. Bruce A. Johnson, Tom Poindexter, & Dan Bodoh. What s new with Tcl and Tk on the JVM. Wednesday, October 26, 11

Graphic Interface Programming II Events and Threads. Uppsala universitet

CSE 331 Software Design and Implementation. Lecture 19 GUI Events

User interfaces and Swing

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Chapter 13 GUI Basics. Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved.

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

Chapter 6: Graphical User Interfaces

Basicsof. JavaGUI and SWING

INTRODUCTION TO (GUIS)

Is image everything?

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

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

2IS45 Programming

Chapter 12 GUI Basics. Motivations. The design of the API for Java GUI programming

Graphical User Interface (GUI)

Part I: Learn Common Graphics Components

Here is a list of a few of the components located in the AWT and Swing packages:

Systems Programming Graphical User Interfaces

Lecture 9. Lecture

Graphic Interface Programming II Using Swing and OOP more advanced issues. Uppsala universitet

Graphical interfaces & event-driven programming

Jonathan Aldrich Charlie Garrod

Lecture 5: Java Graphics

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

WIMP Elements. GUI goo. What is WIMP?

1.00 Lecture 14. Lecture Preview

Today. cisc3120-fall2012-parsons-lectiii.3 2

IT101. Graphical User Interface

Transcription:

CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Java Graphics and GUIs 1

The plan Today: introduction to Java graphics and Swing/AWT libraries Then: event-driven programming and user interaction None of this is comprehensive only an overview and guide to what you should expect to be out there Some standard terminology and perspective Main concepts carry over to most any graphics system you ll be able to pick up new toolkits when you need them Credits: material taken from many places; including slides and materials by Ernst, Hotan, Mercer, Notkin, Perkins, Stepp; Reges; Sun/Oracle docs & tutorial; Horstmann; Wikipedia; others, folklore, 2

References Very useful start: Sun/Oracle Java tutorials http://docs.oracle.com/javase/tutorial/uiswing/index.html Mike Hoton s slides/sample code from CSE 331 Sp12 (lectures 23, 24 with more extensive widget examples) http://courses.cs.washington.edu/courses/cse331/12sp/lectures/lect23-gui.pdf http://courses.cs.washington.edu/courses/cse331/12sp/lectures/lect24-graphics.pdf http://courses.cs.washington.edu/courses/cse331/12sp/lectures/lect23-gui-code.zip http://courses.cs.washington.edu/courses/cse331/12sp/lectures/lect24-graphics-code.zip Good book that covers this (and much more): Core Java vol. I by Horstmann & Cornell There are other decent Java books out there too (and probably even more that aren t so great ) 3

Why study GUIs? Er, because graphical user interfaces are pretty common (duh J) And it s fun! Classic example of using inheritance to organize large class libraries The best (?) example of OOP s strengths Work with a huge API and learn how (not) to deal with all of it Many core design patterns show up: callbacks, listeners, eventdriven programs, decorators, façade 4

What not to do Don t try to learn the whole library: There s way too much Don t memorize look things up as you need them Don t miss the main ideas, fundamental concepts Don t get bogged down implementing eye candy 5

Main topics to learn Organization of the AWT/Swing library Names of essential widgets/components Graphics and drawing Repaint callbacks, layout managers, etc. Handling user events Building GUI applications MVC, user events, updates, 6

A very short history (1) Java s standard libraries have supported GUIs from the beginning Original Java GUI: AWT (Abstract Window Toolkit) Limited set of user interface elements (widgets) Mapped Java UI to host system UI widgets Lowest common denominator Slogan was Write once, run anywhere ; reality turned out to be Write once, debug everywhere 7

A very short history (2) Swing: Newer GUI library, introduced with Java 2 (1998) Basic idea: underlying system provides only a blank window Swing draws all UI components directly Doesn t use underlying system widgets Not a total replacement for AWT: Swing is implemented on top of core AWT classes and both still coexist Use Swing, but deal with AWT when you must 8

GUI terminology window: A first-class citizen of the graphical desktop Also called a top-level container Examples: frame, dialog box, applet component: A GUI widget that resides in a window Called controls in many other languages Examples: button, text box, label container: A component that hosts (holds) components Examples: frame, applet, panel, box 9

Some components 10

Component and container classes Every GUI-related class descends from Component, which contains dozens of basic methods and fields Examples: getbounds, isvisible, setforeground, Component Container Lots of AWT components Atomic components: labels, text fields, buttons, check boxes, icons, menu items Many components are containers things like panels (JPanel) that can hold nested subcomponents JComponent JPanel JFileChooser Various AWT containers Tons of JComponents 11

Swing/AWT inheritance hierarchy Component (AWT) Window Frame JFrame JDialog (Swing) Container JComponent (Swing) JButton JColorChooser JFileChooser JComboBox JLabel JList JMenuBar JOptionPane JPanel JPopupMenu JProgressBar JScrollbar JScrollPane JSlider JSpinner JSplitPane JTabbedPane JTable JToolbar JTree JTextArea JTextField... 12

Component properties Zillions. Each has a get (or is) accessor and a set modifier. Examples: getcolor,setfont,isvisible, name type description background Color background color behind component border Border border line around component enabled boolean whether it can be interacted with focusable boolean whether key text can be typed on it font Font font used for text in component foreground Color foreground color of component height, width int component's current size in pixels visible boolean whether component can be seen tooltip text String text shown when hovering mouse Dimension various sizes, size limits, or desired sizes that the component may take size, minimum / maximum / preferred size 13

Types of containers Top-level containers: JFrame, JDialog, Often correspond to OS windows Usually a host for other components Live at top of UI hierarchy, not nested in anything else Mid-level containers: panels, scroll panes, tool bars Sometimes contain other containers, sometimes not JPanel is a general-purpose component for drawing or hosting other UI elements (buttons, etc.) Specialized containers: menus, list boxes, Technically, all JComponents are containers 14

JFrame top-level window Graphical window on the screen Typically holds (hosts) other components Common methods: JFrame(String title): constructor, title optional setdefaultcloseoperation(int what) What to do on window close JFrame.EXIT_ON_CLOSE terminates application setsize(int width, int height): set size add(component c): add component to window setvisible(boolean b): make window visible or not 15

Example SimpleFrameMain.java 16

JPanel a general-purpose container Commonly used as a place for graphics, or to hold a collection of button, labels, etc. Needs to be added to a window or other container: frame.add(new JPanel( )) JPanels can be nested to any depth Many methods/fields in common with JFrame (since both inherit from Component) Advice: can t find a method/field? Check the superclasses A particularly useful method: setpreferredsize(dimension d) 17

Containers and layout What if we add several components to a container? How are they positioned relative to each other? Answer: each container has a layout manger 18

Layout managers Kinds: FlowLayout (left to right [changeable], top to bottom) Default for JPanel Each row centered horizontally [changeable] BorderLayout ( center, north, south, east, west ) Default for JFrame No more than one component in each of 5 regions (Of course, component can itself be a container) GridLayout (regular 2-D grid) Others... (some are incredibly complex) FlowLayout and BorderLayout should be good enough for now 19

pack() Once all the components are added to their containers, do this to make the window visible: pack(); setvisible(true); pack() figures out the sizes of all components and calls the container s layout manager to set locations in the container (recursively as needed) If your window doesn t look right, you may have forgotten pack() 20

Example SimpleLayoutMain.java 21

Graphics and drawing So far so good and very boring 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 for themselves that (in JLabel s case) draws the label text Other components like JPanel typically inherit an empty paintcomponent that we can override to draw things Note: As we ll see, we override paintcomponent but we don t call it 22

Example SimplePaintMain.java 23

Graphics methods Many methods to draw various lines, shapes, etc., Can also draw images (pictures, etc.): In the program (not in paintcomponent): BufferedImage pic;... try { pic = ImageIO.read( new File("file-name (with path)"); } catch (IOException e) { } do something appropriate Then in paintcomponent: g.drawimage(pic,...); 24

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 25

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 any time 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 26

Example FaceMain.java 27

How repainting happens program repaint() window manager (UI) 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! 28

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 29

What s next and not Major topic for next lecture is how to handle user interactions We already know the core idea: it s a big-time use of the observer pattern Beyond that 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) 30