CSE 331 Software Design and Implementation

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

CMSC 341 Building Java GUIs. 09/26/2007 CMSC 341 GUI 2 Why Java GUI Development? Course is about Data Structures, not GUIs. We are giving you the opportunity.
Graphical User Interfaces CS 2110 Spring Ivan Sutherland: “Sketchpad”,
Corresponds with Chapter 12
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 Java Graphics & GUIs 1.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Event Driven Programming and GUIs Part 3 CS221 – 4/15/09.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Graphical User Interfaces (GUIs) GUI: An application that uses graphical objects to interact with users GUI applications consist of: –Events: A user or.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Advanced Java Class GUI – part 1. Intro to GUI GUI = Graphical User Interface -- “Gooey” Just because it’s “gooey” does not mean you may write messy code.
1 GUI Elements in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CMSC 132: Object-Oriented Programming II Nelson Padua-Perez William Pugh Department of Computer Science University of Maryland, College Park.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 Java Graphics and GUIs (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal.
GUI Basics: Introduction. Creating GUI Objects // Create a button with text OK JButton jbtOK = new JButton("OK"); // Create a label with text "Enter your.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing.
CSE 219 Computer Science III Graphical User Interface.
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
(c) University of Washington07-1 CSC 143 Introduction to Graphical Interfaces in Java: AWT and Swing Reading: Ch. 17.
Java Programming: Advanced Topics 1 Common Elements of Graphical User Interfaces Chapter 6.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSE 331 Event-driven Programming and Graphical User Interfaces (GUIs) with Swing/AWT slides created by Marty Stepp based on materials by M. Ernst, S.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
Java Programming: Advanced Topics 1 Components and Facilities for Rich Graphical User Interfaces Chapter 7.
Swing Differences between Swing and AWT Naming Conventions All Swing components begin with a capital J -- JPanel, JButton, JScrollBar, JApplet, etc..
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
GUI Basics. What is GUI? A graphical user interface (GUI) is a type of user interface item that allows people to interact with programs in more ways than.
University of Limerick1 Software Architecture Java Layout Managers.
Lec.10 (Chapter 8 & 9) GUI Jiang (Jen) ZHENG June 27 th, 2005.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Swing Components. Introduction Swing – A set of GUI classes – Part of the Java's standard library –Much better than the previous library: AWT Abstract.
Introduction to Java Chapter 9 - Graphical User Interfaces and Applets1 Chapter 9 Graphical User Interfaces and Applets.
5-Jan-16 R Ramesh Swing. 5-Jan-16 R Ramesh An Introduction to Java Foundation Classes (JFC) A suite of libraries to assist programmers create enterprise.
Basics of GUI Programming Chapter 11 and Chapter 22.
Creating a Window. A basic window in Java is represented by an object of the class Window in the package java.awt.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Introduction to GUI Programming in Java: Frames, Simple Components, and Layouts.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
1 A Quick Java Swing Tutorial. 2 Introduction Swing – A set of GUI classes –Part of the Java's standard library –Much better than the previous library:
Introduction to Swing Mr. Crone. What is Swing? a collection of pre-made Java classes used to create a modern graphical user interface.
Module 13: Swing API Object Oriented Programming(Java)
A Quick Java Swing Tutorial
Chapter 9: Graphical User Interfaces
A First Look at GUI Applications
Advanced GUI Programming
CSE 331 Software Design and Implementation
Java Swing.
Lecture 27 Creating Custom GUIs
A Quick Java Swing Tutorial
CSE 331 Software Design and Implementation
Ellen Walker Hiram College
Chapter 13: Advanced GUIs and Graphics
CSE 331 Software Design and Implementation
GUIS: Graphical User Interfaces
CSE 331 Software Design and Implementation
Steps to Creating a GUI Interface
A Quick Java Swing Tutorial
CSE 331 Software Design & Implementation
Introduction to Graphical Interfaces in Java: AWT and Swing
Advanced GUIs and Graphics
Graphical User Interface
Presentation transcript:

CSE 331 Software Design and Implementation Lecture 18 Java Graphics and GUIs Leah Perlmutter / Summer 2018

Announcements

Announcements Quiz 6 due Thursday 8/2 Homework 7 due Thursday 8/2 Regression testing for HW7  MAKE SURE HW5 and HW6 TESTS PASS!!!!! staff tests AND your own tests Regression testing helps ensure that new changes (HW7) do not (re)introduce bugs in previous work (HW5/6) You will be graded on HW5/6 tests passing in HW7 includes staff tests and your own implementation/spec tests

Introduction

The plan Today: introduction to Java graphics and Swing/AWT libraries Next Lecture: 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 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, …

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 Book that covers this (and much more): Core Java vol. I by Horstmann & Cornell There are other decent Java books out there too

Why study GUIs? Graphical user interfaces are pretty common (duh ) And it’s fun! Example of using inheritance to organize large class libraries Work with a large API – and learn how (not) to deal with all of it Many core design patterns show up: callbacks, listeners, event-driven programs, decorators, façade Swing seems to have a large API, but you’re likely to work with much larger APIs in the future Android, for example

Dos and Don’ts Don’t try to learn the whole library: There’s way too much Don’t memorize – look things up as you need them Do be aware of rabbit holes. rabbit hole: A time-consuming tangent or detour, often from which it is difficult to extricate oneself. (Wiktionary) Do explore and be creative! Do have fun!

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, …

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 “Write once, debug everywhere”

A very short history (2) less old 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 Why is it called swing? https://blogs.oracle.com/thejavatutorials/why-is-swing-called-swing

Java Swing

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 What is an applet? a small apple? a small app? the predecessor to an app? ooh I know! before flash, web games used to run in a java applet. The serious answer - An applet is a small program that is intended not to be run on its own, but rather to be embedded inside another application. (oracle docs for applet)

Some components… - drop down menus are called “combo box”

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

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

size, minimum / maximum / preferred size 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 whether key text can be typed on it font Font font used for text in component foreground foreground color of component height, width int component's current size in pixels visible whether component can be seen tooltip text String text shown when hovering mouse size, minimum / maximum / preferred size Dimension various sizes, size limits, or desired sizes that the component may take

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

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

Example SimpleFrameMain.java

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)

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

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…

