Lecture 9. Lecture

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

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

Introduction to the JAVA UI classes Advanced HCI IAT351

Graphical User Interfaces (GUIs)

H212 Introduction to Software Systems Honors

Graphical User Interfaces. Comp 152

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

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

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

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!

TTTK Program Design and Problem Solving Tutorial 3 (GUI & Event Handlings)

Graphical User Interface (GUI)

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

Lecture 5: Java Graphics

Control Flow: Overview CSE3461. An Example of Sequential Control. Control Flow: Revisited. Control Flow Paradigms: Reacting to the User

2IS45 Programming

CS 251 Intermediate Programming GUIs: Components and Layout

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

11/6/15. Objec&ves. RouleQe. Assign 8: Understanding Code. Assign 8: Bug. Assignment 8 Ques&ons? PROGRAMMING PARADIGMS

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

Packages: Putting Classes Together

Graphical User Interface (GUI)

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

Lecture 3: Java Graphics & Events

Global Gomoku Lab 4 in D0010E

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

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

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

Programmierpraktikum

Command-Line Applications. GUI Libraries GUI-related classes are defined primarily in the java.awt and the javax.swing packages.

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

CSEN401 Computer Programming Lab. Topics: Graphical User Interface Window Interfaces using Swing

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

CS 2113 Software Engineering

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

Lecture 5. Lecture

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

BASICS OF GRAPHICAL APPS

Java Programming Lecture 6

Swing from A to Z Some Simple Components. Preface

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Java: Graphical User Interfaces (GUI)

Window Interfaces Using Swing Objects

User interfaces and Swing

Hanley s Survival Guide for Visual Applications with NetBeans 2.0 Last Updated: 5/20/2015 TABLE OF CONTENTS

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

CS11 Java. Fall Lecture 4

TYPES OF INTERACTORS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill

Programming Languages and Techniques (CIS120)

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

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

News and info. Array. Feedback. Lab 4 is due this week. It should be easy to change the size of the game grid.

Window Interfaces Using Swing Objects

Object-oriented programming in Java (2)

PIC 20A GUI with swing

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

Java IDE Programming-I

GUI Event Handlers (Part I)

(Incomplete) History of GUIs

Java. GUI building with the AWT

To gain experience using GUI components and listeners.

Systems Programming Graphical User Interfaces

Graphical User Interface (GUI)

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

Introduction This assignment will ask that you write a simple graphical user interface (GUI).

Graphical User Interface (GUI)

Computer Science 210: Data Structures. Intro to Java Graphics

News! Feedback after Lecture 4! About Java and OOP!

Graphical User Interfaces 2

CS 134 Programming Exercise 7:

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

Chapter 12 Advanced GUIs and Graphics

Class 14: Introduction to the Swing Toolkit

Lecture 18 Java Graphics and GUIs

Parts of a Contract. Contract Example. Interface as a Contract. Wednesday, January 30, 13. Postcondition. Preconditions.

We are on the GUI fast track path

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

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

5. In JAVA, is exception handling implicit or explicit or both. Explain with the help of example java programs. [16]

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

Introduction. Introduction

Previously, we have seen GUI components, their relationships, containers, layout managers. Now we will see how to paint graphics on GUI components

G51PRG: Introduction to Programming Second semester Applets and graphics

Graphical User Interfaces

encompass a group of features for building Graphical User Interfaces (GUI).

Week Chapter Assignment SD Technology Standards. 1,2, Review Knowledge Check JP3.1. Program 5.1. Program 5.1. Program 5.2. Program 5.2. Program 5.

Prototyping a Swing Interface with the Netbeans IDE GUI Editor

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Chapter 8. Java continued. CS Hugh Anderson s notes. Page number: 264 ALERT. MCQ test next week. This time. This place.

Java continued. Chapter 8 ALERT ALERT. Last week. MCQ test next week. This time. This place. Closed book. Assignment #2 is for groups of 3

Jonathan Aldrich Charlie Garrod

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

1.00 Lecture 14. Lecture Preview

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

GUI in Java TalentHome Solutions

Dr. Hikmat A. M. AbdelJaber

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

Transcription:

Layout Components MVC Design PaCern GUI Programming Observer Design PaCern D0010E Lecture 8 - Håkan Jonsson 1 Lecture 8 - Håkan Jonsson 2 Lecture 8 - Håkan Jonsson 3 1

1. GUI programming In the beginning, there was Abstract Windowing Toolkit (AWT). Swing was introduced in Java 1.2. Replaces (or rather extends) AWT. Consists of a wide range of different components you can use to build your GUI. Important: Learn the principles, rather than every single class. The look and feel of the GUI can be adjusted in many ways. - Håkan Jonsson 4 Top- level containers An applicamon with a GUI has at least one top- level container. There are three kinds of such containers: JFrame JDialog, and JApplet. Each has a content pane which contains GUI components. Together, they form a containment hierarchy. - Håkan Jonsson 5 Example: The empty window (From Oracles website) (On my MacBook Pro) - Håkan Jonsson 6 2

The empty window package l09.gui.simple;!! import javax.swing.jframe;!! public class EmptyWindow {!! public static void main(string[] args) {! JFrame frame = new JFrame("GUItest");!! frame.setdefaultcloseoperation(jframe.exit_on_close);! frame.setlocation(200,200);! frame.setsize(300,150);! frame.setvisible(true);! }!! }! - Håkan Jonsson 7 The empty window The empty window looks as a filled rectangular area but is (really) empty. It shows nothing. A JFrame gives us this empty area to fill but also the window border and the menu bar on top bounding the frame. Note how the name ( GUItest ) was an argument to the JFrame constructor. To create graphics we fill the empty area with GUI components. It is such components that became visible on the screen at the end. (It is not possible to draw graphics directly on the empty area.) To ease the programming of GUI:s, Java offers a variety of ready- made components. - Håkan Jonsson 8 Lecture 8 - Håkan Jonsson 9 3

