COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Similar documents
Window Interfaces Using Swing Objects

Example: CharCheck. That s It??! What do you imagine happens after main() finishes?

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

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

Graphical User Interface (GUI)

CS 106A, Lecture 23 Interactors and GCanvas

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

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!

Window Interfaces Using Swing Objects

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

Graphic User Interfaces. - GUI concepts - Swing - AWT

AP CS Unit 11: Graphics and Events

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

MIT AITI Swing Event Model Lecture 17

Swing from A to Z Some Simple Components. Preface

First Name: AITI 2004: Exam 2 July 19, 2004

CHAPTER 2. Java Overview

Lecture 5: Java Graphics

Java. GUI building with the AWT

CompSci 125 Lecture 17. GUI: Graphics, Check Boxes, Radio Buttons

An array is a type of variable that is able to hold more than one piece of information under a single variable name.

Systems Programming Graphical User Interfaces

First Name: AITI 2004: Exam 2 July 19, 2004

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Introduction to the JAVA UI classes Advanced HCI IAT351

We are on the GUI fast track path

H212 Introduction to Software Systems Honors

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Swing - JButton. Adding buttons to the main window

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

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

Programming Languages and Techniques (CIS120)

Graphical User Interfaces. Comp 152

UI Software Organization

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

PIC 20A GUI with swing

Graphical User Interface (Part-1) Supplementary Material for CPSC 233

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

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

Graphical User Interfaces (GUIs)

IT101. Graphical User Interface

Programming Languages and Techniques (CIS120e)

CS 251 Intermediate Programming GUIs: Components and Layout

Interfaces & Polymorphism part 2: Collections, Comparators, and More fun with Java graphics

CSE 331 Software Design & Implementation

Assignment 2. Application Development

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

Lecture 5: Inheritance

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Graphical User Interface

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

CS 170 Java Programming 1. Week 15: Interfaces and Exceptions

SD Module-1 Advanced JAVA

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

Lecture 3: Java Graphics & Events

CS 106A, Lecture 24 Interactors and NameSurfer

SD Module-1 Advanced JAVA. Assignment No. 4

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

CSC207 Week 4. Larry Zhang

To gain experience using GUI components and listeners.

More Swing. CS180 Recitation 12/(04,05)/08

DM503 Programming B. Peter Schneider-Kamp.

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

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall (total 7 pages)

GUI Basics. Object Orientated Programming in Java. Benjamin Kenwright

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Name: Checked: Learn about listeners, events, and simple animation for interactive graphical user interfaces.

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

COMPSCI 230. Software Design and Construction. Swing

Implementing Graphical User Interfaces

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

Programmierpraktikum

Final Examination Semester 2 / Year 2012

Commands. Written commands can be reused, cataloged, etc. Sometimes they also contain "undo" commands, too.

Java - Applets. public class Buttons extends Applet implements ActionListener

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

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

Java Help Files. by Peter Lavin. May 22, 2004

Lab & Assignment 1. Lecture 3: ArrayList & Standard Java Graphics. Random Number Generator. Read Lab & Assignment Before Lab Wednesday!

CS 11 java track: lecture 3

GUI Forms and Events, Part II

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

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

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

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

Part I: Learn Common Graphics Components

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

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

ANSWER KEY Exam 2 Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 5 November 2009

CS11 Java. Fall Lecture 3

G51PRG: Introduction to Programming Second semester Applets and graphics

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

Graphical User Interface (GUI)

Datenbank-Praktikum. Universität zu Lübeck Sommersemester 2006 Lecture: Swing. Ho Ngoc Duc 1

CS Exam 1 Review Suggestions

8. Polymorphism and Inheritance

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

Transcription:

