MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Graphic User Interfaces Layout Managers Event Handling.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Chapter 51 More About Objects and Methods Chapter 5.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
© The McGraw-Hill Companies, 2006 Chapter 18 Advanced graphics programming.
Buttons. Appearance of buttons A button has one of three appearances: Disabled by your program Enabled by your program Enabled by your program and pressed.
Intro to Object-Oriented (“OO”) Design. OO Design Simplified methodology 1. Write down detailed description of problem 2. Identify all (relevant) nouns.
Review CSC 171 FALL 2004 LECTURE 21. Topics Objects and Classes Fundamental Types Graphics and Applets Decisions Iteration Designing Classes Testing and.
Chapter Day 9. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 8 Questions from last Class?? Problem set 2 posted  10 programs from.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Welcome to CIS 083 ! Events CIS 068.
Chapter 8: Writing Graphical User Interfaces
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.
F27SB2 Software Development 2 Lecture 4: Java GUIs 3.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Chapter 5: Enhancing Classes
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Copyright © Curt Hill First Window Builder Program Easy GUIs in Eclipse.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
COMP 321 Week 2. Outline Event-Driven Programming Events, Event Sources, Event Listeners Button and Timer Events Mouse Events, Adapters.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Object Oriented Programming.  Interface  Event Handling.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 5 Objectives  Learn about basic GUI components.  Explore how the GUI.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
Creating User Interfaces Event-Driven Programming.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
Lecture 33: More Graphical User Interface (GUI) Announcements & Review Read Ch GU1 & GU2 Cohoon & Davidson Ch 14 Reges & Stepp Lab 10 set game due 4/26.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs & Event-Driven Programming Chapter 11 Review.
GUI Programming using Java - Event Handling
CompSci 230 S Programming Techniques
Java Programming: Guided Learning with Early Objects
“Form Ever Follows Function” Louis Henri Sullivan
Chapter 8: Writing Graphical User Interfaces
Java Programming: From Problem Analysis to Program Design,
Ellen Walker Hiram College
Tonga Institute of Higher Education
Presentation transcript:

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 21 INTRODUCTORY PROGRAMMING Week 4 Lecture 2 Fonts –Horstmann 4.6 ActionEvents –different events from different buttons –Horstmann is too complicated Scope and Null pointers Further Programming overview

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 22 The Font Class Java supplies the Font class to allow text to be displayed in different fonts and styles A Font object has a constructor that fixes –the name, the style, the size Objects which contain text, such as labels and text fields, have a setFont method Read Horstmann 4.6 but don’t worry about the Advanced topic (accurate positioning)

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 23 Font Style and Size Style is fixed by one of three public static constants in the Font class Font.PLAIN, Font.BOLD, Font.ITALIC –These are actually integers and can be added Font.BOLD + Font.ITALIC Size is expressed as a point size –10 or 12 are common sizes for readable text

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 24 The Font Name An actual font name such as Times New Roman, Arial, Helvetica, Courier –Uses the font in the operating system A logical font name such as Serif, SansSerif, Monospaced. –Uses the most suitable available font Use a non-proportional font to control text position (used in FP1) –E.g. Courier (actual) or Monospaced (logical) –Each character takes up the same amount of space

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 25 User Interface Events There are many user interface events. Today we are interested in button clicks Other events might be…… –Mouse pressed, released, clicked, dragged,... –Text value changed –Window closed, activated –Check box, list box, combo box changed –etc

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 26 An Event-driven program An event driven program waits for an event and then reacts to it –User, not programmer, controls the order in which events occur –Natural model for a program with a GUI –The programmer specifies which events they wish to handle –Events which are not handled are ignored The java.awt.event package is used to –Listen for events –Transfer control to the correct place in the program

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 27 Java Event Handling Model Button Operating System ActionEvent Listening Class fires action event passes action event details to is registered to listen for Event source Event handler

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 28 The event source The event source (e.g. Button) –Can generate the event (e.g. button clicks) –Manages the listeners - specifies which class contains the methods which will handle the event The class is specified using the addActionListener method In our case, the method which handles the event will be in the same class i.e. the GUI class, and is referred to as this

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 29 The listeners The event listener class must provide standard methods to handle the event –The class does this by implementing the appropriate Listener interface This interface describes the required methods Any class that implements the interface must provide these methods

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 210 The ActionListener Interface An interface is used when a collection of objects need to perform some common tasks. In this case, the collection of objects is all the GUIs that have buttons The interface which specifies the required methods for button clicks is called ActionListener The method which handles button clicks is called actionPerformed All the information about the button click is provided in an ActionEvent object, which is a parameter to the actionPerformed method.

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 211 What can go wrong with listeners? What if you do not register the listener class with the button? (i.e.no addActionListener ) What if your GUI class implements ActionListener, but does not have an actionPerformed method? What if you provide the actionPerforme d method and do not implement ActionListener ? What if you forget to import the java.awt.event package?

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 212 The event and the source A GUI will often have several buttons generating ActionEvents –Each button sends a package of details to the operating system when it is clicked –These details include a reference to the object firing the event (the event source) –The ActionEvent class has a method getSource() this returns the reference to that object, so you can then check which button was clicked

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 213 Summary : Enabling a GUI class to handle ActionEvents Import java.awt.event.* Add implements ActionListener to the class header use the addActionListener method for each button that is to fire an event: myButton.addActionListener(this) write an actionPerformed method containing the event handling code, using e.getSource() to determine which event occurred

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 214 Declaring a primitive data type When a primitive data type is declared, int num; a small space is allocated to it. A default value is assigned in this space, which is 0 for numbers, something unprintable for characters. When the value is assigned to the variable (num = 3;), the default value is replaced. num 0 3

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 215 Declaring an object When an object datatype (Traveller, JButton etc) is declared without assignment (=), a small amount of space is allocated to it to hold a ‘pointer’. –The pointer is the address in memory of the full details for the object. E.g. JButton myButton; Traveller tourist; –This value is initially null myButton Points nowhere

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 216 Assigning values to an object A value is assigned usually by –For objects, calling the Constructor myButton = new JButton (“Process Now”); tourist = new Traveller(“Monica”, “Geneva”); Be careful not to redeclare the object when you assign it. You would then have a –A local variable within the Constructor called myButton which has had the details (text, colour etc) assigned –AND the original instance variable myButton which still has no details. Points to details myButton Larger area holding details

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 217 Null Pointer Exception If you try to refer to an OBJECT variable which has been declared but not been initialised //declared as instance variable E.g. JLabel myLabel; //repeated declaration when laying out components JLabel myLabel = new JLabel(“some text”); //alteration in actionPerformed() myLabel.setFont(firstFont); java.lang.NullPointerException at ButtonGUI.changeToCourier(ButtonGUI.java:82 at ButtonGUI.actionPerformed(ButtonGUI.java:71) at javax.swing.AbstractButton.fireActionPerformed (AbstractButton.java:1786) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed (AbstractButton.java:1839) Etc………..

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 218 Other scope effects What if a button is declared as an instance variable and declared again when laying out the components? What if a font is declared as an instance variable and never instantiated?

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 219 Strings (recap) A sequence of characters Index numbering starts at 0 Methods to return an altered version of the String –convert to lower case, substring Methods to return a value –character at a certain index –Index of a character –length of a string Joe Blgogs

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 220 Arrays (Further Programming) A sequence of types (primitive or object) Index numbering starts at 0 Write our own (standard) methods to –Search for a value –Sort into order –Insert a value –Delete a value

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 221 Graphical User Interface More complex layouts Text Input/output Formatting input and output Writing and reading complex lines from text files

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 222 Foundations Programming lots of support from tutors and demonstrators non-assessed exercises multiple choice test you can judge whether you are going to find programming –easy choose other programming modules –or hard avoid programming-based modules possibly only able to achieve Diploma may need to commit more time to the course

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 223 Further Programming 3 Lectures per week Choose a tutorial time that suits you –Not in first week You will be reassigned to lab groups on Mon 3 -5, Tue 3- 5 –Not in first week Study the lecture handouts and associated programs, then work on the assignments

MSc/PgDip in ITIntroductory Programming: Week 4 Lecture 224 Assessment 3 assessed exercises over first 6 weeks –weight 10% each –inflexible deadlines (unless medical certificate) –marks lost by late submission –2 hours of lab time per week when tutor/demonstrator available ‘Project’ –handed out in week 6, deadline during second week of second semester –weight 35% –very little support provided Exam in May 35%