2. JPanel A JPanel is a GUI component that acts as a rectangular canvas on which graphics can be drawn. Like in Lab Assignment 2, where LevelGUI drew rooms on a JPanel (in fact, a dynamic object of type Display). However, JPanel:s also funcmons as containers that can hold other GUI components. To group components and simplify layout. - Håkan Jonsson 10 Example: JFrameExample Comments: Note that we create a dynamic object of the same class (JFrameExample) as the (stamc) main method resides in. This works fine since main a) is in the stamc object, b) is called when the program starts, and c) then creates the object like it would create any other object. We add a content pane (a JPanel object) to the empty area of the JFrame. We insert, into this pane, a text label (JLabel) and a bucon (JBuGon). It is possible to press the bucon by clicking on it, and the depressing is animated, but the bucon has no effect on the program. Lecture 8 - Håkan Jonsson 11 Example: JFrameExample Comments: Note that we create a dynamic object of the same class (JFrameExample) as the (stamc) main method resides in. This works fine since main a) is in the stamc object, b) is called when the program starts, and c) then creates the object like it would create any other object. We add a content pane (a JPanel object) to the empty area of the JFrame. We insert, into this pane, a text label (JLabel) and a bucon (JBuGon). It is possible to press the bucon by clicking on it, and the depressing is animated, but the bucon has no effect on the program. Lecture 8 - Håkan Jonsson 12 4

Lecture 8 - Håkan Jonsson 13 3. Extending a component Standard pracmce is to extend GUI components into tailor- made component classes. Example: A Display is a JPanel, but one with color c, preferred size (dx, dy), and a black border. private class Display extends JPanel {!!public Display(int dx, int dy, Color c) {!!!setbackground(c);!!!setpreferredsize(new Dimension(dx, dy));!!!setborder(borderfactory.createlineborder(color.black));!!}! }! - Håkan Jonsson 14 Example: ColoredJPanels (Similar to Lab Assignment 2.) Lecture 8 - Håkan Jonsson 15 5

Example: LampExample Lecture 8 - Håkan Jonsson 16 Lecture 8 - Håkan Jonsson 17 4. InteracMon A GUI contains graphics but also supports user interacion. In Java, interacmon is event- driven and implemented by use of listeners. These react on events like keys depressed on a keyboard and mouse movements/clicks. AcIonListener, KeyListener, MouseListener etc. To enable interacmon, we will use two design pacerns that decouples code and make programs easy to understand. Observer design pacern. Model- View- Control design pacern. Lecture 8 - Håkan Jonsson 18 6

Recap: Observer The Observer design pagern defines a relamon between two classes. The observable: A class that extends the class Observable. The class inherits the methods setchanged, noifyobservers, and addobserver. A dynamic object of the class must call setchanged and noifyobservers whenever it has changed. The method addobserver is used to add an observer to the object. Any number can be added. The observer: A class that implements the interface Observer. Must declare a method update. This is what the interface lists. A dynamic object of the class gets a call to update whenever the observed object calls setchanged and noifyobservers. - Håkan Jonsson 19 - Håkan Jonsson 20 - Håkan Jonsson 21 7

- Håkan Jonsson 22 Recap: Model- View- Control The Model- View- Control (MVC) design pagern defines a relamon between three classes: The model, maintaining a state. The view, displaying the state. The control, instrucmng changes in the state. We implement this separamon as follows: The view will observe the model. The control will be a listener. Programming consequences: The model will extend Observable. The view will implement Observer. The control will implement AcIonListener. Lecture 8 - Håkan Jonsson 23 Example: LampGUIExample Lecture 8 - Håkan Jonsson 24 8

Lecture 8 - Håkan Jonsson 25 5. UML: AggregaMon l09.gui.examples "LampGUI has an (or consists of) a JPanel." JFrame LampGUI Observable LampGUIExample JPanel JButton <<interface>> ActionListener <<interface>> Observer LampController LampModel LampView Example: LampGUIExample A window with a lamp and a bucon l09.gui.examples Main program JFrame Observable LampGUI LampGUIExample JPanel JButton <<interface>> ActionListener <<interface>> Observer LampController LampModel LampView MVC design pacern - Håkan Jonsson 27 9

Lecture 8 - Håkan Jonsson 28 6. Layouts Components added to a container occupy space in the final graphical picture. They are also placed within the available area. Usually next to each other. In Java, every GUI container has a layout manager that specifies how sizing and placement of components is done. The default layout manager for a JPanel is FlowLayout. When windows are resized, the layout needs to be changed. The rules for how this is done is set in the layout manager. - Håkan Jonsson 29 Example: FlowLayoutExample - Håkan Jonsson 30 10

Lecture 8 - Håkan Jonsson 31 7. More layout managers Lecture 8 - Håkan Jonsson 32 Example: GridBagLayoutExample - Håkan Jonsson 33 11

Example: SpringLayoutExample A very general layout manager that specifies relamonships between the edges of components. Constraints that allow dynamic GUI:s. Example: 4 components as shown below. - Håkan Jonsson 34 Example: SpringLayoutExample NORTH WEST text SOUTH EAST 10 5 10 5 5 5 5 label text number button 5 5 5 panel layout.putconstraint(springlayout.west, text, 5, SpringLayout.EAST, label); - Håkan Jonsson 35 12