Miscellaneous Topics #6: Polygons GUI Components and Event Handlers

Slides:



Advertisements
Similar presentations
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Advertisements

Mouse Events and Keyboard Events
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 6: Arrays & More GUI Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used.
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.
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
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 Day 19. © 2007 Pearson Addison-Wesley. All rights reserved7-2 Agenda Day 19 Problem set 3 Corrected  1 A, 2 B’s and 1 D Problem set 4 Posted.
ELC 312 Day 16. © 2004 Pearson Addison-Wesley. All rights reserved Agenda Questions? Capstone Proposals Due Problem set 3 Corrected  Poor performance.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington GUI and the UI API COMP.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
 Definition: An event is an object thrown in response to a user of programmatic action  Definition: A listener is a series of methods that executes in.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Object Oriented Programming.  Interface  Event Handling.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating User Interfaces Event-Driven Programming.
Graphical User Interfaces A Graphical User Interface (GUI) in Java is created with at least three kinds of objects: –components, events, and listeners.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Event Driven (Asynchronous) Programming. Event handling in Unity Subclass a class that contains event handling methods, and then override those methods.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
CSC 205 Programming II Lecture 5 AWT - I.
Events and Event Handling
Chapter 14 Event-Driven Programming
Lecture 9.5 Event Delegation.
CHAPTER Reacting to the user.
Applets.
Chapter 12 Event-Driven Programming
Keyboard Events keyboard acceleration TextFields
Processing Timer Events
Java Programming: From Problem Analysis to Program Design,
Programming in Java Event Handling
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Event Driven Programming
EE 422C Java FX.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Event loops.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Event Driven Systems and Modeling
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Java Inner Classes.
Events, Event Handlers, and Threads
Event loops.
Event loops.
Presentation transcript:

Miscellaneous Topics #6: Polygons GUI Components and Event Handlers

Polygons and Polylines Arrays can be helpful in graphics processing For example, they can be used to store a list of coordinates A polygon is a multisided, closed shape A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled See PolygonShape.java See RocketApp.java

The Polygon Class The Polygon class can also be used to define and draw a polygon It is part of the java.awt package Versions of the overloaded drawPolygon() and fillPolygon() methods take a single Polygon object as a parameter instead of arrays of coordinates A Polygon class has the intersects() methods which returns true if the Polygon intersects with a given rectangle. This method takes four argument, the (x,y) top- left coordinate of the rectangle and its width, and height.

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

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

A corresponding listener Events and Listeners Event Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event

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 Graphical User Interface (GUI) we must: instantiate and set up the necessary components (like JFrames) implement listener classes for any events we care about establish the relationship between listeners and components that generate the corresponding events Let's now look at one such listener that works for the JFrame components and see how this all comes together

KeyListener Example Listener classes are written by implementing a listener interface The KeyAdapter class implements the KeyListener interface An interface is a list of methods that the implementing class must define One method in the KeyListener interface that is implemented in the KeyAdapter class is the KeyTyped method, and this method is sent the event when a keyboard character is typed The Java class library contains interfaces for many types of events

KeyListener Example Here is an example of how to add a handler for certain typed keyboard characters to a GraphicsWindow object (named “graphicsWin”) by creating a KeyListener for it’s JFrame: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (ch == 'q' || ch == 'Q') System.exit(0); } });

KeyListener Example The keyTyped method only handles alpha-numeric keys that are typed. You need to use the keyPressed() method to handle special keys: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { char ch = e.getKeyChar(); int code = e.getKeyCode(); if (ch == 'q' || ch == 'Q') System.exit(0); if (ch == 'u' || ch == 'U' || code == KeyEvent.VK_UP) paddle.moveUp(); if (ch == 'd' || ch == 'D' || code == KeyEvent.VK_DOWN) paddle.moveDown(); } });

KeyListener Example Another approach is to send the characters typed to a separate method like handleKey() to take the appropriate action: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { handleKey(e.getKeyChar()); } }); --------- public static void handleKey(char ch) if (ch == 'p' || ch == 'P') pauseSimulation = (pauseSimulation) ? false : true; if (ch == 'q' || ch == 'Q') System.exit(0);

KeyListener Example Another approach is to send the characters typed to a separate SubClass like KeyListener below, which has a keyPressed() method to take the appropriate action: graphicsWin.getFrame.addKeyListener(new KeyListener()); --------- public static class KeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { char ch = e.getKeyChar(); int code = e.getKeyCode(); if (ch == 'u' || ch == 'U' || code == KeyEvent.VK_UP) paddle.moveUp(); if (ch == 'd' || ch == 'D' || code == KeyEvent.VK_DOWN) paddle.moveDown(); } });