594 Assignment 4: GUI Observations
Spring 2008, David Matuszek
I'm also working on this assignment, in order to increase my familiarity with NetBeans. Here are some things I've found (or think I've found) that may also help you with the assignment.
Save your work often. The NetBeans visual GUI builder is very buggy. Also, it uses some classes that are only available in Java 6.
Try to get the GUI right before you add code to it. To be extra safe, keep all your
added code together in one place, and make a copy of it somewhere before you make
any changes in the GUI. (I put all my additional instance variables just above
The MVC design pattern says that you should keep Model, View, and Controller
as separate as possible. The NetBeans GUI generator puts your
@Actions methods into the View class. This is okay--keep
all your added code together, and your View and Controller will still be logically separate,
even if they are in the same class. The Model, as usual, should still be
in its own class.
Notice the Source and Design tabs. These allow you to switch back and forth between the code view and the GUI view of your application.
When you are in the Design view, there is an Inspector pane on the left side. Double-click on any JPanel in the Inspector to view just that JPanel. Double-click on the main panel to get back to seeing the entire GUI.
In the Properties pane (on the right side) there are Properties and there are Other Properties. Presumably the idea is that the most important properties are in the first batch; if so, I don't agree.
backgroundcolor (probably white) for your animation.
text. NetBeans seems to lose this text any time you do anything else to the button, so you may have to re-enter the text.
jPanel2. Changing the
namefield in the Properties pane should do this, but that doesn't seem to work. However, right-clicking in the Inspector pane and choosing Change Variable Name... does seem to work.
action. Click the
[...]to the right of
actionand, using the Default editor, choose Create New Action..., then enter the name of the method to be called when the button is clicked.
NetBeans generates a lot of code for you--some of it pretty ugly, in my opinion.
@Actionmethods you added.
getGraphics()method. Once you have a
Graphicsobject, you can draw on it.
Animation slides from CIT591,
I use a
java.util.Timer. NetBeans uses a
It is illegal to try to import both into the same class, because then Java
doesn't know which
Timer you mean. There are two possible solutions:
java.util.Timer. Then when NetBeans refers to
Timer, it gets the Swing version. When you need the older utilities version, refer to it using its full name,
java.util.Timer. This turns out to be a little easier--instead of an inner
TimerTaskclass, you just need an
I found that, with the
Timer, I didn't have any need for
Observable. Since I didn't use these, you don't
need to, either.
There are two ways to change the speed of the animation. The simpler way is to reduce the frame rate--the longer the time interval between successive frames, the slower the animation. This has the disadvantage that the animation is very jerky at slow speeds.
The better way is to keep the frame rate high, and to change the amount that the ball
(or other animation) moves between frames. If you do this, it will work much better if
you keep your x,y position and your deltaX,deltaY velocity as
rather than as
ints, and convert the position to
the final step.
When your program works, and you create an executable
.jar from it,
don't assume that the jar
file works! Test it! If it doesn't
run, one possible reason is that NetBeans is using some nonstandard classes that
you haven't included in your jar file--most likely a layout manager. (Another
possibility is that your Java installation isn't completely correct, in which case
the jar file might or might not be fine.)