[ Exercises | Chapter Index | Main Index ]

## Solution for Programming Exercise 7.6

This page contains a sample solution to one of the exercises from Introduction to Programming Using Java.

### Exercise 7.6:

The sample program RandomArt.java from Subsection 6.4.1 shows a different random "artwork" every four seconds. There are three types of "art", one made from lines, one from circles, and one from filled squares. However, the program does not save the data for the picture that is shown on the screen. As a result, the picture cannot be redrawn when necessary. In fact, every time paintComponent() is called, a new picture is drawn.

Write a new version of RandomArt.java that saves the data needed to redraw its pictures. The paintComponent() method should simply use the data to draw the picture. New data should be recomputed only every four seconds, in response to an event from the timer that drives the program.

To make this interesting, write a separate class for each of the three different types of art. Also write an abstract class to serve as the common base class for the three classes. Since all three types of art use a random gray background, the background color can be defined in their superclass. The superclass also contains a draw() method that draws the picture; this is an abstract method because its implementation depends on the particular type of art that is being drawn. The abstract class can be defined as:

    private abstract class ArtData {
       Color backgroundColor;  // The background color for the art.
       ArtData() {  // Constructor sets background color to be a random gray.
          int x = (int)(256*Math.random());
          backgroundColor = new Color( x, x, x );
       }
       abstract void draw(Graphics g);  // Draws this artwork.
    }

Each of the three subclasses of ArtData must define its own draw() method. It must also define instance variables to hold the data necessary to draw the picture. I suggest that you should create random data for the picture in the constructor of the class, so that constructing the object will automatically create the data for the random artwork. (One problem with this is that you can't create the data until you know the size of the panel, so you can't create an ArtData object in the constructor of the panel. One solution is to create an ArtData object at the beginning of the paintComponent() method, if the object has not already been created.) In each of the three subclasses, you will need to use one or more arrays or ArrayLists to store the data.

Discussion

In my solution, I defined subclasses LineArtData, CircleArtData, and SquareArtData of the basic abstract ArtData class. An instance variable named artData, of type ArtData, points to the object that holds the data for the current picture. When it's time to create a new artwork, I call the following method, which changes the value of artData. Note that this method has an equal chance of producing each of the three types of art:

    /**
     * Creates an object belonging to one of the three subclasses of
     * ArtData, and assigns that object to the instance variable, artData.
     * The subclass to use (that is, the type of art) is chosen at random.
     */
    private void createArtData() {
       switch ( (int)(3*Math.random()) ) {
       case 0:
          artData = new LineArtData();
          break;
       case 1:
          artData = new CircleArtData();
          break;
       case 2:
          artData = new SquareArtData();
          break;
       }
    }

This method is called by the action listener that responds to events from the timer, so a new artwork is created every time the timer generates an event, that is, every four seconds.

The paintComponent() method uses the background color from artData to fill the drawing area. It then tells artData to draw itself by calling its draw() method. As suggested in the exercise, it first makes sure that an art data object has been created by calling createArtData() if artData is still null; this can only be true the first time paintComponent() is called. So, the paintComponent() method is quite simple:

    public void paintComponent(Graphics g) {

       if (artData == null)  // If no artdata has yet been created, create it.
          createArtData();

       // Note:  Since the next two lines fill the entire panel, there is
       // no need to call super.paintComponent(g), since any drawing
       // that it does will only be covered up anyway.

       g.setColor(artData.backgroundColor); // Fill with the art's background color.
       g.fillRect( 0, 0, getWidth(), getHeight() );

       artData.draw(g);  // Draw the art.

    } // end paintComponent()

Note in particular the last line, artData.draw(g). This is a polymorphic method, since what gets drawn will depend on what class the object artData belongs to, and that changes as the program runs.

The only thing that remains for discussion is the three classes that define the three types of art. Note that we have been able to get this far without thinking about creating or drawing the actual art. This is because we have been thinking "abstractly," in terms of the abstract class.

Let's look in detail at LineArtData, one of the three concrete subclasses of ArtData. In the original program, line art was drawn in the paintComponent() method as follows:

    for (int i = 0; i < 500; i++) {
       int x1 = (int)(getWidth() * Math.random());
       int y1 = (int)(getHeight() * Math.random());
       int x2 = (int)(getWidth() * Math.random());
       int y2 = (int)(getHeight() * Math.random());
       Color randomHue = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
       g.setColor(randomHue);
       g.drawLine(x1,y1,x2,y2);
    }

