Chapter 14


Exercise 1 The first code example in Chapter 14 used the following code to set a frame's background color:

setBackground(new Color(128, 128, 128));

Describe the color that this line creates.

Solution 1 The color has equal levels of red, green, and blue, so it will be some kind of gray. Since the levels are halfway between the minimum (0) and the maximum (255), the gray will be about halfway between black and white: a neutral gray, neither dark nor light.

Exercise 2 Run Color Lab, and adjust the scrollbars so that the displayed color matches something you can see (a piece of clothing you're wearing, or something on your desk, or anything else you like). Now write an application that displays a frame whose interior is the color you've chosen.

Solution 2 The following code shows a frame whose interior matches the color of the shirt I was wearing when I wrote this.

import java.awt.*;  public class EmptyFrame extends Frame  {      EmptyFrame()      {          setBackground(new Color(0, 217, 255));          setSize(300, 300);      }      public static void main(String[] args)      {          EmptyFrame em = new EmptyFrame();          em.setVisible(true);      }  }

Remember that in addition to setting the background color, you have to call setSize() and setVisible(). Otherwise the frame cannot be seen.

Exercise 3 One of the code examples in Chapter 14 used the getSize() method, which Frame inherits from one of its superclasses. Use the API to find out which superclass implements the method.

Solution 3

java.awt.Component 

Exercise 4 Write a program that draws a five-pointed star. Your frame should be 400 x 400 pixels. The coordinates of the star's points are (200, 375), (97, 58), (366, 254), (34, 254), and (303, 58). The easy way is to write a paint() method that calls drawLine() five times. But that approach isn't ideal, because you have to type each x and each y twice. (Each point is the end of two lines, so it appears in two drawLine() calls.) Typing data, code, or anything else more than once is considered bad style. If one of the copies has a typo and doesn't match the original precisely, your program won't function correctly. To avoid duplication of data, your program should have two int arrays, defined as follows:

int[] xs = {200, 97, 366, 34, 303}; int[] ys = {375, 58, 254, 254, 58};

Your paint() method should have a loop that accesses these arrays. drawLine(...) should appear only once in your code, inside the loop.

Solution 4 The following program uses a loop to draw a five-pointed star:

 1. import java.awt.*;  2.  3. public class Supe extends Frame  4. {  5.   int[] xs = {352, 106, 200, 294, 48};  6.   int[] ys = {151, 329, 40, 329, 151};  7.  8.   Supe()  9.   { 10.     setSize(400, 400); 11.   } 12. 13.   public void paint(Graphics g) 14.   { 15.     for (int startPoint=0; 16.          startPoint<xs.length; 17.          startPoint++) 18.     { 19.       int endPoint = startPoint + 1; 20.        if (endPoint == xs.length) 21.          endPoint = 0; 22.       g.drawLine(xs[startPoint], ys[startPoint],      23.                  xs[endPoint], ys[endPoint]); 24.      } 25.   } 26. 27.   public static void main(String[] args) 28.   { 29.     (new Supe()).setVisible(true); 30.   } 31. }

Lines 19-21 can be replaced by the following single line:

Int endPoint = (startPoint+1) % xs.length;

Exercise 5 Write a program that lists all the font families that are available on your computer.

Solution 5 The following program lists all the available font families.

import java.awt.GraphicsEnvironment; public class ListFonts {   public static void main(String[] args)   {     GraphicsEnvironment grenv =                 GraphicsEnvironment.getLocalGraphicsEnvironment();    String[] names =        grenv.getAvailableFontFamilyNames();    for (int i=0; i<names.length; i++)      System.out.println(names[i]);   } }




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

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