Drawing Demo

What do computer programmers do? I wrote this page as a visual aid, and used it when I spoke at Career Day at my daughter's school. It runs a simple drawing applet, and also shows the java source code.

Today's kids being pretty familiar with computers, they pointed out a bunch of features it was lacking. I decided to grow this into a set of pages, adding the requested features, and hopefully showing something of the development process. What programmers mostly do (or should) is cooperate, and try to reuse existing work instead of doing it over. The Web itself is a fine example of this. Lots of programs cooperate to let you read this page.

Even the small program shown below uses other peoples existing work, so all I have to say to draw a button on the screen is "new Button" and I have one. Button is part of a "toolkit" that comes with Java. The toolkit contains many useful program parts, that can be assembled into larger programs.

Doodle is taken from the DoodlePad example in Exploring Java, by Patrick Niemeyer and Joshua Peck, first edition, Copyright 1996 by O'Reilly and Associates. It lets you draw with the mouse. Use the button to clear the picture.

You cannot run applets

/** From the DoodlePad example in Exploring Java,
 * by Patrick Niemeyer and Joshua Peck,
 * First edition Copyright 1996 O'Reilly Publishing. */

import java.awt.*;
import java.applet.*;

public class Doodle extends Applet {

  DrawingPad dPad;

  Button clearButton;

/** To run as an application,
 * which is handy while we are writing the program,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here.
 * When run on the Web as an applet,
 * the browser provides the frame,
 * and calls the constructor and init methods. */

  public static void main (String []argv) {

      Doodle cd = new Doodle ();

      Frame ff = new Frame ("Doodle");
      ff.setBounds (100, 100, 600, 300);

      ff.add (cd);
      cd.init ();

      ff.show ();
  }

/** Make everything we need,
 * just a drawing surface and control. */

  public void init () {

/* A big central drawing area and just one control */

     setLayout (new BorderLayout ());

     add ("Center", dPad = new DrawingPad ());

     Panel np = new Panel ();

     np.add (clearButton = new Button ("Clear"));

     add ("North", np);
  }

/** Handle the button and say we did */

  public boolean action (Event e, Object o) {
     if (e.target == clearButton) dPad.clear ();

     return true;
  }

}

class DrawingPad extends Canvas {

  Image di;
  Graphics dg;
  int xpos, ypos, xold, yold;

  DrawingPad () {
      setBackground (Color.white);
      setForeground (Color.blue);
  }
   
  public boolean mouseDown (Event e, int x, int y) {

     xold = x; yold = y;

     return true;
  }

/* User is adding to the drawing */

  public boolean mouseDrag (Event e, int x, int y) {
     if (dg == null) return false;

/* add another line segment to the offscreen image. */

     xpos = x; ypos = y;
     dg.drawLine (xold, yold, xpos, ypos);
     xold=xpos; yold=ypos;

/* we need a repaint to show new stuff so ask for it */

     repaint ();
     return true;
  }

/** Prevent pre-clear since we fill whole space anyway,
 * and clearing before draw would make it blink */

  public void update (Graphics g) { paint (g); }

  public void paint (Graphics g) {
     if (di == null) {
         di = createImage (size ().width, size ().height);
         dg = di.getGraphics ();
     }

/* Everything from the accumulated offscreen image */

     g.drawImage (di, 0, 0, null);
  }

/* User wants to start a new drawing so clear the old one */

  public void clear () {
     di.getGraphics ().clearRect (0, 0,
                                  size ().width, size ().height);
     repaint ();
  }

}
Download the Doodle source code. As you can see here, Doodle is a very small program.

Another thing that programmers do (or should) is listen to users, to find out what their programs really need to be useful. Here are some ideas the class had for additions to Doodle.

Some of these are easy to add, but the last few are hard or impossible. Now we can add some useful features to it.

My Other Java..

Please also visit my home page.

Was this Helpful?

Was this Helpful? Suggestions? Was it what you were looking for? How did you find this page? Search result, following a link, recommendation? Please let me know. morris_hirsch@brown.edu

Legalities

This software is provided free of charge with no support. Tell me if you do have a problem, and I will try to help, but I cannot promise to.

Except as otherwise noted, this work is Copyright (C) 2000 by Morris Hirsch. In particular, Doodle is taken from the DoodlePad example in Exploring Java, by Patrick Niemeyer and Joshua Peck, first edition copyright 1996 by O'Reilly and Associates. All rights reserved, except as granted here.

Redistribution and use in source and binary forms, with or without modification, for any purpose, are permitted, except as may be restricted by original copyright holders, provided that the following conditions are met:

DISCLAIMER

This software is provided free of charge with no support.
This software is provided in source format; You are advised to examine it and understand it, before putting it to use.
This software uses only algorithms and coding techniques that are available to the public in the open literature. You are however, advised to make your own determination of legality, for your intended use.
This software is provided by the Author and Contributors ``AS IS'' and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose, are disclaimed.
In no event shall the Author or Contributors, or their Agents or Servants, be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
1