pack() Once all the components are added to their containers, do this to make the window visible, e.g. frame.pack(); frame.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()

Example SimpleLayoutMain.java

Graphics and Drawing in Swing

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 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

Graphics and drawing How does custom stuff get drawn to the screen? Window Manager magic ✨ ✨ ✨ ... add paint event to the event queue paintComponent() more magic ✨ ✨ ✨ ✨ ✨ ✨ Your Code @Override protected void paintComponent(Graphics g) {...} frame.setVisible(true); you define a callback! your callback gets called! ✨ ✨ ✨ AWT and Swing magic takes over ✨ ✨ ✨ - the tiny code on this slide is just filler. I made it too small to read on purpose. Runs in parallel with your other code

Example SimplePaintMain.java Magic: our code didn’t call paintComponent directly! We can think of a graphics object as a friendly robot with a box of crayons setColor(Color.green) says ”pick up the green crayon” fillOval(...) says “use the crayon you’re holding to draw an oval with the given dimensions”

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, …); get the image once, draw it any number of times don’t want to read in the image every time your container gets painted

Graphics vs Graphics2D Class Graphics was part of the original Java AWT Has a procedural interface: g.drawRect(…), g.fillOval(…), … Swing introduced Graphics2D 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

Graphics and drawing How does custom stuff get drawn to the screen? Window Manager This part is required before paintComponent will work as expected. magic ✨ ✨ ✨ ... add paint event to the event queue paintComponent() more magic ✨ ✨ ✨ ✨ ✨ ✨ Your Code @Override protected void paintComponent(Graphics g) {...} frame.setVisible(true); you define a callback! your callback gets called! ✨ ✨ ✨ AWT and Swing magic takes over ✨ ✨ ✨ - the tiny code on this slide is just filler. I made it too small to read on purpose. Runs in parallel with your other code Caution: Don’t call paintComponent yourself!

Graphics and drawing Window Manager Component void repaint() { magic ✨ ✨ ✨ ... add paint event to the event queue paintComponent() more magic ✨ ✨ ✨ ✨ ✨ ✨ void repaint() { ... add paint event to the event queue } milliseconds later... paintComponent(); This part is required before paintComponent will work as expected. your callback gets called! Caution: Don’t call paintComponent yourself! Instead, call repaint! Runs in parallel with your other code

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() Tells the window manager to schedule repainting Window manager will call paintComponent when it decides to redraw (soon, but maybe not right away) DO NOT call paintComponent directly!

Example Face.java FaceMain.java

How repainting happens program window manager (UI) repaint() It’s worse than it looks! Your program and the window manager are running concurrently: Program thread User Interface thread Do not attempt to mess around – follow the rules and nobody gets hurt! paintComponent(g) This is a multithreaded UML Sequence Diagram each vertical line represents one Each color represents one thread time scale: 10s of milliseconds Asynchronous Callback

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

What’s next 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 beware of time sinks)

Announcements

Announcements Quiz 6 due Thursday 8/2 Homework 7 due Thursday 8/2 Regression testing for HW7  MAKE SURE HW5 and HW6 TESTS PASS!!!!! staff tests AND your own tests Regression testing helps ensure that new changes (HW7) do not (re)introduce bugs in previous work (HW5/6) You will be graded on HW5/6 tests passing in HW7 includes staff tests and your own implementation/spec tests