4: GUI Building in NetBeans
Spring 2008, David Matuszek
Create a GUI in NetBeans that can be used to control an animation.
Define an animation (at least "bouncing ball", but feel free to do something more interesting.)
Package your program as an executable
.jar file, including your source files.
First, use NetBeans visual GUI editor to create the GUI for your project.
Start by creating a new project, but instead of choosing
Java Desktop Application. Name your project
and name your main class
animation.Animation. This will start
you in the GUI builder. Use it to create an interface that looks like this:
|(Click to view larger image)|
Start by clicking in the center of the large area to select it. Then right-click
it and set the layout to
BorderLayout. Drag a JPanel to the SOUTH
area, and using the information area on the right, find the
this JPanel and change it to
controlPanel. Drag another JPanel
to the CENTER area, and name it
displayPanel. Drag the appropriate
components to the control panel and, for each component, set its
the text you want on the component, and give it a meaningful
name (for example,
Hint: When you get into a mess that you can't seem to get out of, select the project and delete it. Be sure to also delete the source files. Then you can start over. Using the GUI editor is tricky, so don't be too upset if it takes you several tries to get it right.
To attach a Listener to a button: Select and right-click the button,
Create New Action...,
Action's Method:, type in the name of a method. This
will bring you to the source code where the new method has been created.
Here's what the buttons are supposed to do:
New-- Create a new animation, with new values. For example, with the bouncing ball animation, you might have a ball with a new size, color, and velocity. Don't start the animation running.
Step-- Make one single step of the animation; that is, move from one still picture to the next.
Run-- Run the animation.
Pause-- Pause the animation. When the
Runbutton is clicked, the animation will resume from where it left off. (In the picture this button is named "Stop", but I later decided "Pause" was a better name.)
Reset-- Do not create a new animation, but go back to the beginning of this one. Don't start the animation running.
The slider controls the speed of the animation, from very slow or stopped (at the left end) to very fast (at the right end). Do this by controlling the time between successive frames. Do not exceed 40 frames/second.
About... menu items. The
About... menu item should
include your name as author.
Provide an animation, using the Model-View-Controller and Observer design patterns, and controlled by a Timer. You will probably want to review my Animation slides from CIT591, and you may want to download the BouncingBall.zip example code.
Do any animation you like. I recommend that you start with the basic bouncing ball animation, which is sort of the "Hello World" of animation programs. Once you get that running, you can either stop there, or replace it with something more interesting; at that point, all the basic structure will be in place.
Jar files are like
zip files. In fact,
jar files are
zip files. The
difference is that a
jar file can be executed
by double-clicking on it--if (1) you have Java properly installed
on your machine (try entering
java -version in a DOS window),
and (2) if you have set up the
jar file properly.
In Eclipse you would
Export your program as a
and follow the prompts. In Netbeans there is already a
but you have to fiddle with it. In NetBeans, choose
Help > Help Contents
> Java Applications > Building Java Applications >
a jar file.
Hint: After making the
jar file, it's a good idea to make
a copy of it, rename
the copy to have the
.zip extension instead of the
and unzip it to be sure everything is there that's supposed to be there.
If you are missing anything (especially source files), or if we can't run
jar file, you will lose points.
NetBeans comes with a visual GUI editor, and Eclipse doesn't. However, you can download one, as a plugin, from http://www.eclipse.org/vep/WebContent/main.php. Using it is very similar to using the one in NetBeans; I don't know which one is less buggy.
jar containing everything in the complete NetBeans project, including
all your source files, and submit via Blackboard
before midnight Thursday, February 14.