Table Of ContentCopyright
Copyright © 2014, 2015 Annette Godtland SECOND EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval
system, or transmitted by any means, electronic, mechanical, photocopying,
recording, or otherwise, without written permission from the author.
Many of the designations used by manufacturers and sellers to distinguish their
products are claimed as trademarks. Where those designations appear in the
book, and the author was aware of a trademark claim, the designations have been
printed in initial caps or all caps.
No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation of
this book, the publisher and author assume no responsibility for errors or
omissions. Neither is any liability assumed for damages resulting from the use of
the information contained herein.
You can use and redistribute example code from this book only for non-
commercial purpose and as long as you acknowledge its source and authorship.
The source of the code should be noted in any documentation as well as in the
program code itself (as a comment). The attribution should include author:
Annette Godtland, title: More Do-It-Yourself Java Games, and publisher:
Godtland Software Corporation.
First Published on 08/28/2014
Last Updated 11/14/2015
Published by:
Godtland Software Corporation, Publishing Division Rochester, MN 55901
More Do-It-Yourself Java Games
An Introduction to Java Graphics and Event-Driven
Programming
by Annette Godtland
Godtland Software Corporation, Publishing Division
Rochester, Minnesota
TABLE OF CONTENTS
Copyright
Introduction
The Programs
Do You Know Basic Java?
Setup
Download MoreDIYJava.zip
Project 1 - Wizard of Yes/No
Lesson 1.1 - A Standard Window
Lesson 1.2 - Add Text, Window Size
Lesson 1.3 - Font
Lesson 1.4 - Align Text
Lesson 1.5 - Pack a Window
Lesson 1.6 - Disable Resizing
Lesson 1.7 - Window Title, Center
Lesson 1.8 - Serial Version UID
Lesson 1.9 - Event Queue
Lesson 1.10 - Cross Platform Look and Feel
Project 2 - Guess My Color
Lesson 2.1 - Border Layout
Lesson 2.2 - Panels, Predefined Colors
Lesson 2.3 - Multiple Panels, Preferred Size, Dimension
Lesson 2.4 - Foreground Color
Lesson 2.5 - Opacity
Lesson 2.6 - Custom Colors
Lesson 2.7 - Buttons
Lesson 2.8 - Message Dialogs
Lesson 2.9 - A Custom Label
Project 3 - Wizard of Yes/No Disclaimer
Lesson 3.1 - Text Areas
Lesson 3.2 - Scroll Bars
Project 4 - Framed
Lesson 4.1 - A Custom Button, this
Lesson 4.2 - Grid Layout
Lesson 4.3 - Event Source
Lesson 4.4 - Confirm Dialogs
Project 5 - Watch Your Step
Lesson 5.1 - Another Custom Button
Lesson 5.2 - In Focus Mark
Project 6 - Sliding Tiles
Lesson 6.1 - Buttons with Images
Lesson 6.2 - Read Image Files, Subimages, Image Icons
Lesson 6.3 - Add and Remove Components
Project 7 - Maze Generator and Anti-Maze
Lesson 7.1 - A Painted Panel: Color, Rectangles, Lines, Circles, and
Preferred Size
Lesson 7.2 - Key Listeners
Lesson 7.3 - Button Focus
Project 8 - Sliding Tiles Menu Bar
Lesson 8.1 - Menu Bars
Lesson 8.2 - File Choosers
Lesson 8.3 - An Image File Filter
Lesson 8.4 - New Graphics, Scale an Image
Lesson 8.5 - System Look and Feel
Project 9 - Greedy
Lesson 9.1 - Mouse Listeners
Lesson 9.2 - Box Layout, Combined Layouts
Project 10 - Maze Generator Options Dialog
Lesson 10.1 - A Custom Dialog
Lesson 10.2 - Text Fields, Radio Buttons
Lesson 10.3 - Get Dialog Data
Lesson 10.4 - Modal and Non-Modal Dialogs
Lesson 10.5 - Cancel Dialog Changes
Lesson 10.6 - A Default Button
Project 11 - Word Builder
Lesson 11.1 - Dictionary, Compare Strings
Lesson 11.2 - Draw Strings, Font Metrics
Lesson 11.3 - Space Between Components
Lesson 11.4 - Current Date, Date Format
Lesson 11.5 - Component Listeners, Window State Listeners, Resize a
Window
Lesson 11.6 - Window Size and Position
Lesson 11.7 - Put Data Files in the JAR File
Project 12 - Image Resizer
Lesson 12.1 - Toolbars
Lesson 12.2 - Save an Image
Lesson 12.3 - Focus Listeners
Lesson 12.4 - XOR Mode
Lesson 12.5 - Draw with a Mouse, Mouse Motion Listeners
Project 13 - Baker's Dozen Solitaire
Lesson 13.1 - Transparent Colors, PNG Images
Lesson 13.2 - Objects on a Panel
Lesson 13.3 - Move Objects
I Hope You Enjoyed this Book
Questions, Comments, or Suggestions
Other Books I've Written
Appendix A - Glossary
Appendix B - Completed Listings
Appendix C - Setup
Create a Java Work Folder
Download and Install Java
Download and Install Eclipse
Download MoreDIYJava.zip Detailed Instructions
Appendix D - Using Eclipse
Create a First Project
Create a First Package
Create A First Program
Add a Project to the Build Path
Export a Program
Appendix E - Conventions Used and Other Tips
Introduction
This is the second book in the Do-It-Yourself Java Games series. The first book's
programs were text-based so you could develop basic Java programming skills.
With that background, you should be ready for the next level of programming:
Java graphics and event-driven programming. Java graphics add color and
images to games. But what is event-driven programming?
An event-driven program creates an initial screen, then waits for the program
user to take an action. The user may click a button, click on an image, drag
something, or press a key on the keyboard. The program then responds to that
action; whatever that action may be. Writing such a program is called event-
driven programming.
The event-driven programs in this book will teach you how to respond to user
input from:
Windows and dialogs
Buttons and input fields
Images and drawings
Keyboard input
Mouse clicks and drags
As in the previous book in this series, the lessons in each project are presented
using a “try-it” approach: do the steps shown and see what happens. You'll be
given programming tasks, each followed by a program code listing. The listings
have fill-in blanks to allow you to write much of the program code yourself. But
don't worry if you're unsure of how to fill in the blanks; simply click the
completed listing's link to see the missing code.
Don't skip any lessons. Each lesson explains a new programming technique,
which may be used again in later lessons. Doing all the lessons will make later
lessons easier.
Appendix A - Glossary has definitions of Java programming terms and links to
more information. The first use of each term, like “graphics” and “event-driven
programming” used above, will include a clickable link to the word in the
glossary.
Several different approaches can often accomplish the same programming task.
You don't have to use the code shown in the answer if your code does what was
asked or if you prefer your code's results. Feel free to experiment with the code
and see what happens. Half the fun of programming is making programs do what
you want them to do.
The Do-It-Yourself Java Games books are designed for e-readers. Click the
image below or follow this link to watch a video showing the features in these
electronic books that would not be possible in a print book.
More Do-It-Yourself Java Games was updated to a second edition to work in
Windows 10 and Java 8, and to make sure the games will run as designed across
multiple platforms, including Windows, OS X, and Linux.
The Programs
You'll create ten computer programs that use graphics and event-driven
programming:
Wizard of Yes/No
Learn how to create a window, add text, change fonts, and add scroll bars.
The Wizard of Yes/No can help with making yes or no decisions.
Guess My Color
Description:More Do-It-Yourself Java Games: An Introduction to Java Graphics and Event-Driven Programming is the second book of the Do-It-Yourself Java Games series. In event-driven programming, the program lays out all the game pieces then waits. The user then takes an action and the program responds to that a