Objects to Components with the Java(TM) Platform Copyright 2000 Art Gittleman Table of Contents 1. What Java Can Do 1.1 Overview of Java History Java's Features Simplicity and Robustness Architecture and Portability Multitasking How Java Works 1.2 Running Java Programs and Applets Editing, Compiling, and Running a Java Application Editing, Compiling, and Running a Java Applet A Graphical Applet 1.3 Program-Driven vs. Event-Driven Applications The Character Mode The Graphical User Interface (GUI) 1.4 Procedural vs. Object-Oriented Programming Procedural Programming Object-Oriented Programming 1.5 Networking, Databases, and Component Technology Using Object-Oriented and Event-Driven Programming Networking Databases Component Technology 1.6 Comparing Procedural Programming in Java and C++ The Structure of a Procedural Program Familiar Features Types Operators Control Structures Comments Unfamiliar Features String Concatenation Character-Mode Input A First Look at Packages Other Features Methods Constants Easy Output Mixed-Type Expressions Random Numbers and Other Math 2. Getting Started with Object-Oriented Programming 2.1 Using Objects Defining an Object Strings Values vs. References Comparing Strings 2.2 Introduction to Object-Oriented Programming Object-Oriented Design Use Cases and Scenarios Scenarios for an ATM System Classification 2.3 Classes and Objects The Structure of the BankAccount Class Instance Variables Instance Methods Constructors Using BankAccount Objects Creating Objects Making an Object Behave Method Overloading Class Variables and Methods 2.4 The QuickFood Example Class Diagrams Sequence Diagrams 2.5 Object Composition 3. Object-Oriented Concepts 3.1 Subclasses and Inheritance Classification Revisited Class Hierarchies The Object Class 3.2 Polymorhphism and Abstract Classes Polymorhic Operations Abstract Classes A Shape Class 3.3 Interfaces 3.4 Modifiers and Access A Second Look at Packages Class (and Interface) Visibility Data Field and Method Visibility Access to Data Fields Access to Methods 3.5 Enhancement: Object-Oriented Design with Use Cases and Scenarios Defining the Problem Designing a Solution: Object-Oriented Design--Developing Scenarios Designing a Solution: Object-Oriented Design--Assigning Responsibilities Designing a Solution: Object-Oriented Design--Defining the Classes Completing the Java Code Testing the Code 4. Arrays and Vectors 4.1 Java Arrays 4.2 Copying and Passing Arrays Copying References vs. Copying Values Passing an Array Argument 4.3 Arrays of Arrays 4.4 Enhancement: Insertion Sort Defining the Problem Designing a Solution: Devloping the Pseudocode Designing a Solution: Alternatives Completing the Java Code Testing the Code A Little Extra--Efficiency 4.5 Vectors and Enumerations 4.6 Using a Vector: Interfaces and Callbacks 5. Applets and Graphics 5.1 The World Wide Web and Applets Protocols Clients and Servers The URL HTML 5.2 Event-Driven Programming and a First Look at the AWT Responding to Paint Events Introducing the AWT 5.3 Drawing Shapes Graphics Drawing Drawing Relative to the Screen 5.4 Drawing Text Fonts Initializing an Applet Font Metrics 5.5 Using Color 5.6 Enhancement: Drawing the Tangram Puzzle Defining the Problem Designing a Solution: Starting with One Polygon Designing a Solution: Solving a Simpler Problem Completing the Java Code Testing the Code 6. Graphical User Interfaces 6.1 Introducing Components The Component Hierarchy Adding Buttons to a Container Flow Layout Drawing on a Canvas 6.2 Button and TextField The Java Event Model and Action Events Making Buttons Work Using Labels Number Formatting Wrapper Classes Entering Data in Text Fields 6.3 Checkbox and Choice Inner Classes Checkbox and Choice with a Button Checkbox and Choice with Item Events 6.4 Border and Grid Layouts The Border Layout Panels The Grid Layout 6.5 GridBag Layout Default GridBag Constraints Setting Weight and Fill Anchoring and Internal Padding Insets Positioning Constraints The Form Applet with TextArea 6.6 Enhancement: A GUI for Insertion Sorting Defining the Problem Designing a Solution: The Exploratory Process Designing a Solution: Making a Chart Desigining a Solution: Sorting Designing a Solution: The User Interface Completing the Java Code: Making the User Interface Work Testing the Code 7. Mouse, Key, and Window Events 7.1 Using the Mouse Mouse Events MouseListener and MouseMotionListener Moving the Mouse Adapter Classes 7.2 Using the Keyboard Focus Key Events KeyListener 7.3 Closing Windows Frames Window Events and WindowListener WindowAdapter 7.4 Applets <--> Standalone Applications Applets to Applications Console Application to GUI Application Standalone Application to Applet 7.5 Enhancement: Tangrams with the Mouse and Keys Defining the Problem Designing a Solution: Solving a Subproblem--Rotating a Polygon Completing the Java Code Testing the Code 8. Using Data 8.1 Exception Handling Exception Classes The Array Index Out Of Bounds Exception Control Flow for Exceptions The Number Format Exception 8.2 Text File Input and Output Reading from a File Reading Lines and Fields Writing to a File Inside our Io Class 8.3 Binary and Object Input and Output The File Class Reading and Writing Bytes Reading and Writing Primitive Types Random Access Files Reading and Writing Objects 8.4 Program Arguments and Applet Parameters Program Arguments Applet Parameters 8.5 Recursion: Searching and Sorting Binary Search Merge Sort 8.6 Data Structures: Linked Lists 8.7 Data Structures: Stacks 9. Threads and Multimedia 9.1 Introduction to Threads The Thread Class The Runnable Interface 9.2 Concurrent Programming Synchronization Communication Deadlock 9.3 Animation Pressing a Button to Move a Ball Animating a Ball Animating a Face 9.4 Images Locating an Image Using MediaTracker Animating an Image Double Buffering to Remove Flicker 9.5 Sound 10. Networking 10.1 Using a URL to Connect The Uniform Resource Locator (URL) Connecting from an Applet Connecting from a Standalone Application 10.2 Protocols with a URLConnection The Hypertext Transfer Protocol (HTTP) Using a URL Connection 10.3 Clients and Servers Using Sockets Server Ports A Client-Server Example A Request Header Server 10.4 Browsers and Web Servers A Very Simple Browser A Very Simple Web Server A Threaded Web Server 10.5 Remote Method Invocation (RMI) Distributed Computing: The RMI Solution The Interface The Server The Client 11. Java Database Connectivity (JDBC) 11.1 Database Tables and SQL Queries Relational Database Tables Structured Query Language 11.2 Connecting to a Database Database and Application Servers Creating an ODBC Source Connecting from Java Building the Database 11.3 Retrieving Information Viewing Query Results SELECT Statement Options 11.4 Metadata and Aggregate Functions Database Metadata Result Set Metadata Aggregate Functions 11.5 Prepared Statements and Transactions Using Prepared Statements Transaction Processing 11.6 A GUI for Database Queries The List Component 12. Java Beans 12.1 Building with Beans Starting the BeanBox Building an Applet with Beans 12.2 Bound and Constrained Properties Simple Properties Bound Properties Constrained Properties 12.3 Writing and Packaging a Bean A Simple Bean JAR Files A Bean with a Bound Property Smiley Bean Anonymous Inner Classes 12.4 Using BeanInfo What is BeanInfo? Friendly Bean BeanInfo for Friendly Packaging Friendly 12.5 Programming with Beans Using Beans in an Applet Customizing Beans Appendices A. Compendium of Java Basics B. The Java Class Hierarchy C. The AtmScreen Example D. The ASCII Character Set E. Using Java with Various Environments F. Operator Precedence Rules G. Binary and Hexadecimal Numbers H. Bitwise and Shift Operators