COMP-202 Unit 10: Basics of GUI Programming (Non examinable) (Caveat: Dan is not an expert in GUI programming, so don't take this for gospel :) )

Course Evaluations Please do these. -Fast to do -Used to improve course for future. (Winter 2011 had 6 assignments reduced to 4 based on feedback!) 2

GUI Programming Basics -Setting up a window -Basics of drawing shapes, etc -Responding to events -Good object oriented design 3

Basic Terminology -JFrame : This is your window -Component : This is a piece of a your window. Components contain other components. 4

Getting Started The first thing to do is imports: import java.awt.*; import java.awt.event.*; import javax.swing.*; 5

Next create a JFrame Usually, we will make our class be a JFrame. This brings us to a notion we didn't cover this term : inheritance and polymorphism 6

Inheritance We talked about has-a relationships when defining objects. e.g. A Point HAS an x and a y coordinate. In Java, you can also define is-a relationships. This is very useful when you have many objects that have some similar traits, but are not exactly the same. 7

Example: Shapes Suppose I am modeling a bunch of geometric shapes. There are many kinds of shapes: -square -rectangle -circle -triangle -pyramid Each of these are somewhat different but they do have some things in common. 8

Example: Shapes In Java, I can specify when I define a class that I want a class to BE-A subclass of another class. For example, it doesn't make sense to think of a square as containing a shape. Conceptually a square IS-A shape. 9

Example: Shapes One way that I can define is-a relationships in Java is using inheritance public class Square extends Shape The extends Shape means that every property and method of the Shape class (we assume already defined) is automatically passed down to the Square class. 10

Example: Shapes One immediate use out of this is I avoid code duplication between my Square, Rectangle, Circle, etc classes. Properties of ALL shapes are automatically moved to these new classes. Another benefits is that I can actually store into a Shape variable a Square. Shape shape = new Square(); This is fine because a Shape variable needs to store a link to a Shape. But a Square IS-A Shape! 11

ArrayLists with inheritance This allows me to do some useful things (we'll see how in a few slides) such as store an ArrayList<Shape> and fill it partly with Square and partly with Circle and partly with Rectangle. This is very useful in graphics programming because we will define a JFrame, which remember is essentially a window. This window will contain all of the different subwindows that it needs to draw. These subwindows may all be very different. 12

Dynamic Dispatch Even more useful: Suppose that my Shape class has defined a method called getarea() Now suppose that inside my Square class I ALSO write a method called getarea() When I write: Shape shape = new Square(); shape.getarea(); I am calling the Square getarea! Even though the variable is a Shape variable! 13

Getting started To start, we will define our class to be an extension of a JFrame. Remember that this will mean that every property and method of a Jframe our class automatically has. public class TipCalcuator extends JFrame 14

main method Now we will add a very short main method. The main method will simply create a TipCalculator, which remember, creates a Jframe (a window). We then will set it to be visible. public static void main(string[] args) { TipCalculator window = new TipCalculator(); window.setvisible(true); } The method setvisible is defined as part of Jframe, so we don't need to write it. 15

Constructor for TipCalculator Inside our constructor for TipCalculator, we can now set various properties: public TipCalculator() { this.settitle( Tip Calculator ); this.setdefaultcloseoperator(jframe.exit_on_close); this.setcontentpane(generatetaxchart()); this.pack(); //controls layout } We don't need to write any of these methods except for generatetaxchart(); 16

Containers The idea of this model of GUI programming is our window will have one main Container. This main Container maintains a list of Components. A Container IS a Component which means we can put Containers inside other Containers which in turn can contain lists of other containers. 17

Example hierarchy For example, if we are writing a web brower, we may divide our main window into several containers: -File/Edit/View bar -Toolbar with buttons (back/forward/etc) -Main page viewer -Bottom tool bar with info like link info Each of these containers is subdivided into further components/containers. Remember, the idea here is organization. It would be a mess if we had ALL buttons in one container. 18

What does a container need to know Each container needs to know two things: 1)What other containers it will hold. 2)How to link the containers together in the display. -left to right? -right to left? -top to bottom? -grid? uniform grid? -other? 19

What does a container need to know Each container needs to know two main things: 1)What other containers it will hold. 2)How to link the containers together in the display. -left to right? -right to left? -top to bottom? -grid? uniform grid? -other? Related questions: How large? Borders? 20

What does a container need to know? All Containers have two methods defined on them: -add(component c) -Adds a Component to the Container. (Remember a Container is-a Component) -setlayout(layoutmanager mgr) -There are many kind of LayoutManager. Each LayoutManager specifies how the various containers should be combined together on the screen. 21

JComponent A JComponent is a class that IS-A Container. Remember that every Container IS-A Component. So we will effectively add lots of Jcomponents. There are many kinds of Jcomponent: -JPanel -JButton -JTextField Each have different properties but share some others 22

