Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Java Programming ® JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page i TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Check the Web for Updates To check for updates or corrections relevant to this book and/or CD-ROM visit our updates page on the Web at http://www.prima-tech.com/support. Send Us Your Comments To comment on this book or any other PRIMA TECH title, visit our reader response page on the Web at http://www.prima-tech.com/comments. How to Order For information on quantity discounts, contact the publisher: Prima Publish- ing, P.O. Box 1260BK, Rocklin, CA 95677-1260; (916) 787-7000. On your letter- head, include information concerning the intended use of the books and the number of books you want to purchase. For individual orders, turn to the back of this book for more information. JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page ii TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Java Programming Joseph P. Russell ® JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page iii TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. © 2001 by Prima Publishing. All rights reserved. No part of this book may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photo- copying, recording, or by any information storage or retrieval system without written permission from Prima Publishing, except for the inclusion of brief quotations in a review. A Division of Prima Publishing Prima Publishing and colophon are registered trademarks of Prima Communications, Inc. PRIMA TECH is a trademark of Prima Communi- cations, Inc., Roseville, California 95661. Java, Forte, NetBeans and all trademarks and logos based on Java, Forte and NetBeans are trademarks or registered trade- marks of Sun Microsystems, Inc. in the U.S. and other coun- tries. Internet Explorer is a registered trademark of Microsoft Corporation in the United States and/or other countries. Netscape is a registered trademark of Netscape Communica- tions Corporation in the U.S. and other countries. Important: Prima Publishing cannot provide software sup- port. Please contact the appropriate software manufacturer’s technical support line or Web site for assistance. Prima Publishing and the author have attempted through- out this book to distinguish proprietary trademarks from descriptive terms by following the capitalization style used by the manufacturer. Information contained in this book has been obtained by Prima Publishing from sources believed to be reliable. How- ever, because of the possibility of human or mechanical error by our sources, Prima Publishing, or others, the Publisher does not guarantee the accuracy, adequacy, or completeness of any information and is not responsible for any errors or omissions or the results obtained from use of such informa- tion. Readers should be particularly aware of the fact that the Internet is an ever-changing entity. Some facts may have changed since this book went to press. ISBN: 0-7615-3522-5 Library of Congress Catalog Card Number: 2001-091380 Printed in the United States of America 00 01 02 03 04 BB 10 9 8 7 6 5 4 3 2 1 Publisher: Stacy L. Hiquet Managing Editor: Sandy Doell Acquisitions Editor: Melody Layne Project Editor: Kezia Endsley Technical Reviewer: Michelle Jones Copy Editor: Kezia Endsley Associate Marketing Manager: Heather Buzzingham Interior Layout: William Hartman Cover Design: Prima Design Team Indexer: Sharon Shock Proofreader: Jenny Davidson JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page iv TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. This book is dedicated to Brianne, Tyler, and to the rest of my family, past, present, and future. You all make life on this great big ball of dung worth living. JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page v TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. would like to thank my parents, Joe and Joan, for cheering me on and helping me out with Brianne and Tyler while I was working on this book. Thanks also to Brianne and Tyler for keeping me happy while I wasn’t. Thanks also to my sister Roseanne, for putting me on the right career path. I love all of you. I’d also like to thank Kezia Endsley for doing a great editing job and rewording my babble so that it actually makes sense. Thanks to Melody Layne for finding me and bringing me in to work in this pro- ject. Thanks also to Michelle Jones, Jenny Davidson, and everyone else that was a part of this project. A c k n o wl e d gm e n t s I JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page vi TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. oseph P. Russell is a development programmer for Meditech, a major medical information systems software and service company, developing software for their client/server financial products. He is a Sun certified programmer for the Java 2 platform. He is also a contributing writer for eastcoastgames.com (http://www.eastcoastgames.com). He is a graduate from Rhode Island College where he majored in computer science. He also worked as a Web developer for their Web site (http://www.ric.edu). It was during his college years that he acquired a passion for Web development and decided to learn Java and JavaScript. After graduating, he worked as a Web developer/programmer for Progressive Systems Technology, a company that provides Progress database con- sulting and develops applications for commercial Internet based companies. In his spare time, he enjoys game programming, painting, and playing his elec- tric bass guitar. He is a father of two beautiful children, a girl and a boy, and he loves being a family man. You can visit his home page at http://members. home.net/j.p.russell or e-mail him at j.p.russell@home.com. A b o u t t he A u t h o r J JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page vii TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page viii This page intentionally left blank TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Introduction xix CHAPTER 1 Getting Started 1 CHAPTER 2 Variables, Data Types, and Simple I/O 25 CHAPTER 3 The Fortune Teller: Random Numbers, Conditionals, and Arrays 55 CHAPTER 4 Using Loops and Exception Handling 93 CHAPTER 5 Blackjack: Object-Oriented Programming 121 CHAPTER 6 Creating a GUI Using the Abstract Windowing Toolkit 171 CHAPTER 7 Advanced GUI: Layout Managers and Event Handling 221 CHAPTER 8 Writing Applets 277 CHAPTER 9 The Graphics Class: Drawing Shapes, Images, and Text 313 CHAPTER 10 Animation, Sounds, and Threads 353 CHAPTER 11 Custom Event Handling and File I/O 377 CHAPTER 12 Creating Your Own Components and Packages 429 APPENDIX A Using the CD-ROM 477 APPENDIX B Java Language Summary 481 Index 491 C on t e n t s a t a G la n c e JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page ix TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]...JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page x Contents Introduction 1 C H A P T E R xix Getting Started 1 The Project: the HelloWeb Applet What Is Java? Java Is a Programming Language Java Is Platform Independent Java Is Object-Oriented Why Learn Java? Java Is Relatively Easy to Learn Java Works Everywhere Installing and Setting Up the Java SDK Windows (Win32) Installation... from the ground up Java has infinitely more uses in the real world than game development The purpose of this book is not to teach game development I use game programs as fun examples that demonstrate programming concepts that you can apply to any kind of Java programming solution Many companies use Java because of its platform independence Another use of Java is to create applets for Internet solutions... 464 464 465 465 474 476 APPENDIX Using the CD-ROM 477 APPENDIX Java Language Summary 481 Index 491 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page xix Introduction H ello and welcome to Java Programming for the Absolute Beginner You probably already have a good understanding... interest in programming was sparked that very day This book thoroughly covers basic programming concepts using the Java programming language You apply these concepts through programming games that are not only challenging and rewarding to create, but are fun to play! In addition, you can apply your knowledge to change the game programs to work the way you want them to, just as I did when I started programming. .. Arrays The ArrayTest Program Back to the Fortune Teller Summary 4 C H A P T E R Using Loops and Exception Handling The Project: The NumberGuesser Counting Forward with Loops The Racer Program The for Loop The Increment (++) Operator Skipping Values The CountByFive Program Using Compound Assignment Operators Counting Backwards Making a for Loop Count Backwards Nested for Loops Looping on Arrays Looping on... Methods Getting Back to the Name Game Summary The Fortune Teller: Random Numbers, Conditionals, and Arrays The Project: the Fortune Teller Generating Random Numbers The NumberMaker Application The java. util.Random Class The Math Class Controlling the Random Number Range Getting Values Larger Than 1 Specifying a Range The Dice Roller TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase... applets for Internet solutions Internet companies, such as online stores, can use Java to create user interfaces that online shoppers can use to purchase their goods They can also use server-side Java programs to keep their databases up to date as their products are being purchased Knowledge of Java is a TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com... Running the Application Learning Java Syntax Basics Including Comments The main() Method Writing Your First Applet Back to the HelloWeb Applet! Writing the HTML Running the Applet Summary 2 3 4 4 5 7 7 7 8 8 10 10 11 12 12 14 15 17 19 20 20 21 21 23 TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark JavaProgAbsBeg-00Fnt.qxd 2/25/03... continue Statements TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 67 68 71 73 74 77 78 79 80 81 83 85 85 86 88 88 90 91 93 94 94 95 96 97 98 99 100 101 102 103 105 106 106 108 109 112 112 JavaProgAbsBeg-00Fnt.qxd 2/25/03 8:11 AM Page xiii xiii 5 C H A P T E R Blackjack: Object-Oriented Programming The Project: the BlackJack... PlayAreaListeners Firing PlayAreaEvents Creating the PlayArea Class Inner Classes Accepting User Input for Block Movements Making Blocks Fall The EventThread Inner Class Putting it All Together Creating the ScoreInfoPanel Class Reading and Writing Files Keeping Score Creating the Block Game Application Summary TEAM LinG - Live, Informative, Non-cost and Genuine! Please purchase PDF Split-Merge on www.verypdf.com to . HelloWeb Applet 2 What Is Java? 3 Java Is a Programming Language 4 Java Is Platform Independent 4 Java Is Object-Oriented 5 Why Learn Java? 7 Java Is Relatively Easy to Learn 7 Java Works Everywhere. demonstrate programming concepts that you can apply to any kind of Java programming solution. Many companies use Java because of its platform independence. Another use of Java is to create applets for. company, developing software for their client/server financial products. He is a Sun certified programmer for the Java 2 platform. He is also a contributing writer for eastcoastgames.com (http://www.eastcoastgames.com).