Java Reference
In-Depth Information
Exercises
11-1. Example 11-1 contains a centerText() method that centers one or two lines
of text within a rectangle. Write a modified version of this method that posi-
tions a single line of text according to two new method parameters. One
parameter should specify the horizontal positioning: left-, center-, or right-
justified. The other parameter should specify the vertical position: at the top
of the rectangle, centered, or at the bottom of the rectangle. You can use the
FontMetrics class, as Example 11-1 does, or the Java 2D getStringBounds()
method of the Font class. Write a GraphicsExample class that demonstrates
all nine possible positioning types supported by your method.
11-2. Use the animation techniques demonstrated in this chapter to write an
applet that scrolls a textual message across the screen. The text to scroll and
the scrolling speed should be read from applet parameters specified with
<PARAM> tags in an HTML file.
11-3. Experiment with the graphics capabilities of the Graphics and Graphics2D
classes and write an applet or application that displays some kind of inter-
esting and dynamic graphics. You may want to take your inspiration from
one of the many screensaver programs on the market. For example, you
might draw filled rectangles on the screen, using random sizes, positions,
and colors. (See Math.random() and java.util.Random for ways to generate
random numbers.) Feel free to use any of the custom Shape , Stroke ,or
Paint classes developed in this chapter. Be creative!
11-4. One of the things that makes graphics so powerful is that it gives us the
ability to visualize patterns. Look back at Example 1-15. This program com-
putes prime numbers with the “Sieve of Eratosthenes” algorithm, whereby
prime numbers are located by ruling out multiples of all previous prime
numbers. As part of this algorithm, you maintain an array that specifies
whether a number is prime or not. Write a graphical version of the Sieve
program. It should display the array as a 2D matrix; cells in the matrix that
represent prime numbers should be displayed in one color; non-primes
should be displayed in another color. Write your program as a standalone
application or as a GraphicsExample implementation, if you prefer.
Now extend your program again. When computing primes, don't simply
storea boolean value to indicate whether a number is prime or not. Instead,
storean int value that specifies, for non-prime numbers, the prime number
that was used to rule this one out. After computing a set of primes, display
the contents of your array as a rectangular grid, using a different color to
indicate the multiples of each prime. The patterns that emerge look best
when viewed in a square grid with a prime number as its height and width.
For example, if you compute all the primes up to 361, you can graphically
display your results in a square grid with 19 cells per side. Study the pat-
terns you see. Does it give you insight into the workings of the algorithm?
Does it help you understand why it is called a “sieve”?
Search WWH ::




Custom Search