Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Similar documents
Digital Electronics A Practical Approach with VHDL William Kleitz Ninth Edition

Programmable Logic Controllers. Second Edition

GO! with Microsoft Access 2010 Introductory Gaskin Mclellan Graviett First Edition

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Pearson New International Edition. The 8051 Microcontroller and Embedded Systems Mazidi Mazidi McKinlay Second Edition

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Engineering with Excel. Ronald W. Larsen Fourth Edition

Elementary Number Theory. Kenneth H. Rosen Sixth Edition

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Workplace Communications The Basics George J. Searles Fifth Edition

Numerical Analysis Timothy Sauer Second Edition

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Technical Communication Today

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

Advanced Electronic Communications Systems Wayne Tomasi Sixth Edition

Graphical User Interfaces 2

Graphical User Interfaces 2

Engineering Problem Solving with C++

Graphical User Interfaces 2

IT101. Graphical User Interface

CS 134 Test Program #2

Example Programs. COSC 3461 User Interfaces. GUI Program Organization. Outline. DemoHelloWorld.java DemoHelloWorld2.java DemoSwing.

Method Of Key Event Key Listener must implement three methods, keypressed(), keyreleased() & keytyped(). 1) keypressed() : will run whenever a key is

GUI Program Organization. Sequential vs. Event-driven Programming. Sequential Programming. Outline

BASICS OF GRAPHICAL APPS

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CS 251 Intermediate Programming GUIs: Event Listeners

Object-Oriented Programming in Java

Calculus & Its Applications Larry J. Goldstein David Lay Nakhle I. Asmar David I. Schneider Thirteenth Edition

Programming Languages and Techniques (CIS120)

GLOBAL EDITION. Elementary Surveying. An Introduction to Geomatics FOURTEENTH EDITION. Charles D. Ghilani Paul R. Wolf

Graphical Applications

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

Apprenticeships BUSINESS & Functional Skills Level 1 ICT

AP Computer Science Unit 13. Still More Graphics and Animation.

GLOBAL EDITION. Assembly Language. for x86 Processors SEVENTH EDITION. Kip R. Irvine

Programming Languages and Techniques (CIS120)

Apprenticeships. Functional Skills Level 2 ICT

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Inheritance. One class inherits from another if it describes a specialized subset of objects Terminology:

Editorial Director, ECS: Publishing Administrator, International Edition: Editor-in-Chief:

More about JOptionPane Dialog Boxes

Programming Languages and Techniques (CIS120)

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

Event Driven Programming

GUI DYNAMICS Lecture July 26 CS2110 Summer 2011

Apprenticeships CUSTOMER. Functional Skills Level 1 MATHEMATICS

Essential Series. Springer-Verlag London Ltd.

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England. and Associated Companies throughout the world

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

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

19. GUI Basics. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

GUI Event Handlers (Part I)

GUI in Java TalentHome Solutions

Heinemann is a registered trademark of Pearson Education Limited

PESIT Bangalore South Campus

CSE 331 Software Design & Implementation

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!

References. Chapter 5: Enhancing Classes. Enhancing Classes. The null Reference. Java Software Solutions for AP* Computer Science A 2nd Edition

Outline. More on the Swing API Graphics: double buffering and timers Model - View - Controller paradigm Applets

Unit 7: Event driven programming

For Tricia: never dull, never boring, the smartest and bravest person I know

GUI Event Handlers (Part II)

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

11/7/12. Discussion of Roulette Assignment. Objectives. Compiler s Names of Classes. GUI Review. Window Events

The init() Method. Browser Calling Applet Methods

Lecture 3: Java Graphics & Events

CSC 160 LAB 8-1 DIGITAL PICTURE FRAME. 1. Introduction

Java & Graphical User Interface II. Wang Yang wyang AT njnet.edu.cn

H212 Introduction to Software Systems Honors

SD Module-1 Advanced JAVA

SD Module-1 Advanced JAVA. Assignment No. 4

GUI Components: Part 1

G51PRG: Introduction to Programming Second semester Applets and graphics

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Lecture 5: Java Graphics

GUI 4.1 GUI GUI MouseTest.java import javax.swing.*; import java.awt.*; import java.awt.event.*; /* 1 */

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

Lecture 19 GUI Events

2IS45 Programming

THE UNIVERSITY OF AUCKLAND

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

