Home : Course Map : Chapter 6 : Java :
More Drawing - Demo
JavaTech
Course Map
Chapter 6

Introduction
AWT
Swing
Containers
  Demo 1
UI Components
  Demo 2
UI Layout
  Demo 3   Demo 4
Text Display
  Demo 5
Drawing
  Demo 6   Demo 7
Draw Polygons
  Demo 8   Demo 9
Colors
Text Draw 
  Demo 10
Images
  Demo 11
Exercises

    Supplements
AWT
  Demo 1
Drawing
  Demo 2
Text Drawing
  Demo 3
UI Components
  Demo 4

Java2D
Shapes & Areas
  Demo 1   Demo 2
Stroke & Paint
  Demo 3
Transforms
  Demo 4
Gradients&Textures
  Demo 5   Demo 6
Text
  Demo 7   Demo 8
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

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

            Tech
Java Tech Graphics
Starting to Plot
  Demo 1
Drawing Panel
  Demo 2
Histogram Display

  Demo 3
Exercises

           Physics
Display Text Data
  Demo 1
Plot Data
  Demo 2
Find Max/Min
  Demo 3
Exercises

  
  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.