COS 260 DAY 23 Tony Gauvin.

Slides:



Advertisements
Similar presentations
Graphic User Interfaces Layout Managers Event Handling.
Advertisements

Java Swing Recitation – 11/(20,21)/2008 CS 180 Department of Computer Science, Purdue University.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Objects First with Java A Practical Introduction using BlueJ
Chapter Day 23. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 23 Problem set 4 Due Problem set 5 Posted (Last one)  Due Dec 8 Capstones.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
CPSC150 Week 12 Graphical User Interfaces Chapter 11.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Timer class and inner classes. Processing timer events Timer is part of javax.swing helps manage activity over time Use it to set up a timer to generate.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
OOP (Java): GUI Intro/ OOP Objectives – –use an image viewer application to introduce Java's GUI features Semester 2,
CS1054: Lecture 21 - Graphical User Interface. Graphical User Interfaces vs. Text User Interface.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
5.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
1 COS 260 DAY 14 Tony Gauvin. 2 Agenda Questions? 6 th Mini quiz graded  Oct 29 –Chapter 6 Assignment 4 will be posted later Today –First two problems.
Nested Classes CompSci 230 S Software Construction.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces 2.0.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling Objects First with Java - A Practical.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
1 COS 260 DAY 24 Tony Gauvin. 2 Agenda Questions? 10 th Mini Quiz Corrected –So-so results Capstone progress report over due Course evaluations Assignment.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 COS 260 DAY 12 Tony Gauvin. 2 Agenda Questions? 5 th Mini quiz –Chapter 5 40 min Assignment 3 Due Assignment 4 will be posted later (next week) –If.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
1 COS 260 DAY 22 Tony Gauvin. 2 Agenda Questions? 9 th Mini Quiz corrected –Good results Assignment 5 Not corrected yet Assignment 6 Posted (one more)
1 COS 260 DAY 18 Tony Gauvin. 2 Agenda Questions? 7 th Mini quiz Graded –Good results 8 th Mini Quiz –Chap 8  Next class Assignment 4 Due Assignment.
Objects First With Java A Practical Introduction Using BlueJ Building Graphical User Interfaces (GUIs) Week
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
1 DemoBasic_v3, DemoBasic_v4 JButton JLabel. 2 Registering an ActionListener Register by invoking the following from within constructor DemoBasicFrame.
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:
6.0 Objects First with Java A Practical Introduction using BlueJ David J. Barnes Michael Kölling.
Building Graphical User Interfaces Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken,
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
GUIs and Events Rick Mercer.
CompSci 230 S Software Construction
COS 260 DAY 1 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
A Quick Java Swing Tutorial
Building Graphical User Interfaces
COS 260 DAY 17 Tony Gauvin.
COS 260 DAY 9 Tony Gauvin.
Timer class and inner classes
COS 260 DAY 13 Tony Gauvin.
COS 260 DAY 19 Tony Gauvin.
COS 260 DAY 11 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
COS 260 DAY 10 Tony Gauvin.
COS 260 DAY 2 Tony Gauvin.
COS 260 DAY 18 Tony Gauvin.
COS 260 DAY 16 Tony Gauvin.
1 Graphical User Interfaces
Objects First with Java A Practical Introduction using BlueJ
A Quick Java Swing Tutorial
COS 260 DAY 11 Tony Gauvin.
Advanced Programming in Java
COS 260 DAY 23 Tony Gauvin.
COS 260 DAY 14 Tony Gauvin.
COS 260 DAY 4 Tony Gauvin.
COS 260 DAY 23 Tony Gauvin.
Objects First with Java A Practical Introduction using BlueJ
Presentation transcript:

COS 260 DAY 23 Tony Gauvin

Agenda Mini Quiz 10 Assignment 7 Due Assignment 8 Posted Questions? Assignment 7 Due Assignment 8 Posted Dee Dec 12 Mini Quiz 10 Chapter 12 Password ElmerFudd Capstone progress report 3 Due Building Graphical User Interfaces Con’t

Capstone Project Capstone Project Description Fall 2017.pdf October 2 Proposal Due E-mailed to Tony in Blackboard October 27 Progress Report E-mailed to Tony in Blackboard November 17 Progress Report E-mailed to Tony in Blackboard November 30 Progress Report E-mailed to Tony in Blackboard December 12@8AM All Deliverables & Presentation Due Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Final CountDown Dec 1 Dec 5 Dec 8 Dec 12 @ 8 AM Gui’s Continued Assignment 7 Due Miniquiz 10 Course evaluations Dec 5 Handling errors Dec 8 Min quiz 11 Dec 12 @ 8 AM Assignment 8 due Capstone Projects and presentation Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Building Graphical User Interfaces Objects First with Java Building Graphical User Interfaces Replace this with your course title and your name/contact details. 6.0 © David J. Barnes and Michael Kölling