How to control drawing? Every component determines how to draw itself based on a method with header: public void paint(graphics g) This method is defined inside JContainer so it is not necessary to change it. However, if you override this method by adding the same method AGAIN to your base class, you can specify exactly how to draw. 23

How to control drawing? public class MyPaintWindow extends Component {... public void paint(graphics g) { //in here you can control how MyPaintWindow looks Graphics2D g2 = (Graphics2D) g; } } Inside some other part of your TipCalculator, you could now add a new MyPaintWindow() to a container 24

Graphics2D object A Graphics2D object allows you to do a lot of things: -drawimage (display a jpg/bmp or sprite ) -drawpolygon -draw(shape s) -etc 25

Setting up a window So setting up a window essentially consists of 1)Drawing what we want on a piece of paper 2)Organizing the different parts of our window into various Containers, creating them, and then adding to them. Each of these Containers will normally contain other Containers. 26

How to react to things? Once we set up the window, how can we do anything? For example, how can we make it so that when a user types something we respond. Or how can we make a button click do something? Or make a button gray until a user types some text? 27

Listeners In normal programming, we think of our program as executing the main method from top to bottom. In GUI programming though, we think of it a bit differently. -We initially set up our window and then we listen for events. -We specify what to do to respond in each of these cases. 28

JButton JButton has a method addactionlistener() that takes as input an ActionListener An ActionListener must have the following header (name could change) public class ComputeTaxButtonListener implements ActionListener 29

implements The impements keyword is similar to extends in that it specifies an Is-a relationship. However, unlike an inheritance example, you are not defining an extension you are implementing an interface. This means that the base class does NOT provide the method implementations, just the methods that should be there. 30

implements You can't create an ActionListener directly because no methods are actually defined. Just the interface or the method headers are there. Any class that implements an ActionListener MUST have a method with header: public void actionperformed(actionevent e) (it can, of course, have other additional methods) 31

implements After you add an ActionListener to a JButton, there will constantly be a listener attached to the JButton. When someone clicks the button, code will jump to your method you defined actionperformed Inside actionperformed, you can do things such as set the property of a text field. taxamounttf.settext( + taxamount); 32

Layout in code public class MyWindow extends JFrame { private JtextField taxamounttf;... private void inializebuttonrow() { JButton button = new JButton( Click me! ); button.addactionlistener(new ModifyTaxAmountListener()); //add button to some container } //see next slide } 33

Layout in code public class MyWindow extends JFrame { class ModifyTaxAmountListener implements ActionListener(ActionEvent e) { taxamounttf.settext( You clicked! ); //also methods on JTextFields such //as setbackground or seteditable } } 34

Good design principles Generally, we try to separate business logic (e.g. the rules of calculating taxes) from GUI logic. To do this, rather than calculate the tax in the same class as the GUI, we try to have our GUI class store one property, for example TaxController Then when a user does certain actions, we will update properties in TaxController and get data from it. 35

Example: TipCalculator double taxrate = Double.parseDouble(taxRateTF.getText()); double tiprate = Double.parseDouble(tipRateTF.getText()); double price = Double.parseDouble(priceTF.getText()); taxrate); tiprate); double taxamount = TipCalculator.calculatePercentage(price, double tipamount = TipCalculator.calculatePercentage(price, taxamounttf.settext("" + taxamount); tipamounttf.settext("" + tipamount); Notice that we do NOT read from the text field directly in calculatepercentage. We simply provide that method with the information. Ideally this is even better if it's in another class (to further establish organization) 36

Who cares anyway? There are 2 (related) important reasons to separate these from each other: 1)Code reusability : By keeping things separate, it means if you want to switch to a different GUI (e.g. webpage, console window, etc), you don't need to start from scratch. You just need to redesign the window (which you have to do anyway) 2)Splitting things better : Allows separate design of code logic from GUI logic. This is better as sometimes a coder is not good at laying things out. 37

maybe him? 38

Other things This is a really brief sampling of GUI programming in Java. The best way to learn this is by practice and also reading the tutorials. http://docs.oracle.com/javase/tutorial/uiswing/index. html There is also the idea of 3d programming : This is a very complicated subject. You can look at a library such as Java3d. In general there is a lot of linear algebra in this because much of it relies on matrix multiplications. One issue here is with runtime and 39 performance.