CS 2113 Software Engineering

Similar documents
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!

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

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

CS11 Java. Fall Lecture 4

Graphic User Interfaces. - GUI concepts - Swing - AWT

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

User interfaces and Swing

Systems Programming Graphical User Interfaces

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

GUI in Java TalentHome Solutions

Graphical User Interfaces. Comp 152

Window Interfaces Using Swing Objects

Introduction to the JAVA UI classes Advanced HCI IAT351

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

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

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

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

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

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

Window Interfaces Using Swing Objects

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Graphical User Interfaces (GUIs)

Graphical User Interface (GUI)

Packages: Putting Classes Together

Graphical User Interface (GUI)

Java. GUI building with the AWT

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

Java Programming Lecture 6

Part I: Learn Common Graphics Components

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

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

CS11 Java. Fall Lecture 3

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

Jonathan Aldrich Charlie Garrod

Lecture 9. Lecture

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

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

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

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

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

CS 251 Intermediate Programming GUIs: Components and Layout

Unit 7: Event driven programming

Class 16: The Swing Event Model

Introduction. Introduction

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

Java Graphical User Interfaces AWT (Abstract Window Toolkit) & Swing

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

Graphical User Interfaces 2

Graphical User Interface (GUI)

Object Oriented Design & Patterns. Part 1

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

Sri Vidya College of Engineering & Technology

PIC 20A GUI with swing

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

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

IT101. Graphical User Interface

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

Graphical User Interfaces

To gain experience using GUI components and listeners.

2IS45 Programming

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

Lecture 5: Java Graphics

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

Laying Out Components. What is Widget Layout?

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Java Swing. Recitation 11/(20,21)/2008. CS 180 Department of Computer Science, Purdue University

DM503 Programming B. Peter Schneider-Kamp.

Programming graphics

Class 14: Introduction to the Swing Toolkit

(Incomplete) History of GUIs

CSC 161 SPRING 17 LAB 2-1 BORDERLAYOUT, GRIDLAYOUT, AND EVENT HANDLING

Java: Graphical User Interfaces (GUI)

CS 251 Intermediate Programming GUIs: Event Listeners

CHAPTER 2. Java Overview

Graphical User Interfaces 2

1.00 Lecture 14. Lecture Preview

Programming Mobile Devices J2SE GUI

Agenda. Container and Component

Graphical User Interfaces 2

2110: GUIS: Graphical User Interfaces

Object-Oriented Programming: Revision. Revision / Graphics / Subversion. Ewan Klein. Inf1 :: 2008/09

CSE Lab 8 Assignment Note: This is the last lab for CSE 1341

Computer Science 210: Data Structures. Intro to Java Graphics

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

Calculator Class. /** * Create a new calculator and show it. */ public Calculator() { engine = new CalcEngine(); gui = new UserInterface(engine); }

Basicsof. JavaGUI and SWING

University of Cape Town Department of Computer Science. Computer Science CSC117F Solutions

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

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

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

Programmierpraktikum

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

CS 11 java track: lecture 3

BASICS OF GRAPHICAL APPS

CSSE 220 Day 19. Object-Oriented Design Files & Exceptions. Check out FilesAndExceptions from SVN

Swing from A to Z Some Simple Components. Preface

Transcription:

CS 2113 Software Engineering Java 5 - GUIs Import the code to intellij https://github.com/cs2113f18/template-j-5.git Professor Tim Wood - The George Washington University

Class Hierarchies Abstract Classes Interfaces Last Time...!2

GUIs in Java AWT vs Swing Swing Basics Event Handling This Time Inner and Anonymous classes!3

Project 2... Zombie Infestation Simulator brains?!4

Zombie Sim Structure ZombieSim main() instantiates city loop: update city and draw City private Walls[][] update draw populate() what else to add??? Tips/Best Practices: - Think carefully about class structure and the data and functions in each one - Think carefully about the "is a" versus "has a" relationship when designing your classes - It is better to have a class interact with another using an API (functions) instead of directly accessing data - Use classes to encapsulate both data and functions. A City class should be responsible for everything to do with the city and a Cat class would be responsible for everything to do with cats, etc.!5

Starting next week: Labs Over-achievers Should not go to lab Middlers Must go to lab Strugglers Must go to lab and start assignments early You will get an email with your group (based on grade in course)!6

Quiz: bit.ly/petsquiz2 Click Fork, then Edit Write a program to: Store two types of pets---cats and dogs When you create a pet, the constructor takes a name. Cats also take a number of lives remaining. All pets have a printname() function that prints the name All pets have a makenoise() function Cats: "NAME says meow" and dogs: "NAME says woof" Your main method should: Create a single array with two dogs named Fido and Spot, and three cats named Fluffy, Mowzer, and Pig Print the names of all the pets Call the makenoise function on the first dog and second cat Use good OOP practices!!7

