Source of FractalJPanel.java


  1: // Fig. 15.24: FractalJPanel.java
  2: // FractalJPanel demonstrates recursive drawing of a fractal.
  3: import java.awt.Graphics;
  4: import java.awt.Color;
  5: import java.awt.Dimension;
  6: import javax.swing.JPanel;
  7: 
  8: public class FractalJPanel extends JPanel
  9: {
 10:    private Color color; // stores color used to draw fractal
 11:    private int level;   // stores current level of fractal
 12: 
 13:    private final int WIDTH = 400;  // defines width of JPanel
 14:    private final int HEIGHT = 400; // defines height of JPanel
 15: 
 16:    // set the initial fractal level to the value specified
 17:    // and set up JPanel specifications
 18:    public FractalJPanel( int currentLevel )
 19:    {
 20:       color = Color.BLUE;  // initialize drawing color to blue
 21:       level = currentLevel; // set initial fractal level
 22:       setBackground( Color.WHITE );
 23:       setPreferredSize( new Dimension( WIDTH, HEIGHT ) );
 24:    } // end constructor FractalJPanel
 25: 
 26:    // draw fractal recursively
 27:    public void drawFractal( int level, int xA, int yA, int xB, 
 28:       int yB, Graphics g )
 29:    {
 30:       // base case: draw a line connecting two given points
 31:       if ( level == 0 ) 
 32:          g.drawLine( xA, yA, xB, yB );
 33:       else // recursion step: determine new points, draw next level
 34:       {        
 35:          // calculate midpoint between (xA, yA) and (xB, yB)
 36:          int xC = ( xA + xB ) / 2;
 37:          int yC = ( yA + yB ) / 2;
 38: 
 39:          // calculate the fourth point (xD, yD) which forms an 
 40:          // isosceles right triangle between (xA, yA) and (xC, yC) 
 41:          // where the right angle is at (xD, yD)
 42:          int xD = xA + ( xC - xA ) / 2 - ( yC - yA ) / 2;
 43:          int yD = yA + ( yC - yA ) / 2 + ( xC - xA ) / 2;
 44:          
 45:          // recursively draw the Fractal
 46:          drawFractal( level - 1, xD, yD, xA, yA, g );
 47:          drawFractal( level - 1, xD, yD, xC, yC, g );
 48:          drawFractal( level - 1, xD, yD, xB, yB, g );  
 49:       } // end else
 50:    } // end method drawFractal
 51: 
 52:    // start the drawing of fractal
 53:    public void paintComponent( Graphics g )
 54:    {
 55:       super.paintComponent( g );
 56: 
 57:       // draw fractal pattern
 58:       g.setColor( color );
 59:       drawFractal( level, 100, 90, 290, 200, g ); 
 60:    } // end method paintComponent
 61: 
 62:    // set the drawing color to c
 63:    public void setColor( Color c )
 64:    {
 65:       color = c;
 66:    }
 67:     
 68:    // set the new level of recursion
 69:    public void setLevel( int currentLevel )
 70:    {
 71:       level = currentLevel;
 72:    }
 73: 
 74:    // returns level of recursion 
 75:    public int getLevel()
 76:    {
 77:       return level;
 78:    }
 79: }; // end class FractalJPanel
 80: 
 81: /*************************************************************************
 82: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 83: * Pearson Education, Inc. All Rights Reserved.                           *
 84: *                                                                        *
 85: * DISCLAIMER: The authors and publisher of this book have used their     *
 86: * best efforts in preparing the book. These efforts include the          *
 87: * development, research, and testing of the theories and programs        *
 88: * to determine their effectiveness. The authors and publisher make       *
 89: * no warranty of any kind, expressed or implied, with regard to these    *
 90: * programs or to the documentation contained in these books. The authors *
 91: * and publisher shall not be liable in any event for incidental or       *
 92: * consequential damages in connection with, or arising out of, the       *
 93: * furnishing, performance, or use of these programs.                     *
 94: *************************************************************************/