Exercises


Note

The solutions to these exercises are in Appendix B.

  1. The first code example in this chapter used the following code to set a frame's background color:

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

    Describe the color that this line creates.

  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.

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

  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.

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




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