|
Sample Code
|
Sample Code
- House.java
- BankAccount.java
- RainGauge.java
- Animation Code
- The framework classes, used by all the examples: Stage.java, Actor.java,
Prop.java, ShapeProp.java
[You don't need to copy these, as they're part of the uwcse.jar
file. They're here only so you can look at their source code
easily.]
- The jiggling landscape example: JigglingLandscape.java,
JigglingLandscapeEventHandler.java,
JigglingHouse.java, House.java,
Tree.java, RisingSun.java
[Try JigglingLandscape.test()]
- The asteroid field example: Asteroids.java,
AsteroidsEventHandler.java,
Asteroid.java
[Try Asteroids.test()]
- Asteroids.jar is a version of
Asteroids that's been turned into an "executable .jar
file", so that the Asteroids simulation can be run directly by
a) double-clicking on it (in Windows) or b) running it from a
command prompt by typing "java -jar
Asteroids.jar".
- Here is a web page that runs the
Asteroids application inside it, assuming you're running with a web
browser that has the JDK1.3.1 plug-in installed in it. (You
can also run applets using the appletviewer program; download (don't
just click!) the Asteroids.html web
page and the Asteroids.jar file, go to a
command prompt and type "appletviewer
Asteroids.html" to test it out.)
- The bouncing balls example: BouncingBalls.java,
BouncingBallsEventHandler.java,
BouncingBall.java
[Try BouncingBalls.test()]
- The checkerboard example: JigglingCheckerboard.java,
JigglingCheckerboardEventHandler.java,
JigglingSquare.java, Checker.java
[Try JigglingCheckerboard.test()]
Building Your Own Executable Jar File
(I.e., Java's version of a stand-alone application program)
Asteroids.jar is a version of Asteroids that's
been turned into an "executable .jar file", so that the Asteroids
simulation can be run directly by a) double-clicking on it (in Windows) or b)
running it from a command prompt by typing "java
-jar Asteroids.jar".
To create the executable .jar file, I followed the following procedure:
- I added a "public static void main(String[]
args)" method to Asteroids.java (the main class), which is what
gets invoked when the application is executed. In this case, I just
had it invoke the existing static test
method.
- I copied the uwcse.jar file to Asteroids.jar.
- I compiled all the .java files above to .class files, e.g. using BlueJ.
- I added the .class files to the Asteroids.jar file by typing at a command
prompt "jar uf Asteroids.jar *.class".
- I wrote the following Asteroids.mf
"manifest" text file, which identifies the main class in the
application. For some other application, I would write a slightly
different .mf file to identify the main class of that application.
- I added Asteroids.mf to the .jar file by typing "jar
ufm Asteroids.jar Asteroids.mf".
Check out the Sun
JDK documentation on the jar tool for more information. Also look at this
page for information on manifest text files.
Building Your Own Applet
(I.e., making your Java application run inside a web browser)
To make a web-based interface to running the Asteroids application, I
followed the following procedure:
- I wrote AsteroidsApplet.java wrapper
class. Look over the comments in that code to see a recipe for making
your own applet wrapper class that works well with our graphics library
classes.
- I built the Asteroids.jar file as described above, but including
AsteroidsApplet.class.
- I wrote the Asteroids.html web page,
including an APPLET HTML tag. Look over the web page to see how to
write your own APPLET HTML tags.
- I tested this by typing "appletviewer
Asteroids.html" at a command prompt (assuming Asteroids.html and
Asteroids.jar are both in the current directory).