15.5. Labels

 
[Page 458]

Programming Exercises

Sections 13.2 “13.9

13.1* ( Displaying a 3 x 3 grid ) Write a program that displays a 3 x 3 grid, as shown in Figure 13.27(a). Use red color for vertical lines and blue color for horizontal lines.
Figure 13.27. (a) Exercise 13.1 displays a grid. (b) Exercise 13.2 displays two objects of OvalButton . (c) Exercise 13.3 displays a checkerboard.

13.2** ( Creating a custom button class ) Develop a custom button class named OvalButton that extends JButton and displays the button text inside an oval. Figure 13.27(b) shows two buttons created using the OvalButton class.
13.3* ( Displaying a checkerboard ) Write a program that displays a checkerboard, as shown in Figure 13.27(c).
13.4* ( Displaying a multiplication table ) Write a program that displays a multiplication table in a panel using the drawing methods , as shown in Figure 13.28(a).
Figure 13.28. (a) Exercise 13.4 displays a multiplication table. (b) Exercise 13.5 displays numbers in a triangle formation.


13.5** ( Displaying numbers in a triangular pattern ) Write a program that displays numbers in a triangular pattern, as shown in Figure 13.28(b). The number of lines in the display changes to fit the window as the window resizes.
13.6** ( Improving FigurePanel ) The FigurePanel class in Listing 13.5 can display lines, rectangles, round-cornered rectangles, and ovals. Add appropriate new code in the class to display arcs and polygons. Write a test program to display the shapes as shown in Figure 13.29(a), using the new FigurePanel class.
[Page 459]
Figure 13.29. (a) Four panels of geometric figures are displayed in a frame of GridLayout . (b) TicTacToe cells display X, O, or nothing randomly . (c) Exercise 13.8 draws an octagon.

13.7** ( Displaying a TicTacToe board ) Create a custom panel that displays X, O, or nothing. What to display is randomly decided whenever a panel is repainted. Use the Math.random() method to generate an integer , 1 , or 2 , which corresponds to displaying X, O, or nothing. Create a frame that contains nine custom panels, as shown in Figure 13.29(b).
13.8** ( Drawing an octagon ) Write a program that draws an octagon, as shown in Figure 13.29(c).
13.9* ( Creating four fans ) Write a program that places four fans in a frame of GridLayout with two rows and two columns , as shown in Figure 13.30(a).
Figure 13.30. (a) Exercise 13.9 draws four fans. (b) Exercise 13.10 draws a cylinder. (c) Exercise 13.11 draws a diagram for function f ( x ) = x 2 .

13.10* ( Creating a cylinder ) Write a program that draws a cylinder, as shown in Figure 13.30(b).
13.11** ( Plotting the square function ) Write a program that draws a diagram for the function f ( x ) = x 2 (see Figure 13.30(c)).

Hint

Add points to a polygon p using the following loop:

   double   scaleFactor =   0.1   ;   for   (   int   x =   -100   ; x <=   100   ; x++) { p.addPoint(x +   200   ,   200   - (   int   )(scaleFactor * x * x)); } 


[Page 460]

Connect the points using g.drawPolyline(p.xpoints, p.ypoints, p.npoints) for a Graphics object g . p.xpoints returns an array of x coordinates, p.ypoints returns an array of y coordinates, and p.npoints returns the number of points in Polygon object p .


