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.

Slides:



Advertisements
Similar presentations
OLD BUSINESS : Lets talk about the next exam Project proposals – Functional Definitions Go over homework – NEW BUSINESS: Chapter 4 today GUIs Assignment.
Advertisements

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Outline Graphics Applets Drawing Shapes Components and Containers Images.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java.
ITEC220 GUI Lecture – Part 2 References  Java Software Solutions,” by Lewis & Loftus  Chapter 7  Chapter 8  Chapter 9  Java Foundations-Introduction.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Chapter 5: Enhancing Classes
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Event Handling Events and Listeners Timers and Animation.
Chapter 5: Keyboard I/O & Simple GUI’s Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Chapter Day 7. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 7 Questions from last Class?? Problem set 1 Corrected  Good results 3.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Chapter Day 10. © 2007 Pearson Addison-Wesley. All rights reserved4-2 Agenda Day 10 Questions from last Class?? Problem set 2 posted  10 programs from.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Chapter 4 Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
ITEC220 GUI Lecture – Part 1 References  Java Software Solutions,” by Lewis & Loftus  Chapter 3  Chapter 4  Chapter 5  Chapter 6  Java Foundations-Introduction.
ELC 312 Day 6. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Agenda Questions? Problem set two Posted in WebCT  Due Sept 30  On Page 153&154.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2012 Pearson Education, Inc. Chapters 3-8 Graphical User Interfaces Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
© 2004 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4: Writing classes Intermediate Java Programming Summer 2007.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Chapter 5: Enhancing Classes
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
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.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
Chapter 4 Writing Classes. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Writing Classes We've been using predefined classes. Now we will learn.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Object Oriented Programming.  Interface  Event Handling.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 – Graphical User Interfaces Java Foundations: Introduction to Programming.
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 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
1 Objects and Classes -- Now that some low-level programming concepts have been established, we can examine objects in more detail Chapter 4 focuses on:
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Processing Timer Events
Chapter 5: Enhancing Classes
Java Programming: From Problem Analysis to Program Design,
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chapter 4 Writing Classes.
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Timer class and inner classes
Chapter 4 Writing Classes.
Chapter 4 Writing Classes.
Chapter 4 Writing Classes
Chapter 4 Writing Classes.
Chapter 4 Writing Classes
Chapter 4 Writing Classes
4 Writing Classes Software Solutions Lewis & Loftus java 5TH EDITION
ITEC220 GUI Lecture – Part 2 References
Presentation transcript:

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 3 & 4  Due in 7 Days (Oct 6) Quiz 1 will be Oct 6  Chapter 1-4  25 M/C open book, open notes, 40 Min  Maybe an extra credit question Today we will  Continue on Chapter 4 Writing classes  Do in-class GROUP lab on writing classes  More on Graphics and GUIS

Chapter 4 Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

4-4 myMethod(); myMethodcompute Method Control Flow If the called method is in the same class, only the method name is needed

© 2007 Pearson Addison-Wesley. All rights reserved4-5 doIt helpMe helpMe(); obj.doIt(); main Method Control Flow The called method is often part of another class or object

© 2007 Pearson Addison-Wesley. All rights reserved4-6 Method Header A method declaration begins with a method header char calc (int num1, int num2, String message) method name return type parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal parameter

© 2007 Pearson Addison-Wesley. All rights reserved4-7 Object and Methods

© 2007 Pearson Addison-Wesley. All rights reserved4-8 In class Lab Representing names  Page 58 of the lab manual Representing Boxes  PP 4.3 on Page 203 of the text

© 2007 Pearson Addison-Wesley. All rights reserved4-9 Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

© 2007 Pearson Addison-Wesley. All rights reserved4-10 Graphical Objects Some objects contain information that determines how the object should be represented visually Most GUI components are graphical objects We can have some effect on how components get drawn We did this in Chapter 2 when we defined the paint method of an applet Let's look at some other examples of graphical objects

© 2007 Pearson Addison-Wesley. All rights reserved4-11 Smiling Face Example The SmilingFace program draws a face by defining the paintComponent method of a panel See SmilingFace.java (page 182)SmilingFace.java See SmilingFacePanel.java (page 183)SmilingFacePanel.java The main method of the SmilingFace class instantiates a SmilingFacePanel and displays it The SmilingFacePanel class is derived from the JPanel class using inheritance

© 2007 Pearson Addison-Wesley. All rights reserved4-12 Smiling Face Example Every Swing component has a paintComponent method The paintComponent method accepts a Graphics object that represents the graphics context for the panel We define the paintComponent method to draw the face with appropriate calls to the Graphics methods Note the difference between drawing on a panel and adding other GUI components to a panel

