[ Team LiB ] Previous Section Next Section

12.17 Exercises

Exercise 12-1. Example 12-1 contains a centerText( ) method that centers one or two lines of text within a rectangle. Write a modified version of this method that positions 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 12-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.

Exercise 12-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.

Exercise 12-3. Experiment with the graphics capabilities of the Graphics and Graphics2D classes, and write an applet or application that displays some kind of interesting 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!

Exercise 12-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 computes 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, and nonprimes 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 store a boolean value to indicate whether a number is prime or not. Instead, store an int value that specifies, for nonprime 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 patterns you see. Does it give you insight into the workings of the algorithm? Does it help you understand why it is called a "sieve"?

Exercise 12-5. Example 12-10 demonstrates how font glyphs can be used as Shape objects and shows how those Shape objects can be transformed with an arbitrary AffineTransform object. Write a custom Stroke object that is initialized with a Font and a string of text. When this Stroke class is used to draw a shape, it should display the specified text, using the specified font, along the outline of the specified shape. The text should hug the outline of the shape as closely as possible, which means that each font glyph has to be rotated, positioned, and drawn independently of the other glyphs. Use the two-argument version of Shape.getPathIterator( ) to obtain a FlatteningPathIterator that approximates the shape using straight line segments. You'll need to use geometry to compute the slope of each line segment and trigonometry to convert the slope to a rotation angle for each font glyph (you may find Math.atan2( ) helpful). Write a GraphicsExample class that displays some text art that demonstrates your new Stroke object.

    [ Team LiB ] Previous Section Next Section