Aqua Phoenix
     >>  Research >>  OOP  
 

Navigator
   
 
       
   

17. Kaleidoscope

17.1 Summary

  • Building a Kaleidoscope out of colored lines
  • Extending it with other shapes

17.2 Contents

17.2.1 Building a Kaleidoscope out of colored lines

The goal for this class is to program a Graphics Java applet (Kaleidoscope) to demonstrate the use of the Graphics object. Only one class is necessary for the Kaleidoscope; one that incorporates a constructor and a Paint() method. Students will make extensive use of loops, as new shapes constantly appear on the Kaleidoscope applet. For the purpose of the exercise, the screen is divided into 8 triangles with a common point in the middle of the screen. The final division looks somewhat like the British flag with eight triangles. Every object that is drawn in one of the eight triangles, must also be drawn in perspective in the other seven triangles, i.e. flipped, rotated, and/or mirrored.

For each iteration A, a color is picked at random; then two pairs of coordinates are randomly chosen. For each sub-iteration B, the random points are checked, and should they lie outside the triangle, the pairs are chosen again, until they both lie within the triangle. End of iteration B. Now, two "steps" are randomly chosen. These numbers lie between 0 and 4. For each of 40 iterations C, the first of the two previously picked pairs of coordinates are continuously incremented by step1 (both x and y), and the same for the second pair using step2. End of iteration C. After 50 iterations of A, B, and C, the screen is swiped from left to right, and the algorithm starts anew.

The complete algorithm is built in 3 flow charts, where the first concentrates on the ground structure and only drawing one line per triangle; the second flow chart then adds another loop that repeatedly draws lines so as to form pseudo-two dimensional shapes. The third flow chart incorporates the code for swiping the image clean.

Let's look at the flowchart diagrams of each of the 3 Kaleidoscopes:

1. The simplest version:

Figure 1.5
2. The intermediate version:

Figure 1.6
3. The final version:

Figure 1.7
The final applet would look at follows:

Everyone is encouraged to come up with their own idea of what shapes to use and how these shapes are represented in the applet. For homework, students are encouraged to come up with exotic shapes. The following is the code for the existing applet. The file "Swipe.java" should be downloaded as well, however, students need not change it or understand it at this point.

Kaleidoscope.java

Swipe.java

Kaleidoscope.html