© 2007 Pearson Addison-Wesley. All rights reserved4-13 Splat Example The Splat example is structured a bit differently It draws a set of colored circles on a panel, but each circle is represented as a separate object that maintains its own graphical information The paintComponent method of the panel "asks" each circle to draw itself See Splat.java (page 185)Splat.java See SplatPanel.java (page 187)SplatPanel.java See Circle.java (page 188)Circle.java

© 2007 Pearson Addison-Wesley. All rights reserved4-14 Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

© 2007 Pearson Addison-Wesley. All rights reserved4-15 Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects:  components  events  listeners We've previously discussed components, which are objects that represent screen elements  labels, buttons, text fields, menus, etc. Some components are containers that hold and organize other components  frames, panels, applets, dialog boxes

© 2007 Pearson Addison-Wesley. All rights reserved4-16 Events An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs:  the mouse is moved  the mouse is dragged  a mouse button is clicked  a graphical button is clicked  a keyboard key is pressed  a timer expires Events often correspond to user actions, but not always

© 2007 Pearson Addison-Wesley. All rights reserved4-17 Events and Listeners The Java standard class library contains several classes that represent typical events Components, such as a graphical button, generate (or fire) an event when it occurs A listener object "waits" for an event to occur and responds accordingly We can design listener objects to take whatever actions are appropriate when an event occurs

© 2007 Pearson Addison-Wesley. All rights reserved4-18 Events and Listeners Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event Event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event

© 2007 Pearson Addison-Wesley. All rights reserved4-19 GUI Development Generally we use components and events that are predefined by classes in the Java class library Therefore, to create a Java program that uses a GUI we must:  instantiate and set up the necessary components  implement listener classes for any events we care about  establish the relationship between listeners and components that generate the corresponding events Let's now explore some new components and see how this all comes together

© 2007 Pearson Addison-Wesley. All rights reserved4-20 Outline Anatomy of a Class Encapsulation Anatomy of a Method Graphical Objects Graphical User Interfaces Buttons and Text Fields

© 2007 Pearson Addison-Wesley. All rights reserved4-21 Buttons A push button is a component that allows the user to initiate an action by pressing a graphical button using the mouse A push button is defined by the JButton class It generates an action event The PushCounter example displays a push button that increments a counter each time it is pushed See PushCounter.java (page 192)PushCounter.java See PushCounterPanel.java (page 193)PushCounterPanel.java

© 2007 Pearson Addison-Wesley. All rights reserved4-22 Push Counter Example The components of the GUI are the button, a label to display the counter, a panel to organize the components, and the main frame The PushCounterPanel class is represents the panel used to display the button and label The PushCounterPanel class is derived from JPanel using inheritance The constructor of PushCounterPanel sets up the elements of the GUI and initializes the counter to zero

© 2007 Pearson Addison-Wesley. All rights reserved4-23 Push Counter Example The ButtonListener class is the listener for the action event generated by the button It is implemented as an inner class, which means it is defined within the body of another class That facilitates the communication between the listener and the GUI components Inner classes should only be used in situations where there is an intimate relationship between the two classes and the inner class is not needed in any other context

© 2007 Pearson Addison-Wesley. All rights reserved4-24 Push Counter Example Listener classes are written by implementing a listener interface The ButtonListener class implements the ActionListener interface An interface is a list of methods that the implementing class must define The only method in the ActionListener interface is the actionPerformed method The Java class library contains interfaces for many types of events We discuss interfaces in more detail in Chapter 6

© 2007 Pearson Addison-Wesley. All rights reserved4-25 Push Counter Example The PushCounterPanel constructor:  instantiates the ButtonListener object  establishes the relationship between the button and the listener by the call to addActionListener When the user presses the button, the button component creates an ActionEvent object and calls the actionPerformed method of the listener The actionPerformed method increments the counter and resets the text of the label

© 2007 Pearson Addison-Wesley. All rights reserved4-26 Text Fields Let's look at another GUI example that uses another type of component A text field allows the user to enter one line of input If the cursor is in the text field, the text field component generates an action event when the enter key is pressed See Fahrenheit.java (page 196)Fahrenheit.java See FahrenheitPanel.java (page 197)FahrenheitPanel.java

© 2007 Pearson Addison-Wesley. All rights reserved4-27 Fahrenheit Example Like the PushCounter example, the GUI is set up in a separate panel class The TempListener inner class defines the listener for the action event generated by the text field The FahrenheitPanel constructor instantiates the listener and adds it to the text field When the user types a temperature and presses enter, the text field generates the action event and calls the actionPerformed method of the listener The actionPerformed method computes the conversion and updates the result label

© 2007 Pearson Addison-Wesley. All rights reserved4-28 Summary Chapter 4 focused on:  class definitions  instance data  encapsulation and Java modifiers  method declaration and parameter passing  constructors  graphical objects  events and listeners  buttons and text fields