7.2. Programs as Specifying Drawing ProcessAnother thing we can do with drawing is create pictures that are exactly specifiedthings that might be too hard to do by hand. Take, for example, Figure 7.9. Figure 7.9. A programmed grayscale effect. |
/** * Method to draw a gray effect picture on the * current picture */ public void drawGrayEffect() { // create a medium gray color to use Color medGray = new Color(100,100,100); // Do 100 columns of medium gray for (int x = 0; x < 100; x++) for (int y = 0; y < 100; y++) this.getPixel(x,y).setColor(medGray); /* Do 100 columns of gray starting at medium * gray and getting lighter */ for (int x=100, grayLevel=100; x < 200; x++,grayLevel++) for (int y=0; y < 100; y++) this.getPixel(x,y).setColor( new Color(grayLevel,grayLevel,grayLevel)); // Do 100 columns starting at black and getting lighter for (int x=200, grayLevel=0; x < 300; x++, grayLevel++) for (int y=0; y < 100; y++) this.getPixel(x,y).setColor( new Color(grayLevel,grayLevel,grayLevel)); // Do 100 columns of medium gray for (int x=300; x < 400; x++) for (int y=0; y < 100; y++) this.getPixel(x,y).setColor(medGray); } |
To use this method, create a picture of the file that has a blank 640 by 480 picture in it. Invoke the method on that picture.
> Picture p = new Picture(FileChooser.getMediaPath("640x480.jpg")); > p.drawGrayEffect(); > p.show();
Graphics methods are very good at drawings that are repeated where the positions of lines and shapes and the selection of colors can be made by mathematical relationships.
/** * Method to draw a picture with a succession of * filled rectangles with the top left corner the * darkest and the bottom right the lightest on * the current picture */ public void drawFilledRectangles() { Graphics g = this.getGraphics(); Color color = null; // loop 25 times for (int i = 25; i < 0; i--) { color = new Color(i * 10, i * 5, i); g.setColor(color); g.fillRect(0,0,i*10,i*10); } } |
To use this method create a picture of the file that has a blank 640 by 480 picture in it. Invoke the method on that picture.
> Picture p = new Picture(FileChooser.getMediaPath("640x480.jpg")); > p.drawFilledRectangles(); > p.show();
/** * Method to draw a picture with a succession of * rectangles on the current picture */ public void drawRectangles() { Graphics g = this.getGraphics(); Color color = null; // loop 25 times for (int i = 25; i > 0; i--) { g.setColor(Color.black); g.drawRect(i,i,i*3,i*4); g.drawRect(100+i*4,100+i*3,i*8,i*10); } } |
To use this method, create a picture of the file that has a blank 640 by 480 picture in it. Invoke the method on that picture.
> Picture p = new Picture(FileChooser.getMediaPath("640x480.jpg")); > p.drawRectangles(); > p.show();
Why do we write programs, especially to draw pictures? Couldn't we draw pictures like these in Photoshop or Visio? Certainly we can, but we'd have to know how, and that's not easy knowledge to come by. Could we teach you how to do this in Photoshop? Probably, but that may take a lot of effortPhotoshop isn't simple.
But if we give you these methods (programs), you can create the picture anytime you want. What's more, by giving you the methods, We're giving you the exact definition that you can go and change for yourself.
|
Imagine that you have some process to communicate. It doesn't have to be drawingimagine that it's a financial process (such that you could do it in a spreadsheet or in a program like Quicken) or something that you do with text (such as laying out text for a book or a brochure). If you can do something by hand, you should just do it. If you need to teach someone else to do it, consider writing a program to do it. If you need to explain to lots of people how to do it, definitely use a program. If you want lots of people to be able to do the process themselves, without someone having to teach them something first, write a program and give the people the program.