H212 Introduction to Software Systems Honors

Java Software Solutions Foundations of Program Design

Chapter 5: Enhancing Classes

COSC 123 Computer Creativity. Graphics and Events. Dr. Ramon Lawrence University of British Columbia Okanagan

11/19/2014. Inheritance. Chapter 7: Inheritance. Inheritance. Inheritance. Java Software Solutions for AP* Computer Science A 2nd Edition

ECE 462 Object-Oriented Programming using C++ and Java. Key Inputs in Java Games

Graphics. Lecture 18 COP 3252 Summer June 6, 2017

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England. and Associated Companies throughout the world

EXCEPTIONS & GUI. Errors are signals that things are beyond help. Review Session for. -Ankur Agarwal

Final Exam Computer Programming 230 Dr. St. John Lehman College City University of New York Thursday, 20 May 2010

OBJECT ORIENTED PROGRAMMING. Course 8 Loredana STANCIU Room B613

The AWT Event Model 9

Handling Mouse and Keyboard Events

Virtualians.ning.pk. 2 - Java program code is compiled into form called 1. Machine code 2. native Code 3. Byte Code (From Lectuer # 2) 4.

2. (True/False) All methods in an interface must be declared public.

Transcription:

Java Foundations John Lewis Peter DePasquale Joe Chase Third Edition

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world Visit us on the World Wide Web at: www.pearsoned.co.uk Pearson Education Limited 2014 All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, Saffron House, 6 10 Kirby Street, London EC1N 8TS. All trademarks used herein are the property of their respective owners. The use of any trademark in this text does not vest in the author or publisher any trademark ownership rights in such trademarks, nor does the use of such trademarks imply any affi liation with or endorsement of this book by such owners. ISBN 10: 1-292-02548-4 ISBN 13: 978-1-292-02548-3 ISBN 13: 978-1-292-03797-4 (Print) (PDF) British Library Cataloguing-in-Publication Data A catalogue record for this book is available from the British Library Printed in the United States of America

ListinG 28 //******************************************************************** // DirectionPanel.java Java Foundations // // Represents the primary display panel for the Direction program. //******************************************************************** import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DirectionPanel extends JPanel private final int WIDTH = 300, HEIGHT = 200; private final int JUMP = 10; // increment for image movement private final int IMAGE_SIZE = 31; private ImageIcon up, down, right, left, currentimage; private int x, y; // Constructor: Sets up this panel and loads the images. public DirectionPanel() addkeylistener (new DirectionListener()); x = WIDTH / 2; y = HEIGHT / 2; up = new ImageIcon("arrowUp.gif"); down = new ImageIcon("arrowDown.gif"); left = new ImageIcon("arrowLeft.gif"); right = new ImageIcon("arrowRight.gif"); currentimage = right; setbackground(color.black); setpreferredsize(new Dimension(WIDTH, HEIGHT)); setfocusable(true); 306

ListinG 28 continued // Draws the image in the current location. public void paintcomponent(graphics page) super.paintcomponent(page); currentimage.painticon(this, page, x, y); //***************************************************************** // Represents the listener for keyboard activity. //***************************************************************** private class DirectionListener implements KeyListener // Responds to the user pressing arrow keys by adjusting the // image and image location accordingly. public void keypressed(keyevent event) switch (event.getkeycode()) case KeyEvent.VK_UP: currentimage = up; y -= JUMP; case KeyEvent.VK_DOWN: currentimage = down; y += JUMP; case KeyEvent.VK_LEFT: currentimage = left; x -= JUMP; case KeyEvent.VK_RIGHT: currentimage = right; x += JUMP; repaint(); // Provide empty definitions for unused event methods. 307

ListinG 28 continued public void keytyped(keyevent event) public void keyreleased(keyevent event) image observer, the graphics context on which the image will be drawn, and the (x, y) coordinates where the image is drawn. An image observer is a component that serves to manage image loading; in this case we use the panel as the image observer. The private inner class called DirectionListener is set up to respond to key events. It implements the KeyListener interface, which defines three methods that we can use to respond to keyboard activity. Figure 8 lists these methods. Specifically, the Direction program responds to key pressed events. Because the listener class must implement all methods defined in the interface, we provide empty methods for the other events. The KeyEvent object passed to the keypressed method of the listener can be used to determine which key was pressed. In the example, we call the getkeycode method of the event object to get a numeric code that represents the key that was pressed. We use a switch statement to determine which key was pressed and to respond accordingly. The KeyEvent class contains constants that correspond to the numeric code that is returned from the getkeycode method. If any key other than an arrow key is pressed, it is ignored. void keypressed (KeyEvent event) Called when a key is pressed. void keyreleased (KeyEvent event) Called when a key is released. void keytyped (KeyEvent event) Called when a pressed key or key combination produces a key character. FiGUre 8 The methods of the KeyListener interface 308

Key events fire whenever a key is pressed, but most systems enable the concept of key repetition. That is, when a key is pressed and held down, it s as if that key is being pressed repeatedly and quickly. Key events are generated in the same way. In the Direction program, the user can hold down an arrow key and watch the image move across the screen quickly. The component that generates key events is the one that currently has the keyboard focus. Generally, the keyboard focus is held by the primary active component. A component usually gets the keyboard focus when the user clicks on it with the mouse. The call to the setfocusable method in the panel constructor sets the keyboard focus to the panel. The Direction program sets no boundaries for the arrow image, so it can be moved out of the visible window and then moved back in if desired. You could add code to the listener to stop the image when it reaches one of the window boundaries. This modification is left as a programming project. Key COnCept A listener class can be created by deriving it from an event adapter class. extending adapter Classes In previous event-based examples, we ve created the listener classes by implementing a particular listener interface. For instance, to create a class that listens for mouse events, we created a listener class that implemented the MouseListener interface. As we saw in the previous examples in this section, a listener interface often contains event methods that are not important to a particular program, in which case we provided empty definitions to satisfy the interface requirement. An alternative technique for creating a listener class is to use inheritance and extend an event adapter class. Each listener interface that contains more than one method has a corresponding adapter class that already contains empty definitions for all of the methods in the interface. To create a listener, we can derive a new listener class from the appropriate adapter class and override any event methods in which we are interested. Using this technique, we no longer need to provide empty definitions for unused methods. The MouseAdapter class, for instance, implements the MouseListener interface and provides empty method definitions for the five mouse event methods (mousepressed, mouseclicked, and so on). Therefore, you can create a mouse listener class by extending the MouseAdaptor class instead of by implementing the MouseListener interface directly. The new listener class inherits the empty definitions and therefore doesn t need to define them. Because of inheritance, we now have a choice when it comes to creating event listeners. We can implement an event listener interface, or we can extend an event adapter class. This is a design decision that should be considered carefully. Which technique is best depends on the situation. 309

5 Dialog boxes A component called a dialog box can be helpful in GUI processing. A dialog box is a graphical window that pops up on top of any currently active window so that the user can interact with it. A dialog box can serve a variety of purposes, such as conveying some information, confirming an action, or allowing the user to enter some information. Usually a dialog box has a solitary purpose, and the user s interaction with it is brief. The Swing package of the Java class library contains a class called JOptionPane that simplifies the creation and use of basic dialog boxes. Figure 9 lists some of the methods of JOptionPane. The basic formats for a JOptionPane dialog box fall into three categories. A message dialog box simply displays an output string. An input dialog box presents a prompt and a single input text field into which the user can enter one string of data. A confirm dialog box presents the user with a simple yes-or-no question. Let s look at a program that uses each of these types of dialog boxes. Listing 29 shows a program that first presents the user with an input dialog box that requests the user to enter an integer. After the user presses the OK button on the input dialog box, a second dialog box (this time a message dialog box) appears, informing the user whether the number entered was even or odd. After the user dismisses that box, a third dialog box appears, to determine whether the user would like to test another number. If the user presses the button labeled Yes, the series of dialog boxes repeats. Otherwise, the program terminates. The first parameter to the showmessagedialog and the showconfirmdialog methods specifies the governing parent component for the dialog box. Using a null reference as this parameter causes the dialog box to appear centered on the screen. static String showinputdialog (Object msg) Displays a dialog box containing the specified message and an input text field. The contents of the text field are returned. static int showconfirmdialog (Component parent, Object msg) Displays a dialog box containing the specified message and Yes/No button options. If the parent component is null, the box is centered on the screen. static void showmessagedialog (Component parent, Object msg) Displays a dialog box containing the specified message. If the parent component is null, the box is centered on the screen. FiGUre 9 Some methods of the JOptionPane class 310