Here, the coordinates and colors for each line are chosen at random and the line is immediately drawn using that data. For the new version, we want to save the data in instance variables so that the picture can be drawn and redrawn on demand. The creation of the data will be split from the drawing; the data will be created in the constructor and will be used to draw the picture in the draw() method. To save the data for all 500 lines, we have to store the coordinates and colors in arrays. There are several ways to do this, but I used one array for each piece of data, x1, y1, x2, y2, and the color. This is an example of parallel arrays. To store data for 500 lines, each array should be of length 500. For example, x1 is declared as an instance variable of type int[], and the array is created with the command "x1 = new int[500];". The arrays are created in the LineArtData constructor and are filled with random data. In the draw() method, the i-th line can be drawn with the commands:

    g.setColor( color[i] );    // Use the i-th color in the array
    g.drawLine( x1[i], y1[i], x2[i], y2[i] );

We just need a for loop to draw all the lines. The complete definition of LineArtData class is as follows:

    /**
     * Stores data for a picture that contains 500 random lines drawn in
     * different random colors.
     */
    private class LineArtData extends ArtData {
       Color[] color;         // color[i] is the color of line number i
       int[] x1, y1, x2, y2;  // line i goes from (x1[i],y1[i]) to (x2[i],y2[i]).
       LineArtData() {  // Constructor creates arrays and fills then randomly.
          color = new Color[500];
          x1 = new int[500];
          y1 = new int[500];
          x2 = new int[500];
          y2 = new int[500];
          for (int i = 0; i < 500; i++) {
             x1[i] = (int)(getWidth() * Math.random());
             y1[i] = (int)(getHeight() * Math.random());
             x2[i] = (int)(getWidth() * Math.random());
             y2[i] = (int)(getHeight() * Math.random());
             color[i] = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
          }
       }
       void draw(Graphics g) {  // Draw the picture.
          for (int i = 0; i < 500; i++) {
             g.setColor(color[i]);
             g.drawLine( x1[i], y1[i], x2[i], y2[i] );
          }
       }
    }

This is, by the way, a non-static nested class in the RandomArtPanel class. It can't be static because it uses the instance methods getWidth() and getHeight() from the containing class.

The other two classes can be approached in a similar way, but to mix things up a bit, I decided to use an ArrayList of objects, instead of parallel arrays, for the other two classes. For the circles, I created a class OneCircle to hold the data for one circle, and I used an ArrayList<OneCircle> to hold data for 100 circles. (An array of OneCircle would have worked just as well.) You can see the result in the complete solution below. Note, by the way, that OneCircle is a nested class that is inside another nested class. Java allows multiple levels of nesting.