What is a GUI library? A way to: Open windows Display widgets on screen Process events Widgets: Buttons, images, Menu bars, tabs, popups, etc Events: Mouse clicks, keyboard interactions, windows being moved/resized/minimized/closed, etc!8

GUIs in Java Abstract Window Toolkit (AWT) Java library to interact with the OS's native graphical interface tools Swing Interface library relying (almost) purely on Java JavaFX Newer redesign of Swing We will use Swing!9

GUIs are made up of: Containers Holds other widgets Components A widget to interact with or display something Common examples: JFrame JPanel JLabel hello world JButton JButton Frame: basic window Panel: an area to group other objects or draw images/art TextField/TextArea: allows text input Simple widgets: Checkbox, List Button, Label, Scrollbar and Scrollpane. Swing widget classes all start with "J"!10

Our First Window Is this code enough? import javax.swing.*; public class TestSwing1 { public static void main (String[] argv) { JFrame f = new JFrame ();!11

Our First Window Is this code enough? import javax.swing.*; public class TestSwing1 { public static void main (String[] argv) { JFrame f = new JFrame (); Nope! Also need to: Give the window a size and make itself visible!12

Open a Window Get the code for today from the class site Look at the guis.helloswing.java file What happens when you run it? What happens when you try to close the window? Can you figure out how to set the title of the window to "Hello World"? More fun: - have the window appear at a specific location - open five windows instead of one!13

Content Pane A JFrame has a ContentPane to hold widgets Can use this to: Make low level drawing calls (strings, circles, lines, etc) Add components like buttons, sliders, and other containers JFrame JPanel JLabel hello world JButton JButton!14

Draw me a picture Draw a pretty picture Edit the guis.prettypicture.java file drawrect(int topleftx, int toplefty, int width, int height): The first two integers specify the topleft corner. The next two are the desired width and height of the rectangle. drawoval(int topleftx, int toplefty, int width, int height): The first two integers specify the topleft corner. The next two are the desired width and height of the enclosing rectangle. Also have filledrect and filledoval equivalents drawline(int x1, int y1, int x2, int y2 ): Unfortunately, the line thickness is fixed at one pixel. To draw thicker lines, you have to "pack" one-pixel lines together yourself.!15

Another way to say "hello" It doesn't always make sense to use drawstring() Low level function What if we want to change the text dynamically? Does not feel very "object oriented" Can also use the JLabel component Container cpane = f.getcontentpane(); JLabel hellolabel = new JLabel("Hello!"); cpane.add(hellolabel); Gives us an object to store a message Add it to a panel/frame and it will be drawn automagically!!16

JLabel Try out guis.helloswing2.java At a low level, how do you think JLabel works? Where does the label appear? What happens if you create another JLabel and add it to the frame as well?!17

Java Layout Managers Swing (and AWT) use Layout Managers to control where components are placed You (generally) do not have precise control over placement Simplifies automated GUI creation Makes hand designed GUIs trickier Default layout manager: BorderLayout North West Center East South!18

JLabel take two You can specify (approximately) where to add a component with: cpane.add(hellolabel, BorderLayout.WEST); // or.east, NORTH, SOUTH, CENTER Add a second JLabel so that it does NOT replace the first one More fun: - go back to PrettyPicture.java and make it add three PrettyPanels to the window next to each other!19

More Layouts Commonly used layouts managers: BorderLayout: tries to place components either in one of five locations: North, South, East, West or Center (default). FlowLayout: places components left to right and row-by-row. CardLayout: displays only one component at a time, like a rolodex. GridLayout: places components in a grid. GridBagLayout: uses a grid-like approach that allows for different row and column sizes. Change a container's layout with: Container cpane = f.getcontentpane(); cpane.setlayout(new FlowLayout());!20

Events and Listeners Clicking a button, tapping a key, or moving the mouse causes events What happens if a tree falls in a forest and nobody is there to hear it? Same idea with buttons How do you think events should work codewise?!21

How should this work? Any class that is a MouseListener should implement the following classes: public void mouseclicked(mouseevent m); public void mouseentered(mouseevent m); public void mouseexited(mouseevent m); public void mousepressed(mouseevent m); public void mousereleased(mouseevent m); What support does Java provide to ensure our class will definitely handle these methods?!22

Event Interfaces Java uses Interfaces! Interfaces define a contract - you must implement the methods Then we can: Define a new class that implements the appropriate interface Tell a JFrame that our class can definitely support events related to dragging the mouse around Tell a JButton that our class can definitely support events related to clicking it!23

Mouse/Keyboard Events Interfaces for basic mouse and keyboard events MouseListener public void mouseclicked(mouseevent m); public void mouseentered(mouseevent m); public void mouseexited(mouseevent m); public void mousepressed(mouseevent m); KeyListener public void mousereleased(mouseevent m); public void keytyped(java.awt.event.keyevent arg0); public void keypressed(java.awt.event.keyevent arg0); public void keyreleased(java.awt.event.keyevent arg0);!24

actionlistener Button Events Must implement one function: public void actionperformed (ActionEvent a) ActionEvent object: a.getactioncommand() Returns the command string associated with this action. the button s label a.getwhen() Returns the timestamp of when this event occurred. a.getsource() Returns the object on which the Event initially occurred. the button itself!25

Inside a Button Click me! public class JButton extends AbstractButton { private ArrayList<ActionListener> listeners; protected void fireactionperformed(actionevent event) { for(actionlistener al: listeners) { al.actionperformed(event); protected addactionlistener(actionlistener L) { listeners.add(l);!26

Button Events Something must implement ActionListener One option: have the JFrame do it class NewFrame extends JFrame implements ActionListener { public NewFrame (int width, int height) { //... button.addactionlistener(this); Why this? //... public void actionperformed (ActionEvent a) { System.out.println ("ActionPerformed!");!27

Simple Button In guis.buttons.buttonaction Implement the ActionListener interface Define an actionperformed() function Use b.addactionlistener to set the object that will handle the events Print a message when the button is clicked and make it exit the program with System.exit()!28

Multiple Buttons In guis.buttons.buttontest Make the hello and bye buttons change the text of the msg label. Two approaches: 1: Create different event handler classes for each Button 2: Look at the event parameter to distinguish the source of an event within a single event handler class Let s use both! Already have a QuitActionListener class for quit Make NewFrame2 implement ActionListener for the other buttons How can we differentiate between the two buttons? Inspect the ActionEvent parameter you are passed a.getactioncommand() returns the clicked button's text label a.getsource() returns a reference to the object that started the event (i.e., the JButton instance that was clicked)!29

...that can get messy when we have lots buttons! public void actionperformed (ActionEvent a) { // Get the button string. String s = a.getactioncommand(); if (s.equalsignorecase ("Bold")) { //... else if (s.equalsignorecase ("Italic")) { //... else if (s.equalsignorecase ("Right Justify")) { //...!30

What else can we do? Having one giant event handler function is messy Need to be careful that a change to one button won't break code for another Implementing lots of classes is also undesirable What is a key limitation of using separate classes? What can you do in NewFrame2 but not in a separate class? We want to use OOP principles! Compartmentalize functionality Reuse code instead of copy/pasting Isolate and protect data!31

Options... We could create custom classes just for handling the events class QuitButtonHandler implements ActionListener { public void actionperformed (ActionEvent a) { System.out.println ("ActionPerformed!"); class NewFrame extends JFrame { public NewFrame () { //... quitbutton.addactionlistener(new QuitButtonHandler); randbutton.addactionlistener(new RandomButtonHandler);!32

Problem??? We could create custom classes just for handling the events class QuitButtonHandler implements ActionListener { public void actionperformed (ActionEvent a) { System.out.println ("ActionPerformed!"); What if the event handler needs access to data from NewFrame? class NewFrame extends JFrame { public NewFrame () { //... quitbutton.addactionlistener(new QuitButtonHandler); randbutton.addactionlistener(new RandomButtonHandler);!33

What we really want: To isolate the event handler for each object To allow the event handlers to access the data of the class they are in!34

What we really want: To isolate the event handler for each object but a single class can only implement the functions in an interface once! To allow the event handlers to access the data of the class they are in but if we use separate classes for each event handler we won't be able to do this! Oh noes! :(!35

Java to the rescue! Inner Classes Use an Inner Class class myclass { class myinnerclass { void somefunc() {!36

Inner Classes Use an Inner Class class mypanel { private JLabel mylabel; class ehandler1 implements ActionListener { mylabel.settext("handler 1!"); class ehandler2 implements ActionListener { class ehandler3 implements ActionListener {!37

What can it do? Can an inner class touch its outer's privates? Yes it can! Can an "outer" class call functions in the inner? Yes it can! Can an inner class implement an interface? Yes it can! Can an inner class extend another class? Yes it can! Can an inner class access local variables in outside functions? No it can t!!38

Sample Code Check out guis.inner.innertest.java Note: The inner class can have: functions, data, and constructors The inner class can access private data of its outer class The outer class can access private data of the inner class Todo: Make the outer class print out the values of X and Y!39

Inner Class Event Handlers Look at guis.inner.innerevents.java We want to have: Quit: quits Hello: display "hello" World: prints "bye" (in the msg JLabel) Bye The quit button currently uses an inner class Your turn: Add two new inner classes for Hello and Bye Elite Hacker: Combine your two inner classes into a single inner class!40

Types of Classes in Java A public/private class Must have name equal to file A class with no privacy modifier Only usable within that package An inner class inside of another class Inner can access the outer and vice versa An anonymous inner class Declared as part of a function call quitb.addactionlistener ( new ActionListener() { public void actionperformed (ActionEvent a) { System.exit (0); ); and a few other types too!!41

Event Listeners!42