EVENTS Wiring together objects, code, and actions.

Slides:



Advertisements
Similar presentations
Chapter 16 GUI Programming Basics GUI Overview Event-Driven Programming Basics GUI Classes and Packages A Simple Window Program JFrame Class Java Components.
Advertisements

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.
Computer Science 209 Images and GUIs. Working with Java Colors The class java.awt.Color includes constants, such as Color.red, for some commonly used.
Web Design & Development Lecture 19. Java Graphics 2.
Examples. // A simple Frame with Rectangle Inside import java.awt.*; import javax.swing.*; import java.awt.geom.*; // For Shapes class rectComponent extends.
Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Bar Graph Design. Left-side/Right-side mechanical processing creative, abstract reasoning.
Lecture 10.2 Different Types of Loops, including for and do.
Introduction to Java Classes, events, GUI’s. Understand: How to use TextPad How to define a class or object How to create a GUI interface How event-driven.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
Combo Box, Check Boxes, and Radio Buttons. Radio Buttons User can click radio buttons, just like other buttons BUT Radio buttons are mutually exclusive.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
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,
Inheritance Like father like son Image from:
GUI Components and Design Here we add one more component to our programs, JButtons –JButtons can only be inserted into JPanels (or JApplets) –Clicking.
CS 11 java track: lecture 4 This week: arrays interfaces listener classes inner classes GUI callbacks.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
Java Graphics Graphical Components as objects. Graphics A Component is ◦A rectangular region of a computer screen ◦A graphical entity ◦Can sometimes contains.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
1 Class 6. 2 Objectives Objectives Enable your applications to perform actions in response to JButton clicks Get the text the user enters in a textfield.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Anonymous Classes An anonymous class is a local class that does not have a name. An anonymous class allows an object to be created using an expression.
Introduction to GUI in 1 Graphical User Interface 2 Nouf Almunyif.
A simple swing example GETTING STARTED WITH WIND CHILL.
4.3.1 Non-void Methods Parameters are largely one-way communication.  Shared instances variables is one way to accomplish this. calling codemethod parameter.
Chapter 11 - A GUI Interacting With a Problem Domain Class1 Chapter 11 A GUI Interacting With a Problem Domain Class 11.
More GUI Components We finish off our examination of GUI components today by looking at a few that we haven’t seen yet –we start off with the JSlider the.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 4 – Completing the Inventory Application.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
A cannon game ?. Simple version angle from command line, one shot only Coordinate system is “upside-down”: Use dy(int) method to transform y coordinate:
Creating a GUI Class An example of class design using inheritance and interfaces.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 5.1 Test-Driving the Enhanced Inventory Application.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
1 Layout Managers Layout managers –Provided for arranging GUI components –Provide basic layout capabilities –Processes layout details –Programmer can concentrate.
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,
KeyListener and Keyboard Events Just as we can implement listeners to handle mouse events, we can do the same for keyboard events (keypresses) –to implement.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Lecture 8.5 Animating with EventTimer. © 2006 Pearson Addison-Wesley. All rights reserved A Crash Course in the Use of Timed Events What kinds of.
GUIs & Event-Driven Programming Chapter 11 Review.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
1 Event Driven Programming with Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Lecture 15.1 Event Delegation.
GUIs and Events Rick Mercer.
A First Look at GUI Applications
public class Doubler extends Base {
Ellen Walker Hiram College
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Introduction to Event Handling
Class Hierarchies and Type Conformance
Advanced Programming in Java
Computer Science 209 Images and GUIs.
Presentation transcript:

EVENTS Wiring together objects, code, and actions

Events 2  An ‘event’ is something that the user does  move the mouse  push down on a key  let up on a key  click the mouse button  …  A program should usually respond to these events  This happens by attaching ‘code’ to an ‘event’

EventButton 3 «constructor» + EventButton( String txt) «query» + int getX( ) + int getY( ) + int getWidth( ) + int getHeight( ) + String getText( ) «update» + repaint() + setSize( int, int ) + setText( String ) «event handler» + actionPerformed( ActionEvent ) EventButton «constructor» + EventButton( String txt) «query» + int getX( ) + int getY( ) + int getWidth( ) + int getHeight( ) + String getText( ) «update» + repaint() + setSize( int, int ) + setText( String ) «event handler» + actionPerformed( ActionEvent )

A Solution 4 import java.awt.event.*; import java.awt.*; public class RightButton extends EventButton { private Oval dot; public RightButton(int x, int y, int w, int h, Oval c) { super(“Move Right”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() + 5, dot.getY()); dot.repaint(); } import java.awt.event.*; import java.awt.*; public class RightButton extends EventButton { private Oval dot; public RightButton(int x, int y, int w, int h, Oval c) { super(“Move Right”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() + 5, dot.getY()); dot.repaint(); }

The Client 5 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); win.repaint(); win.setVisible(true); } Image courtesy Can we make program with two buttons. One moving the circle left and another moving it right?

A Solution 6 import java.awt.event.*; import java.awt.*; public class LeftButton extends EventButton { private Oval dot; public LeftButton(int x, int y, int w, in th, Oval c) { super(“Move Left”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() - 5, dot.getY()); dot.repaint(); } import java.awt.event.*; import java.awt.*; public class LeftButton extends EventButton { private Oval dot; public LeftButton(int x, int y, int w, in th, Oval c) { super(“Move Left”); setBounds(x,y,w,h); dot = c; } public void actionPerformed(ActionEvent e) { dot.setLocation(dot.getX() - 5, dot.getY()); dot.repaint(); }

The Client 7 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } Image courtesy

Another Way (Callbacks) 8  It is often useful to write a button class that doesn’t know anything about an Oval (or any other object that it is meant to operate on)  The button might command another object to perform the operation.  The other object is typically the controller.

The Client 9 public class OvalController { Oval oval; public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } public void moveLeft() { oval.setLocation(oval.getX()-5, oval.getY()); oval.repaint(); } public void moveRight() { oval.setLocation(oval.getX()+5, oval.getY()); oval.repaint(); } public class OvalController { Oval oval; public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); oval = new Oval(50,50,50,50); win.add(oval); RightButton rButton = new RightButton(5, 5, 100, 20, oval); win.add(rButton, 0); LeftButton lButton = new LeftButton(5, 30, 100, 20, oval); win.add(lButton); win.repaint(); win.setVisible(true); } public void moveLeft() { oval.setLocation(oval.getX()-5, oval.getY()); oval.repaint(); } public void moveRight() { oval.setLocation(oval.getX()+5, oval.getY()); oval.repaint(); } Image courtesy Make the oval an instance variable so it can be used in two new methods. These methods are callback methods. The buttons will “call-back” to them when pushed.

Another Way 10 import java.awt.event.*; public class LeftButton extends EventButton { private OvalController controller; public LeftButton(int x, int y, int w, int h, OvalController c) { super(“Move Left”); setBounds(x, y, w, h); controller = c; } public void actionPerformed(ActionEvent e) { controller.moveLeft(); } import java.awt.event.*; public class LeftButton extends EventButton { private OvalController controller; public LeftButton(int x, int y, int w, int h, OvalController c) { super(“Move Left”); setBounds(x, y, w, h); controller = c; } public void actionPerformed(ActionEvent e) { controller.moveLeft(); } import java.awt.event.*; public class RightButton extends EventButton { // other code omitted public void actionPerformed(ActionEvent e) { controller.moveRight(); } import java.awt.event.*; public class RightButton extends EventButton { // other code omitted public void actionPerformed(ActionEvent e) { controller.moveRight(); }

Text Fields 11  A text field is a way for users to type in textual information.  An EventTextField is  A Component  A JTextField EventTextField «constructor» + EventTextField( String txt) «query» + int getX() + int getY() + int getWidth() + int getHeight() + Color getParent() + String getText()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setText( String )... «event handler» + void actionPerformed( actionEvent ) EventTextField «constructor» + EventTextField( String txt) «query» + int getX() + int getY() + int getWidth() + int getHeight() + Color getParent() + String getText()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setText( String )... «event handler» + void actionPerformed( actionEvent )

Example 12 import java.awt.event.*; public class OvalSizerField extends EventTextField { private Oval oval; public OvalSizerField(int x, int y, int w, int h, Oval c) { super(c.getWidth() + “”); setBounds(x, y, w, h); oval = c; } public void actionPerformed(ActionEvent e) { String fieldText = getText(); int numericValue = new Scanner(fieldText).nextInt(); oval.setSize(numericValue, numericValue); } import java.awt.event.*; public class OvalSizerField extends EventTextField { private Oval oval; public OvalSizerField(int x, int y, int w, int h, Oval c) { super(c.getWidth() + “”); setBounds(x, y, w, h); oval = c; } public void actionPerformed(ActionEvent e) { String fieldText = getText(); int numericValue = new Scanner(fieldText).nextInt(); oval.setSize(numericValue, numericValue); }

The Client 13 import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); OvalSizerField f = new OvalSizerField(5, 5, 100, 20, oval); win.add(f); win.repaint(); win.setVisible(true); } import javax.swing.*; public class OvalController { public OvalController() { JFrame win = new JFrame(“Demo”); win.setSize(300, 300); win.setLayout(null); Oval oval = new Oval(50,50,50,50); win.add(oval); OvalSizerField f = new OvalSizerField(5, 5, 100, 20, oval); win.add(f); win.repaint(); win.setVisible(true); } Image courtesy

Sliders 14  A slider represents a single integer value.  The value is within a range of integers [min, max]  The value is controlled by the position of the slider To the left: value is min To the right: value is max Elsewhere: a value in [min, max]

15 EventSlider Crash Course EventSlider + static final int HORIZONTAL + static final int VERTICAL «constructor» + EventSlider( int, int, int, int ) «query» + int getX() + int getY() + int getWidth() + int getHeight() + int getMaximum() + int getMinimum() + Container getParent() + int getValue()... «update» + void repaint() + void setBounds( int, int, int, int ) + void setSize( int, int ) + void setLocation( int, int ) + void setMinimum( int ) + void setMaximum( int ) + void setValue( int )... «event handler» + void stateChanged( javax.swing.event.ChangeEvent ) knob getMinimum()getMaximum() getValue() called when user adjusts the knob or arrow buttons assigns new values to HORIZONTAL or VERTICAL getMinimum(), getMaximum() & getValue()

Example 16  Write a program to use three sliders to change the color of an oval. Each slider controls the amount of red, green or blue in the color of the on-screen oval.

Type Conformance 17  For any class C, an object V conforms to C if  V is an object of that exact type  OR V is a subclass of class C  For assignment of the form  VAR = EXPRESSION; The EXPRESSION must conform to the class of VAR  For a method of the form  TYPE NAME(type1 name1, type2 name2, …) {…} All actual parameters must conform to the specified types

Conformance 18 Write a method to return true if the height of a Rectangle is 10 pixels or greater and false otherwise. public boolean isTall(Rectangle r) { return r.getHeight() >= 10; } public boolean isTall(Rectangle r) { return r.getHeight() >= 10; } Write a method to return true if the height of an Oval is 10 pixels or greater and false otherwise. public boolean isTall(Oval o) { return o.getHeight() >= 10; } public boolean isTall(Oval o) { return o.getHeight() >= 10; } Write a method to return true if the height of an Oval or Rectangle or Line is 10 pixels or greater and false otherwise. public boolean isTall(Component v) { return v.getHeight() >= 10; } public boolean isTall(Component v) { return v.getHeight() >= 10; } Oval oval = new Oval(); Rectangle rect = new Rectangle(); SelectableRectangle rect2 = new ASelectableRectangle(); …isTall(rect)… …isTall(oval)… …isTall(rect2)…

Dynamic Conformance Testing 19  The instanceof operator tests conformance  Syntax:  Expression instanceof ClassName  Semantics:  Binary boolean-valued infix operator  Left-hand side is an object  Right-hand side is the name of a class  returns true if the LHS conforms to the RHS and false otherwise

Conformance 20 Write a method that accepts a Component object. If the object conforms to Rectangle then move it right by 5 pixels. If the object conforms to Oval then move it left by 5 pixels. All other Components should move down by 5 pixels. public void move(Component v) { int dx = 0, dy = 0; if(v instanceof Rectangle) { dx = 5; } else if(v instanceof Oval) { dx = -5; } else { dy = 5; } v.setLocation(v.getX() + dx, v.getY() + dy); } public void move(Component v) { int dx = 0, dy = 0; if(v instanceof Rectangle) { dx = 5; } else if(v instanceof Oval) { dx = -5; } else { dy = 5; } v.setLocation(v.getX() + dx, v.getY() + dy); }