The Solution

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.ArrayList;

    /**
     * A RandomArtPanel draws random pictures which might be taken to have
     * some vague resemblance to abstract art.  A new picture is produced every
     * four seconds.  There are three types of pictures:  random lines,
     * random circles, and random 3D rects.  The figures are drawn in
     * random colors on a background that is a random shade of gray.  The
     * data for a given piece of art is stored in a data structure so
     * that the picture can be redrawn if necessary.  The data is created
     * in response to the action event from a timer.
     */
    public class RandomArt2 extends JPanel {

        /**
         * A main routine to make it possible to run this program as an application.
         */
        public static void main(String[] args) {
            JFrame window = new JFrame("Random Art ??");
            RandomArt2 content = new RandomArt2();
            window.setContentPane(content);
            window.setSize(400,400);
            window.setLocation(100,100);
            window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            window.setVisible(true);
        }

        //---------------------------------------------------------------------
        /**
         * The data for the currently displayed picture (unless it is null).
         */
        private ArtData artData;

        /**
         * The constructor creates a timer with a delay time of four seconds
         * (4000 milliseconds), and with a RepaintAction object as its
         * ActionListener.  It also starts the timer running.  The
         * RepaintAction class is a nested class, defined below.
         */
        public RandomArt2() {
            RepaintAction action = new RepaintAction();
            Timer timer = new Timer(4000, action);
            timer.start();
        }

        /**
         * The paintComponent() method fills the panel with a random shade of
         * gray and then draws one of three types of random "art".  The data for
         * the picture is in the variable artData (if this variable is null,
         * then an artData object is created here).
         */
        public void paintComponent(Graphics g) {

            if (artData == null)  // If no artdata has yet been created, create it.
                createArtData();

            // Note:  Since the next two lines fill the entire panel, there is
            // no need to call super.paintComponent(g), since any drawing
            // that it does will only be covered up anyway.

            g.setColor(artData.backgroundColor); // Fill with the art's background color.
            g.fillRect( 0, 0, getWidth(), getHeight() );

            artData.draw(g);  // Draw the art.

        } // end paintComponent()

        /**
         * Creates an object belonging to one of the three subclasses of
         * ArtData, and assigns that object to the instance variable, artData.
         * The subclass to use (that is, the type of art) is chosen at random.
         */
        private void createArtData() {
            switch ( (int)(3*Math.random()) ) {
            case 0:
                artData = new LineArtData();
                break;
            case 1:
                artData = new CircleArtData();
                break;
            case 2:
                artData = new SquareArtData();
                break;
            }
        }

        /**
         * An abstract class that represents the data for a random work
         * of "art".  Different concrete subclasses of this class represent
         * different types of art.  This class contains a background
         * color which is a random shade of gray, selected when the object
         * is constructed.
         */
        private abstract class ArtData {
            Color backgroundColor;  // The background color for the art.
            ArtData() {  // Constructor sets background color to be a random shade of gray.
                int x = (int)(256*Math.random());
                backgroundColor = new Color( x, x, x );
            }
            abstract void draw(Graphics g);  // Draw the picture.
        }

        /**
         * Stores data for a picture that contains 500 random lines drawn in
         * different random colors.
         */
        private class LineArtData extends ArtData {

            Color[] color;         // color[i] is the color of line number i
            int[] x1, y1, x2, y2;  // line i goes from (x1[i],y1[i]) to (x2[i],y2[i]).
            LineArtData() {  // Constructor creates arrays and fills then randomly.
                color = new Color[500];
                x1 = new int[500];
                y1 = new int[500];
                x2 = new int[500];
                y2 = new int[500];
                for (int i = 0; i < 500; i++) {
                    x1[i] = (int)(getWidth() * Math.random());
                    y1[i] = (int)(getHeight() * Math.random());
                    x2[i] = (int)(getWidth() * Math.random());
                    y2[i] = (int)(getHeight() * Math.random());
                    color[i] = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
                }
            }
            void draw(Graphics g) {  // Draw the picture.
                for (int i = 0; i < 500; i++) {
                    g.setColor(color[i]);
                    g.drawLine( x1[i], y1[i], x2[i], y2[i] );
                }
            }
        }

        /**
         * Stores data for a picture that contains 200 circles with
         * radius 50, with random centers, and drawn in random colors.
         */
        private class CircleArtData extends ArtData {
            class OneCircle {
                Color color;  // the color of the th circle
                int centerX;  // center of circle is at (centerX, centerY)
                int centerY;
            }
            ArrayList<OneCircle> circles;
            CircleArtData() {  // Constructor creates arrays and fills then randomly.
                circles = new ArrayList<OneCircle>();
                for (int i = 0; i < 200; i++) {
                    OneCircle c = new OneCircle();
                    c.centerX =  (int)(getWidth() * Math.random());
                    c.centerY = (int)(getHeight() * Math.random());
                    c.color = Color.getHSBColor( (float)Math.random(), 1.0F, 1.0F);
                    circles.add(c);
                }
            }
            void draw(Graphics g) {  // Draw the picture.
                for (OneCircle circle : circles) {
                    g.setColor(circle.color);
                    g.drawOval(circle.centerX - 50, circle.centerY - 50, 100, 100);
                }
            }
        }

        /**
         * Stores data for a picture that contains 25 filled squares with
         * random sizes and  with random centers, and drawn in random colors.
         */
        private class SquareArtData extends ArtData {
            class OneSquare {
                Color color;  // the color of the square
                int centerX;  // the center of square is (centerX, centerY)
                int centerY;
                int size;     // the length of a side of the square
            }
            ArrayList<OneSquare> squares = new ArrayList<OneSquare>();
            SquareArtData() {  // Constructor creates arrays and fills then randomly.
                for (int i = 0; i < 25; i++) {
                    OneSquare s = new OneSquare();
                    s.centerX =  (int)(getWidth() * Math.random());
                    s.centerY = (int)(getHeight() * Math.random());
                    s.size = 30 + (int)(170*Math.random());
                    s.color = new Color( (int)(256*Math.random()),
                            (int)(256*Math.random()), (int)(256*Math.random()) );
                    squares.add(s);
                }
            }
            void draw(Graphics g) {  // Draw the picture.
                for ( OneSquare square : squares ) {
                    g.setColor(square.color);
                    g.fill3DRect(square.centerX - square.size/2, square.centerY - square.size/2,
                            square.size, square.size, true);
                }
            }
        }

        /**
         * A RepaintAction object creates a new artData object and calls the repaint
         * method of this panel each time its actionPerformed() method is called.
         * An object of this type is used as an action listener for a Timer that
         * generates an ActionEvent every four seconds.  The result is a new work of
         * art every four seconds.
         */
        private class RepaintAction implements ActionListener {
            public void actionPerformed(ActionEvent evt) {
                createArtData();
                repaint();
            }
        }

    } // end class RandomArt2

[ Exercises | Chapter Index | Main Index ]