media @ VU
applet-math-fractal-iterateL.jva
applet-math-fractal-iterateL.jva
/ applet-math-fractal-iterateL
// Interactive X = X*X + C Iterations, Evgeny Demidov, 30 June 2006
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class applet-math-fractal-iterateL extends java.applet.Applet
implements MouseMotionListener, KeyListener {
Image buffImage; Graphics buffGraphics;
int maxIt = 10, numIt, w,h,h2, iXo,iYo, lbSize = 30,
xFun[],yFun[], xGist[],yGist[], xIt[],yIt[];
double C = -.5, maxIYI = 2., Xo = 0., Step;
Label lbIt, lbL, lbC, lbXo;
TextField tfIt, tfL, tfC, tfXo;
public void init() {
h = Integer.parseInt(getParameter("height")) - lbSize; h2 = h / 2;
w = Integer.parseInt(getParameter("width"));
String s=getParameter("Xo");
if (s != null) Xo = Double.valueOf(s).doubleValue();
s=getParameter("MaxIt"); if (s != null) maxIt=Integer.parseInt(s);
s = getParameter("C"); if (s != null) C = Double.valueOf(s).doubleValue();
xFun = new int[h]; for (int i = h-1; i >= 0; i--) xFun[i] = i;
yFun = new int[h];
buffImage = createImage(w, h);
buffGraphics = buffImage.getGraphics();
lbC = new Label("C", Label.RIGHT); add(lbC);
tfC = new TextField( "" + C, 6); add(tfC);
lbXo = new Label("Xo", Label.RIGHT); add(lbXo);
tfXo = new TextField( ""+Xo, 4); add(tfXo);
lbIt = new Label("It", Label.RIGHT); add(lbIt);
tfIt = new TextField( "" + maxIt, 2); add(tfIt);
lbL = new Label("L", Label.RIGHT); add(lbL);
tfL = new TextField( "" + 0, 4); add(tfL);
tfC.addKeyListener(this);
tfIt.addKeyListener(this); tfXo.addKeyListener(this);
addMouseMotionListener(this);
Step = 4./h;
iXo = iYo = h2;
draw();
}
public void destroy() { removeMouseMotionListener(this); }
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
final int keyEnter = 10;
if (e.getKeyCode() == keyEnter) {
try{
maxIt = Integer.parseInt( tfIt.getText() );
C = Double.valueOf(tfC.getText()).doubleValue();
Xo = Double.valueOf(tfXo.getText()).doubleValue();
}catch ( NumberFormatException ne) {}
draw(); repaint();
}
e.consume();
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
if ( e.isShiftDown() ){
Xo = Step*(e.getX() - h2);
tfXo.setText( Float.toString((float)Xo) );}
else{
C = Step*(h2 - (e.getY() - lbSize));
tfC.setText( Float.toString((float)C) );}
draw(); repaint();
e.consume();
}
public void draw() {
xGist = new int[maxIt<<1 + 1]; yGist = new int[maxIt<<1 + 1];
xIt = new int[maxIt+1]; yIt = new int[maxIt+1];
for (int i = 0; i <= maxIt; i++)
xIt[i] = h + (int)((w-h-1)*(double)i/(maxIt));
generateFun(); iterations();
buffGraphics.setColor(Color.white);
buffGraphics.fillRect(0, 0, w - 1, h - 1);
buffGraphics.setColor(Color.black);
buffGraphics.drawRect(0, 0, w - 1, h - 1);
buffGraphics.drawLine(h, 0, h, h - 1);
buffGraphics.drawLine(0, iYo, w - 1, iYo);
buffGraphics.drawLine(iXo, 0, iXo, h - 1);
buffGraphics.setColor(Color.green);
buffGraphics.drawLine(0, h, h, 0);
buffGraphics.setColor(Color.blue);
buffGraphics.drawPolyline( xFun, yFun, h );
buffGraphics.setColor(Color.red);
buffGraphics.drawPolyline( xGist, yGist, numIt<<1 );
buffGraphics.drawPolyline( xIt, yIt, numIt+1 );
buffGraphics.fillRect(xGist[0]-1, yGist[0]-1, 3, 3);
}
public void paint(Graphics g) {
g.drawImage(buffImage, 0, lbSize, this);
// showStatus( "It=" + numIt);
}
public void iterations() {
double X = Xo, Y, PX = 1, L; int bakX, bakY;
xGist[0] = bakX = h2 + (int)(Xo/Step);
yGist[0] = h2; yIt[0] = h - bakX;
for (int i = 1; i <= maxIt; i++) {
Y = X*X + C; PX *= X;
int i2 = i<<1;
xGist[i2 - 1] = bakX;
yGist[i2 - 1] = ( bakY = h2 - (int)(Y/Step) );
xGist[i2] = ( bakX = h2 + (int)(Y/Step) );
yGist[i2] = yIt[i] = bakY;
X = Y; numIt = i;
if (Math.abs(Y) > maxIYI) break;
}
L = Math.log(2.) + Math.log(Math.abs(PX))/maxIt;
tfL.setText( Float.toString((float)L) );
}
public void generateFun() {
for (int i = h - 1; i >= 0; i--) {
double X = (i-h2)*Step;
X = X*X + C;
yFun[i] = h2 - (int)(X/Step);
}
}
public void update(Graphics g) { paint(g); }
}
(C) A. Eliëns
2/9/2007
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.