Section 7.2. Programs as Specifying Drawing Process


[Page 226 (continued)]

7.2. Programs as Specifying Drawing Process

Another 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.
(This item is displayed on page 227 in the print version)


This is a rendering of a famous optical illusion, and it's not as effective as the famous onesbut it's simple to understand how this version works. Our eyes tell us that the left half of the picture is lighter than the right half, even though the end quarters are exactly the same shade of gray. The effect is caused by the sharp boundary between the middle quarters, where one moves (left-to-right) from gray to white, and the other moves black to gray.

The image in Figure 7.9 is a carefully defined and created picture. It would be very hard to do with pencil and paper. It would be possible to do with something like Photoshop, but it wouldn't be easy. Using the graphics methods in this chapter, however, we can easily specify exactly what that picture should be.


[Page 227]
Program 54. Draw the Gray 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); }



[Page 228]

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.

Program 55. Draw the Picture in Figure 7.10

/**  * 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);   } }


Figure 7.10. Nested colored rectangles.



[Page 229]

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();


Program 56. Draw the Picture in Figure 7.11

/**  * 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);   } }


Figure 7.11. Nested outlined rectangles.


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();



[Page 230]

7.2.1. Why Do We Write Programs?

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.

Computer Science Idea: We Write Programs to Encapsulate and Communicate Process

The reason why we write programs is to exactly specify a process and to communicate it to others.


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.



Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
Introduction to Computing & Programming Algebra in Java(c) A Multimedia Approach
ISBN: N/A
EAN: N/A
Year: 2007
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net