Learn Java GUI Applications
Table of Contents
1. Introduction to Java
Preview 1-1
Course Objectives 1-2
What is Java? 1-3
What is a GUI Application? 1-4
A Brief Look at Object-Oriented Programming (OOP) 1-6
What is Needed to Learn Java? 1-7
Downloading and Installing Java 1-8
Downloading and Installing NetBeans 1-10
Testing the Installation 1-12
Getting Help with a Java Program 1-17
Structure of a Java Program 1-18
Structure of a Java GUI Application 1-19
Swing Controls 1-21
Creating a Java Project with NetBeans 1-24
Create a Frame 1-28
Saving Java Projects with NetBeans 1-30
NetBeans and Java Files 1-31
Create the User Interface 1-32
Example 1-1: Stopwatch Application – Adding Controls 1-36
Adding Event Methods 1-42
Variables 1-46
Java Data Types 1-48
Variable Declaration 1-49
Arrays 1-50
Constants 1-51
Variable Initialization 1-52
Example 1-2: Stopwatch Application – Writing Code 1-53
Class Review 1-61
Practice Problems 1 1-62
Problem 1-1. Beep Problem
Problem 1-2. Caption Problem
Problem 1-3. Enabled Problem
Problem 1-4. Date Problem
Exercise 1: Calendar/Time Display 1-63
Addenda: Java Programming without a Development Environment 1-64
2. The Java Language
Review and Preview 2-1
A Brief History of Java 2-2
Rules of Java Programming 2-3
Java Statements and Expressions 2-4
Type Casting 2-6
Java Arithmetic Operators 2-7
Comparison and Logical Operators 2-9
Concatenation Operators 2-11
Strings to Numbers to Strings 2-12
Java String Methods 2-14
Random Number Generator 2-19
Math Functions 2-20
Example 2-1: Savings Account 2-22
Focus Traversal 2-33
Example 2-2: Savings Accounts – Setting Focus 2-34
Improving a Java Application 2-35
Java Decisions – if Statements 2-36
Switch Statement – Another Way to Branch 2-40
Control Focus 2-43
Input Validation 2-45
Example 2-3: Savings Account – Input Validation 2-47
Java Looping 2-57
Java Counting 2-60
Example 2-4: Savings Account – Decisions 2-62
Class Review 2-79
Practice Problems 2 2-80
Problem 2-1. Random Number Problem
Problem 2-2. Price Problem
Problem 2-3. Odd Integers Problem
Problem 2-4. Pennies Problem
Problem 2-5. Code Problem
Exercise 2-1: Computing a Mean and Standard Deviation 2-81
Exercise 2-2: Flash Card Addition Problems 2-82
3. Java Swing Controls
Review and Preview 3-1
Function Overloading 3-2
Confirm Dialog (JOptionPane) 3-3
Font Object 3-8
Color Object 3-10
JFrame Object 3-13
Frame Layout and Centering 3-15
JButton Control 3-19
JLabel Control 3-21
JTextField Control 3-23
JTextArea Control 3-25
Example 3-1: Password Validation 3-27
JCheckBox Control 3-39
JRadioButton Control 3-41
JPanel Control 3-43
Handling Multiple Events in a Single Procedure 3-45
Control Arrays 3-47
Example 3-2: Pizza Order 3-49
JList Control 3-73
JScrollPane Control 3-77
JComboBox Control 3-80
Example 3-3: Flight Planner 3-83
Class Review 3-98
Practice Problems 3 3-99
Problem 3-1. Message Box Problem
Problem 3-2. Tray Problem
Problem 3-3. List Box Problem
Problem 3-4. Combo Box Problem
Exercise 3: Customer Database Input Screen 3-100
4. More Java Swing Controls
Review and Preview 4-1
JSpinner Control 4-2
Example 4-1: Date Input Device 4-6
JScrollBar Control 4-15
JSlider Control 4-19
Example 4-2: Temperature Conversion 4-22
JLabel Control (Revisited) 4-34
Example 4-3: “Find the Burger” Game 4-38
JFileChooser Control 4-48
Example 4-4: Image Viewer 4-54
Class Review 4-60
Practice Problems 4 4-61
Problem 4-1. Number Guess Problem
Problem 4-2. RGB Color Problem
Problem 4-3. Tic-Tac-Toe Problem
Problem 4-4. File Times Problem
Exercise 4: Student Database Input Screen 4-62
5. Java Application Design and Distribution
Review and Preview 5-1
Application Design Considerations 5-2
JTabbedPane Control 5-3
Example 5-1: Shopping Cart 5-6
Using General Methods in Applications 5-28
Example 5-2: Average Value 5-31
Returning Multiple Values from General Methods 5-47
Example 5-3: Circle Geometry 5-49
Adding Menus to Java Applications 5-62
Example 5-4: Note Editor 5-77
Distribution of a Java GUI Application 5-92
Executable (jar) Files 5-93
Creating a jar File in NetBeans 5-94
Creating a jar File in Command Prompt 5-101
Application Icons 5-103
Using IconEdit 5-105
Running a Project on Another Computer 5-109
Program Installers 5-113
Installing/Removing a Java Application 5-120
Class Review 5-126
Practice Problems 5 5-127
Problem 5-1 Tabbed Pane Problem
Problem 5-2 Note Editor About Box Problem
Problem 5-3 Normal Numbers Problem
Exercise 5: US/World Capitals Quiz 5-128
6. Exception Handling, Debugging and Sequential Files
Review and Preview 6-1
Program Errors 6-2
Exception Handling 6-3
Debugging Java Programs 6-7
Simple Debuggin 6-8
Example 6-1: Debugging Example 6-9
Using the Java Debugger 6-16
Using the Debugging Tools 6-18
Debugging Strategies 6-22
Sequential Files 6-23
Sequential File Output (Variables) 6-25
Example 6-2: Writing Variables to Sequential Files 6-29
Sequential File Input (Variables) 6-32
Example 6-3: Reading Variables from Sequential Files 6-35
Parsing Data Lines 6-37
Example 6-4. Parsing Data Lines 6-39
Reading Tokenized Lines 6-44
Example 6-5. Reading Tokenized Data Lines 6-45
Building Data Lines 6-47
Example 6-6: Building Data Lines 6-50
Configuration Files 6-63
Example 6-7: Configuration Files 6-65
Writing and Reading Text Using Sequential Files 6-71
JFileChooser Control (Save Files) 6-74
Example 6-8: Note Editor – Reading and Saving Text Files 6-79
Class Review 6-93
Practice Problems 6 6-94
Problem 6-1. Debugging Problem
Problem 6-2. Option Saving Problem
Problem 6-3. Text File Problem
Problem 6-4. Data File Problem
Exercise 6-1: Information Tracking 6-96
Exercise 6-2: ‘Recent Files’ Menu Option 6-97
7. Graphics Techniques with Java
Review and Preview 7-1
Simple Animation 7-2
Example 7-1: Simple Animation 7-4
Timer Object 7-10
Example 7-2: Timer Example 7-13
Basic Animation 7-17
Example 7-3: Basic Animation 7-19
Random Numbers (Revisited) and Games 7-29
Example 7-4: One-Buttoned Bandit 7-31
Randomly Sorting Integers 7-48
Example 7-5: Random Integers 7-50
Java2D Graphics 7-60
Graphics2D Object 7-61
Stroke and Paint Objects 7-62
Line2D Shape 7-65
Graphics Demonstration 7-67
Persistent Graphics 7-72
Example 7-6: Drawing Lines 7-78
Rectangle2D Shape 7-89
RoundRectangle2D Shape 7-92
Example 7-7: Drawing Rectangles 7-94
Ellipse2D Shape 7-108
Example 7-8: Drawing Ellipses 7-111
Arc2D Shape 7-119
Example 7-9: Drawing Pie Segments 7-122
Pie Charts 7-135
Line Charts and Bar Charts 7-139
Coordinate Conversions 7-143
Example 7-10: Line, Bar and Pie Charts 7-153
Class Review 7-169
Practice Problems 7 7-170
Problem 7-1. Dice Rolling Problem
Problem 7-2. Shape Guessing Problem
Problem 7-3. Pie Chart Problem
Problem 7-4. Plotting Problem
Exercise 7-1: Blackjack 7-171
Exercise 7-2: Information Tracking Plotting 7-172
8. More Graphics Techniques and Multimedia Effects
Review and Preview 8-1
Mouse Events 8-2
Example 8-1: Blackboard 8-7
Persistent Graphics, Revisited (Vector Class) 8-28
Example 8-2: Blackboard (Revisited) 8-32
More Graphics Methods 8-42
Point2D Object 8-43
GeneralPath Object 8-44
Drawing Polygons 8-46
Example 8-3: Drawing Polygons 8-49
Drawing Curves 8-63
Example 8-4: Drawing Curves 8-70
Example 8-5: Animated Curves 8-78
GradientPaint Object 8-85
Example 8-6: Gradient Paint 8-87
TexturePaint Object 8-101
Example 8-7: Texture Paint 8-107
drawString Method 8-113
Multimedia Effects 8-118
Animation with drawImage Method 8-119
Example 8-8: Bouncing Ball 8-121
Scrolling Backgrounds 8-132
Example 8-9: Horizontally Scrolling Background 8-135
Sprite Animation 8-145
Keyboard Methods 8-148
Example 8-10: Sprite Animation 8-151
Collision Detection 8-158
Example 8-11: Collision Detection 8-159
Sounds in Java 8-167
Example 8-12: Playing Sounds 8-160
Example 8-13: Bouncing Ball with Sound! 8-176
Class Review 8-181
Practice Problems 8 8-182
Problem 8-1. Blackboard Problem
Problem 8-2. Rubber Band Problem
Problem 8-3. Plot Labels Problem
Problem 8-4. Bouncing Balls Problem
Problem 8-5. Moon Problem
Exercise 8: The Original Video Game – Pong! 8-183
9. Other Java Topics
Review and Preview 9-1
Other Controls 9-2
JTextPane Control 9-3
Example 9-1: Note Editor (Revisited) 9-7
JToolBar Control 9-17
Example 9-2: Note Editor Toolbar 9-21
More Swing Controls 9-33
Even More Controls 9-38
Calendar Control 9-41
Example 9-3: Date Selection 9-45
Printing with Java 9-52
Printing Pages of a Document 9-55
Printing Text 9-57
Printing Lines and Rectangles 9-59
Printing Swing Components 9-61
pageDialog Method 9-64
printDialog Method 9-65
Example 9-4: Printing 9-67
Creating a Help System 9-83
Creating Topic Files 9-86
Creating a Map File 9-89
Creating a Table of Contents File 9-90
Creating a Help Set File 9-91
Displaying the Help System 9-92
Example 9-5: Help System Display 9-93
Class Review 9-97
Course Summary 9-98
Practice Problems 9 9-99
Problem 9-1. Biorhythm Problem
Problem 9-2. Loan Printing Problem
Problem 9-3. Plot Printing Problem
Problem 9-4. Note Editor Help Problem
Exercise 9-1: Phone Directory 9-101
Exercise 9-2: The Ultimate Application 9-102
Appendix I. General Purpose Methods and Classes
average AI-2
BarChartPanel AI-2
blankLine AI-5
circleGeometry AI-5
degFTodegC AI-5
GraphicsPanel AI-6
LineChartPanel AI-6
loanPayment AI-9
midLine AI-10
PieChartPanel AI-10
PrintUtilities AI-12
randomNormalNumber AI-13
rectangleInfo AI-14
sortIntegers AI-15
soundEx AI-16
standardDeviation AI-17
validateDecimalNumber AI-18
validateIntegerNumber AI-20
xPhysicalToxUser AI-21
yPhysicalToyUser AI-21
Appendix II. Brief Primer on Classes and Objects
Introduction AII-1
Objects in Java AII-3
Adding a Class to a Java Project AII-4
Declaring and Constructing an Object AII-6
Adding Properties to a Class AII-7
Another Way to Add Properties to a Class AII-9
Validating Class Properties AII-13
Adding Constructors to a Class AII-16
Adding Methods to a Class AII-18
Inheritance AII-23
Example II-1. Savings Account AII-28
Inheriting from Java Controls AII-45
Building a Custom Control AII-46
Adding New Properties to a Control AII-49
Adding Control Methods AII-51
Example II-2. Savings Account (Revisited) AII-58
Class Review AII-64
Practice Problems Solutions (Part 1: Classes 1 to 5)
Practice Problems Solutions (Part 2: Classes 6 to 9)
Exercise Solutions (Part 1: Classes 1 to 5)
Exercise Solutions (Part 2: Classes 6 to 9)