| This applet illustrates additional drawing techniques and some 
              object design.  An instance of the Circle 
              class holds the radius and position (randomly chosen) of a circle. 
              The paintComponent() 
              method for DrawingPanel 
              loops through an array of Circle 
              objects and uses the fillOval() 
              method to draw each circle. The init() 
              method in SimpleDraw2Applet 
              selects a random number of circles and creates the array of Circle 
              objects, which it passes to the DrawingPanel 
              constructor. The DrawingPanel 
              object is added to the applet's panel.  
              
                 
                  |  |   
                  |  import 
                      javax.swing.*;import java.awt.*;
 
 /** Drawing example. **/
 public class SimpleDraw2Applet extends JApplet
 {
 public void init () {
 Container content_pane = getContentPane 
                      ();
 
 Math.random ();
 
 // Get the drawing area of the applet
 int dY = getSize ().height;
 int dX = getSize ().width;
 
 // Create a random number of circles 
                      up to 3
 int numCircles =  (int) 
                      (2.0 * Math.random () + 1.0);
 
 // Create a circle array
 Circle [] circs = new Circle[numCircles];
 
 // Now create the circle instances
 for (int i=0; i < numCircles; i++) 
                      {
 circs[i] 
                      = new Circle (dX,dY);
 }
 
 // Create an instance of DrawingPanel
 DrawingPanel drawing_panel = new 
                      DrawingPanel (circs);
 
 // Add the DrawingPanel to the contentPane.
 content_pane.add (drawing_panel);
 
 } // init
 
 } // class SimpleDraw2Applet
 |   
                  | class 
                    DrawingPanel extends JPanel {
 Circle [] fCircles;
 int fNumCircles;
 
 DrawingPanel  (Circle [] circles) {
 fCircles = circles;
 fNumCircles = circles.length;
 } // ctor
 
 public void paintComponent (Graphics g) {
 // First paint background
 super.paintComponent (g);
 
 // Add your drawing instructions here
 g.setColor (Color.red);
 
 // Draw the circles
 for ( int i=0; i < fNumCircles; i++){
 g.fillOval (fCircles[i].fX,fCircles[i].fY,
 fCircles[i].fDx,fCircles[i].fDy);
 }
 
 } // paintComponent
 
 } // class DrawingPanel
 |   
                  | /** 
                    Circle class definition. **/ class Circle
 {
 // These attributes provide the position &
 // dimensions of the circle and a box that
 // bounds it.
 int fCenterX,fCenterY;
 int fRadius;
 int fX,fY;
 int fDx,fDy;
 
 // Use constructor to
 Circle (int x_axis_width, int y_axis_height) {
 // Fix circles at 20% of horizontal 
                    axis
 fRadius = (int) (0.1 * x_axis_width);
 
 // choose random center location
 fCenterX = (int) ((x_axis_width) * 
                    Math.random ());
 fCenterY = (int) ((y_axis_height) 
                    * Math.random ());
 
 // Now calculate the bounding rectangle
 fX = fCenterX - fRadius;
 fY = fCenterY - fRadius;
 
 fDx = 2 * fRadius;
 fDy = 2 * fRadius;
 
 // Move away from edges.
 if (fX < 0) {
 fX = -fX;
 fCenterX += 
                    2 * fX;
 } else if ((fX+fDx) > x_axis_width) 
                    {
 fX -= 2 * 
                    fDx;
 fCenterX -= 
                    2 * fDx;
 } if (fY < 0) {
 fY = -fY;
 fCenterY += 
                    2 * fY;
 } else if ((fY+fDy) > y_axis_height) 
                    {
 fY -= 2 * 
                    fDy;
 fCenterY -= 
                    2 * fDy;
 }
 } // ctor
 } // class Circle
 |    Latest update: Oct. 26, 2004 |