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.
/** 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.
Please also visit my home page.
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: