A Little Object-Orientation and Some Graphics
Prime Number Program
Let's look at this program that prints
out the first 100 prime numbers. If your browser asks you what program
to view this Java source file with, choose a text editor. To get this program
into your account so you can compile it, type the following at the command line:
% wget http://www.cs.utsa.edu/~dj/cs1713-1/primes/OldPrintPrimes.java
The little percent sign above represents the command prompt; don't type it.
This program is somewhat painful because it contains a nested loop and
the logic is a little complicated so it's hard to follow. We can clean
it up by separating out the test for primality into a separate method; see
this slightly less painful version.
Now the part that goes through numbers printing them out is separated
from the part that tests for primality, so the logic is easier to follow.
Code Reuse
The program is still somewhat painful because it's hard to reuse the code
elsewhere. If I want to, say, write a program that counts the number of
primes up to a certain number, I have to do one of the following things:
- Write the new program from scratch.
- Copy-and-paste the isPrime method into the new program.
- Make isPrime public and static, and then awkwardly refer to PrintPrimes.isPrime() from the new program.
None of these solutions is satisfactory. The first two don't allow
improvements in the original isPrime() method to be easily
incorporated into the new program. The third one is awkward; we have to
refer to a static method in a program that was originally intended to print
out prime numbers. Also, note that testing for primality is not really
what we want, either; we really want a way of generating prime numbers.
From CS 1063 you might remember that you don't necessarily have to test
for primality to get a list of prime numbers.
Object-oriented Version
Let's look at a version of the program that takes advantage of Java's object-oriented features.
There are two files now:
- PrintPrimes.java has code that just counts up numbers from a list of primes.
- Primes.java is a more general-purpose primality class that includes code to generate a list of primes (similar to the way Scanner or Random work).
Primes.java includes isPrime as a public static method
so we can still use it as before (prepended with Primes), while
the nextPrime class gets us what we are really after: a way of
generating a list of prime numbers.
Counting Prime Numbers
With this new class, we easily can write another program that gets a number from
the user and counts the number of primes up to that number. Note that
this program features rudimentary input checking with exceptions.
Graphics
Let's look at some programs that use graphics. We'll use Java's
JApplet and Graphics classes to draw some simple graphics.
Note that these programs will not have a public static void main
because they are applets rather than stand-alone programs.
These programs will have just a paint method that draws graphics
when the program starts or some other event takes place, e.g. a window
resize event.
JApplet is a class we extend to make our applet.
That is, we write a class that is a subclass of JApplet.
We override the paint method of JApplet to draw graphics
in a window. The paint method is passed a parameter of class
Graphics that has methods like drawRect to draw a rectangle
and setColor to set the current color we want to draw with.
An applet can be run from a web browser, or using the appletviewer
application provided in Sun's, I mean Oracle's JDK. Applets must
be placed in an HTML wrapper so they can be viewed with a browser or
appletviewer. More about this fun topic can be found in Chapter
2.8 of your text.
Drawing a Grid
Here's a first graphics example
(ex1) that draws a grid on the screen with squares that are
10×10 pixels. To run it, we'll have to make an HTML file that refers
to the .class file. Let's not try to learn too much about HTML,
just the bare minumum to get this to work:
<applet code="ex1.class" width="400" height="400">
</applet>
We can put that stuff into a file called ex1.html (or anything
else .html if you like), then compile ex1.java into
ex1.class, then run the appletviewer on the whole thing:
% wget http://www.cs.utsa.edu/~dj/cs1713-1/graphics/ex1.java
% javac ex1.java
% appletviewer ex1.html
The little percent signs above represent the command prompt; don't type
them. Note that you have to make ex1.html with a text editor or
by cutting and pasting.
Drawing a 20×20 Grid
Note that when you resize the window for the ex1 program, it
draws boxes of the same size so that a bigger window gets more boxes
and a smaller window gets fewer boxes. Maybe we want a window where the
number of boxes stays the same no matter how the user resizes the window.
We want a 20×20 grid. Here's a little
program (ex2) that does that.
Let's compile that program and make an HTML file called ex2.html that contains
the right incantation to invoke the ex2 program.
Drawing a Weird, Squishy 20×20 Grid
Now let's change the program a little so it draws
a fun squishy grid (ex3). My teacher showed me how to draw
these on paper in the third grade back when we still had paper. You'll see
what I mean when you run it.
But wait, making these .html files is becoming tedious. Let's write
a Java program that does that for us
by accepting a command-line parameter with the name of the program and prints
the HTML code on the standard output which we can redirect into an HTML file.
Print Some Text In A Box
We're having fun here, so let's augment our program to draw "CS 1713
is fun" in the center of the screen, with a box around the text. To do this we'll have to know how to measure
text represented as graphics, which is not too hard in Java.
Random Colors on a Grid
Let's go back to our non-squishy grid and draw
some randomly colored squares on the screen.