Sei sulla pagina 1di 14

i

Course Notes for:

Learn Java
(GUI Applications)

Lou Tylee, 2004


KIDware

PO Box 28234
Seattle, WA 98118
(206) 721-2556
FAX (508) 464-0455
E-Mail: support@kidwaresoftware.com
Web Site: www.kidwaresoftware.com

KIDware (206) 721-2556

ii

Learn Java (GUI Applications)

Notice
These notes were developed for the course, Learn
Java (GUI Applications). They are not intended to be
a complete reference to Java. Consult other texts and
various websites for more detailed information.
The notes refer to several software and hardware
products by their trade names. These references are
for informational purposes only and all trademarks are
the property of their respective companies.
Lou Tylee
Seattle, Washington
January 2004

KIDware (206) 721-2556

Contents

iii

Learn Java

(GUI Applications)

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 JCreator........................................................1-10
Testing the Installation..............................................................................1-12
Getting Help with a Java Program............................................................1-18
Structure of a Java Program.....................................................................1-19
Structure of a Java GUI Application..........................................................1-20
Swing Controls..........................................................................................1-22
Creating a Java Project with JCreator......................................................1-25
Create a Frame.........................................................................................1-31
Saving Java Projects with JCreator..........................................................1-34
JCreator and Java Files............................................................................1-35
Create the User Interface..........................................................................1-36
Example 1-1: Stopwatch Application - Adding Controls...........................1-40
Adding Event Methods..............................................................................1-46
Variables....................................................................................................1-50
Java Data Types.......................................................................................1-51
Variable Declaration..................................................................................1-53
Arrays........................................................................................................1-54
Constants..................................................................................................1-55
Variable Initialization.................................................................................1-56
Example 1-2: Stopwatch Application - Writing Code................................1-57

KIDware (206) 721-2556

iv
1.

Learn Java (GUI Applications)


Introduction to Java (continued)
Class Review.............................................................................................1-65
Practice Problems 1..................................................................................1-66
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-67
Addenda: Java Programming without a Development Environment........1-68

KIDware (206) 721-2556

Contents
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

KIDware (206) 721-2556

vi
3.

Learn Java (GUI Applications)


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-17
JButton Control..........................................................................................3-21
JLabel Control...........................................................................................3-23
JTextField Control.....................................................................................3-26
JTextArea Control.....................................................................................3-29
Example 3-1: Password Validation...........................................................3-31
JCheckBox Control....................................................................................3-43
JRadioButton Control................................................................................3-45
JPanel Control...........................................................................................3-48
Handling Multiple Events in a Single Procedure......................................3-50
Control Arrays...........................................................................................3-52
Example 3-2: Pizza Order.........................................................................3-54
JList Control..............................................................................................3-78
JScrollPane Control..................................................................................3-82
JComboBox Control..................................................................................3-85
Example 3-3: Flight Planner......................................................................3-89
Class Review...........................................................................................3-104
Practice Problems 3................................................................................3-105
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-106

KIDware (206) 721-2556

Contents
4.

vii

More Java Swing Controls


Review and Preview....................................................................................4-1
JSpinner Control..........................................................................................4-2
Example 4-1: Date Input Device.................................................................4-7
JScrollBar Control.....................................................................................4-16
JSlider Control...........................................................................................4-20
Example 4-2: Temperature Conversion....................................................4-23
JLabel Control (Revisited).........................................................................4-35
Example 4-3: Find the Burger Game......................................................4-39
JFileChooser Control (Open Files)...........................................................4-49
Example 4-4: Image Viewer......................................................................4-57
Class Review.............................................................................................4-63
Practice Problems 4..................................................................................4-64
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-65

KIDware (206) 721-2556

viii
5.

Learn Java (GUI Applications)


Java GUI 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
Class Review.............................................................................................5-95
Practice Problems 5..................................................................................5-96
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-97

KIDware (206) 721-2556

Contents
6.

ix

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 Debugging.......................................................................................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

KIDware (206) 721-2556

x
7.

Learn Java (GUI Applications)


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

KIDware (206) 721-2556

Contents
8.

xi

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
Keyboard Methods..................................................................................8-145
Collision Detection..................................................................................8-148
Example 8-10: Collision Detection..........................................................8-149
Sounds in Java........................................................................................8-157
Example 8-11: Playing Sounds...............................................................8-160
Example 8-12: Bouncing Ball with Sound!..............................................8-166
Class Review...........................................................................................8-171
Practice Problems 8................................................................................8-172
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-173

KIDware (206) 721-2556

xii
9.

Learn Java (GUI Applications)


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

KIDware (206) 721-2556

Contents

xiii

Practice Problems Solutions (Part 1: Classes 1 to 5)......................P1-1


Practice Problems Solutions (Part 2: Classes 6 to 9)......................P2-1
Exercise Solutions (Part 1: Classes 1 to 5)...........................................E1-1
Exercise Solutions (Part 2: Classes 6 to 9)...........................................E2-1
Appendix I. General Purpose Methods and Classes......................AI-1
Appendix II. Brief Primer on Classes and Objects.........................AII-1

KIDware (206) 721-2556

xiv

Learn Java (GUI Applications)

This page intentionally not left blank.

KIDware (206) 721-2556

Potrebbero piacerti anche