/* * Copyright (c) 2002 Aravind Krishnaswamy. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * -Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduct the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended for * use in the design, construction, operation or maintenance of any nuclear * facility. */ /* * @(#)Fractal.java 1.00 08/30/2002 */ import java.awt.*; import java.awt.event.*; import java.applet.*; import java.awt.image.BufferedImage; public class MandelbrotFractalApplet extends Applet { MandelbrotFractalDrawPanel panel; MandelbrotFractalControls controls; public void init() { setLayout( new BorderLayout() ); panel = new MandelbrotFractalDrawPanel( 256, 256 ); controls = new MandelbrotFractalControls( panel ); add( "Center", panel ); add( "South", controls ); } public void destroy() { remove( panel ); } public static void main( String args[] ) { Frame f = new Frame("Mandelbrot Fractal test"); MandelbrotFractalApplet fractal = new MandelbrotFractalApplet( ); fractal.init(); fractal.start(); f.add( "Center", fractal ); f.setSize( 700, 300 ); f.show(); } public String getAppletInfo() { return "Mandelbrot fractal viewer by Aravind Krishnaswamy"; } } class MandelbrotFractalDrawPanel extends Panel { public MandelbrotFractal fractal = new MandelbrotFractal(); private BufferedImage output; private double lastX = 0; private double lastY = 0; public MandelbrotFractalDrawPanel( int width, int height ) { output = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB ); GenerateFractalImage( ); } public void paint( Graphics g ) { g.clearRect( 0, 0, this.getSize().width, this.getSize().height ); g.drawImage( output, 0, 0, this ); } public void GenerateFractalImage( ) { for( int y=0; y= 256 ) return count_so_far; if( mag > 4 ) return count_so_far; return count_mandelbrot_steps( xnsq - ynsq + x, 2 * xn * yn + y, x, y, count_so_far + 1 ); } public PEL GetColor( double x, double y ) { double fracx = x*(upper_x - lower_x) + lower_x; double fracy = y*(upper_y - lower_y) + lower_y; int count = count_mandelbrot_steps( fracx, fracy, fracx, fracy, 0 ); double d = 1.0 - (count / 256.0); if( (exp > 1.0) || (exp < 1.0) ) d = Math.pow( d, exp ); double OMd = 1.0 - d; // Interpolate PEL ret = new PEL(); ret.r = colorA.r * OMd + colorB.r * d; ret.g = colorA.g * OMd + colorB.g * d; ret.b = colorA.b * OMd + colorB.b * d; return ret; } }