Overview Constructing GUIs Interface components GUI layout Event handling © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Dialogs Modal dialogs block all other interaction. Forces a response from the user. Non-modal dialogs allow other interaction. This is sometimes desirable. May be difficult to avoid inconsistencies. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

JOptionPane standard dialogs Message dialog Message text plus an OK button. Confirm dialog Yes, No, Cancel options. Input dialog Message text and an input field. Variations are possible. https://docs.oracle.com/javase/8/docs/api/javax/swing/JOptionPane.html https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

A message dialog private void showAbout() { JOptionPane.showMessageDialog(frame, "ImageViewer\n" + VERSION, "About ImageViewer", JOptionPane.INFORMATION_MESSAGE); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Image filters Functions applied to the whole image. int height = getHeight(); int width = getWidth(); for(int y = 0; y < height; y++) { for(int x = 0; x < width; x++) { Color pixel = getPixel(x, y); alter the pixel's color value; setPixel(x, y, pixel); } © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Adding further filters private void makeLighter() { if(currentImage != null) { currentImage.lighter(); frame.repaint(); showStatus("Applied: lighter"); } else { showStatus("No image loaded."); Code duplication? Refactor! private void threshold() { if(currentImage != null) { currentImage.threshold(); frame.repaint(); showStatus("Applied: threshold"); } else { showStatus("No image loaded."); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Adding further filters Define a Filter superclass (abstract). Create function-specific subclasses. Create a collection of subclass instances in ImageViewer. Define a generic applyFilter method. See imageviewer2-0. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

imageviewer2-0 Ex 13.38 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Buttons and nested layouts A GridLayout inside a FlowLayout inside a BorderLayout. Ex 13.45 13.46 , 13.47 , 13.48 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Borders Used to add decoration around components. Defined in javax.swing.border BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, TitledBorder. https://docs.oracle.com/javase/tutorial/uiswing/components/border.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Adding spacing JPanel contentPane = (JPanel)frame.getContentPane(); contentPane.setBorder(new EmptyBorder(6, 6, 6, 6));   // Specify the layout manager with nice spacing contentPane.setLayout(new BorderLayout(6, 6)); imagePanel = new ImagePanel(); imagePanel.setBorder(new EtchedBorder()); contentPane.add(imagePanel, BorderLayout.CENTER); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Other components Slider https://docs.oracle.com/javase/tutorial/uiswing/components/slider.html Spinner https://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html Tabbed pane https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html Scroll pane https://docs.oracle.com/javase/tutorial/uiswing/components/scrollpane.html © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Nested class syntax Class definitions may be nested. public class Enclosing { … private class Inner { … } } This is tight coupling © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Inner classes Instances of the inner class are localized within the enclosing class. Instances of the inner class have access to the private members of the enclosing class. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Anonymous inner classes Obey the rules of inner classes. Used to create one-off objects for which a class name is not required. Use a special syntax. The instance is always referenced via its supertype, as it has no subtype name. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Anonymous action listener JMenuItem openItem = new JMenuItem("Open"); openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { openFile(); } }); Compare with the lambda version © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Lambda for ActionListener JMenuItem openItem = new JMenuItem("Open"); ... openItem.addActionListener( (ActionEvent ev) -> { openFile(); } ); General form item.addActionListener(ev -> { actions }); © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Why use inner classes? The Lambda notations only works with functional interface (one abstract message) Most listener(s) have more than one interface https://docs.oracle.com/javase/8/docs/api/java/awt/event/MouseListener.html https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseMotionListener.html Sometime we use adapters instead of listeners https://www.youtube.com/watch?v=UuKNGMCfSkQ https://docs.oracle.com/javase/8/docs/api/java/awt/event/WindowAdapter.html Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling

Anonymous class elements Anonymous object creation openItem.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) openFile(); } ); Class definition Actual parameter © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Exit on window close frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); WindowAdapter provides a no-op implementation of the WindowListener interface. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Review Aim for cohesive application structures. Endeavor to keep GUI elements separate from application functionality. Pre-defined components simplify creation of sophisticated GUIs. Layout managers handle component juxtaposition. Nest containers for further control. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.

Review Many components recognize user interactions with them. Reactive components deliver events to listeners. Lambdas are commonly used to implement listeners. Anonymous inner classes are used where lambdas are not possible. © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved.