media @ VU
applet-math-fractal-iterate1.jva
applet-math-fractal-iterate1.jva
/ applet-math-fractal-iterate1
// Symbolic x=x*x+c Iterations, Evgeny Demidov, 1 July 2002
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class applet-math-fractal-iterate1 extends java.applet.Applet
implements MouseMotionListener, KeyListener {
Image buffImage; Graphics buffGraphics;
int maxIt = 10, N = 1, numIt, w,h,h2, iXo,iYo, lbSize = 30,
xFun[],yFun[], xGist[],yGist[], xIt[],yIt[];
double C = -.5, maxIYI = 2., Xo = 0., Yo = 0., delX = 4., Step;
Label lbIt, lbN, lbC;
TextField tfIt, tfN, tfC;
public void init() {
h = Integer.parseInt(getParameter("height")) - lbSize; h2 = h / 2;
w = Integer.parseInt(getParameter("width"));
String s=getParameter("XYoDel"); if (s != null) {
StringTokenizer st = new StringTokenizer(s);
Xo = Double.valueOf(st.nextToken()).doubleValue();
Yo = Double.valueOf(st.nextToken()).doubleValue();
delX = Double.valueOf(st.nextToken()).doubleValue();}
s=getParameter("MaxIt"); if (s != null) maxIt=Integer.parseInt(s);
s=getParameter("N"); if (s != null) N=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"); add(lbC);
tfC = new TextField( "" + C, 9); add(tfC);
lbN = new Label("N"); add(lbN);
tfN = new TextField( "" + N, 3); add(tfN);
lbIt = new Label("It"); add(lbIt);
tfIt = new TextField( "" + maxIt, 3); add(tfIt);
tfC.addKeyListener(this); tfN.addKeyListener(this);
tfIt.addKeyListener(this);
addMouseMotionListener(this);
Step = delX / h;
iXo = h2 - (int)(Xo/Step); iYo = h2 + (int)(Yo/Step);
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{
N = Integer.parseInt( tfN.getText() );
maxIt = Integer.parseInt( tfIt.getText() );
C = Double.valueOf(tfC.getText()).doubleValue();
}catch ( NumberFormatException ne) {}
draw(); repaint();
}
e.consume();
}
public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {
C = Yo + 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);
int iYe = (int)((Yo-Xo)/Step);
buffGraphics.drawLine(0, h+iYe, h, iYe);
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 );
for (int i = 0; i <= maxIt; i++)
buffGraphics.fillRect(xIt[i]-1, yIt[i]-1, 3, 3);
}
public void paint(Graphics g) {
g.drawImage(buffImage, 0, lbSize, this);
showStatus( "It=" + numIt);
}
public void iterations() {
double X = 0., Y; int bakX, bakY;
xGist[0] = ( bakX = h2 - (int)(Xo/Step) );
yGist[0] = yIt[0] = h2 + (int)(Yo/Step);
for (int i = 1; i <= maxIt; i++) {
Y = X;
for (int n = N; n > 0; n--) Y = Y*Y + C;
int i2 = i<<1;
for (int n = N; n > 0; n--)
xGist[i2 - 1] = bakX;
yGist[i2 - 1] = ( bakY = h2 - (int)((Y - Yo)/Step) );
xGist[i2] = ( bakX = h2 + (int)((Y - Xo)/Step) );
yGist[i2] = yIt[i] = bakY;
X = Y; numIt = i;
if (Math.abs(Y) > maxIYI) break;
}
}
public void generateFun() {
for (int i = h - 1; i >= 0; i--) {
double X = Xo + (i-h2)*Step;
for (int n = N; n > 0; n--) X = X*X + C;
yFun[i] = h2 - (int)((X - Yo)/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.