13.12** ( Plotting the sine function ) Write a program that draws a diagram for the sine function, as shown in Figure 13.31(a).
Figure 13.31. (a) Exercise 13.12 draws a diagram for function f ( x ) = sin ( x . (b) Exercise 13.13 draws the sine and cosine functions.

Hint

The Unicode for p is \u03c0. To display “2p use g.drawString("-2\u03c0", x, y) . For a trigonometric function like sin(x) , x is in radians. Use the following loop to add the points to a polygon p :

   for   (   int   x =   -100   ; x <=   100   ; x++) { p.addPoint(x +   200   ,   100   - (   int   )(   50   * Math.sin((x /   100.0   ) *   2   * Math.PI))); } 

“2p is at ( 100 , 100 ), the center of the axis is at ( 200 , 100 ), and 2p is at ( 300 , 100 ). Use the drawPolyline method in the Graphics class to connect the points.


13.13** ( Plotting functions using generic methods ) Write a generic class that draws the diagram for a function. The class is defined as follows :
   public abstract class   AbstractDrawFunction   extends   JPanel {  /** Polygon to hold the points */    private   Polygon p = new Polygon();   protected   AbstractDrawFunction () { drawFunction(); }  /** Return the y coordinate */    abstract double   f(   double   x);  /** Obtain points for x coordinates 100, 101, ..., 300 */    public void   drawFunction() {   for   (   int   x =   -100   ; x <=   100   ; x++) { p.addPoint(x +   200   ,   200   - (   int   )f(x)); } }  /** Implement paintComponent to draw axes, labels, and   * connecting points   */  

[Page 461]
   protected void   paintComponent(Graphics g) {  // To be completed by you  } } 

Test the class with the following functions:

 f(x) = x  2  ; f(x) = sin(x); f(x) = cos(x); f(x) = tan(x); f(x) = cos(x) + 5sin(x); f(x) = cos(x) + 5sin(x); f(x) = log(x) + x  2  ; 

For each function, create a class that extends the AbstractDrawFunction class and implements the f method. Figure 13.31(b) displays the drawings for the sine function and the cosine function.

13.14** ( Displaying a bar chart ) Write a program that uses a bar chart to display the percentages of the overall grade represented by projects, quizzes, midterm exams, and the final exam, as shown in Figure 13.32(a). Suppose that projects take 20 percent and are displayed in red, quizzes take 10 percent and are displayed in blue, midterm exams take 30 percent and are displayed in green, and the final exam takes 40 percent and is displayed in orange.
Figure 13.32. Exercise 13.14 and Exercise 13.15 use a bar chart and a pie chart to show the percentages of projects, quizzes, midterm exams, and final exam in the overall grade.

13.15** ( Displaying a pie chart ) Write a program that uses a pie chart to display the percentages of the overall grade represented by projects, quizzes, midterm exams, and the final exam, as shown in Figure 13.32(b). Suppose that projects take 20 percent and are displayed in red, quizzes take 10 percent and are displayed in blue, midterm exams take 30 percent and are displayed in green, and the final exam takes 40 percent and is displayed in orange.
13.16 ( Obtaining font information ) Write a program that displays the message "Java is fun" in a panel. Set the panel's font to TimesRoman, bold, and 20-pixel. Display the font's leading, ascent, descent, height, and the string width as a tool tip text for the panel, as shown in Figure 13.33(a).
Figure 13.33. (a) Exercise 13.16 displays font properties in a tool tip text. (b) Exercise 13.17 uses MessagePanel to display four strings.


[Page 462]
13.17 ( Using the MessagePanel class ) Write a program that displays four messages, as shown in Figure 13.33(b).
13.18 ( Using the StillClock class ) Write a program that displays two clocks. The hour , minute, and second values are 4 , 20 , 45 for the first clock, and 22 , 46 , 15 for the second clock, as shown in Figure 13.34(a).
Figure 13.34. (a) Exercise 13.18 displays two clocks. (b) Exercise 13.19 displays a clock with random hour and minute values. (c) Exercise 13.20 displays a detailed clock.

13.19* ( Random time ) Modify the StillClock class with three new Boolean properties: hourHandVisible , minuteHandVisible , and secondHandVisible , and their associated accessor and mutator methods. You can use the set methods to make a hand visible or invisible. Write a test program that displays only the hour and minute hands. The hour and minute values are randomly generated. The hour is between and 11 , and the minute is either or 30 , as shown in Figure 13.34(b).
13.20** ( Drawing a detailed clock ) Modify the StillClock class in §13.12, "Case Study: The StillClock Class," to draw the clock with more details on the hours and minutes, as shown in Figure 13.34(c).
13.21** ( Displaying a TicTacToe board with images ) Rewrite Exercise 12.7 to display an image in a JPanel instead of displaying an image icon in a JLabel .
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net