media @ VU
applet-math-fractal-stdmap-separatrix.jva
applet-math-fractal-stdmap-separatrix.jva
/ applet-math-fractal-stdmap-separatrix
// Mixing, Evgeny Demidov, 28 Aug 2007
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.StringTokenizer;
public class applet-math-fractal-stdmap-separatrix extends java.applet.Applet implements MouseListener,
KeyListener, ActionListener {
double Yc=.0, Xc=-.5, dx=6.283, dy=dx, R=.3, K=1, Pi=Math.PI, Pi2=Pi*2;
int maxColor=3, N=1, w, h, w2, h2, pixArr[];
Image img; IndexColorModel RainbowColor;
Label lbK, lbN, lbR;
TextField tfXY, tfK, tfN, tfR;
Button btPl, btMn;
public void init() {
String s=getParameter("XYmidDelCD"); if (s != null) {
StringTokenizer st = new StringTokenizer(s);
Xc = Double.valueOf(st.nextToken()).doubleValue();
Yc = Double.valueOf(st.nextToken()).doubleValue();
dx = Double.valueOf(st.nextToken()).doubleValue();
dy = Double.valueOf(st.nextToken()).doubleValue();}
s=getParameter("K"); if (s != null) K = Double.valueOf(s).doubleValue();
s=getParameter("R"); if (s != null) R = Double.valueOf(s).doubleValue();
s=getParameter("N"); if (s != null) N=Integer.parseInt(s);
byte rColor[] = new byte[maxColor], bColor[] = new byte[maxColor],
gColor[] = new byte[maxColor];
bColor[1] = gColor[1] = 0; rColor[1] = (byte)255;
rColor[2] = gColor[2] = 0; bColor[2] = (byte)255;
rColor[0] = gColor[0] = bColor[0] = (byte)255;
RainbowColor = new IndexColorModel( 8, maxColor, rColor,gColor,bColor);
w = getSize().width; h = getSize().height;
w2 = w/2; h2 = h/2;
pixArr = new int[w*h];
this.setLayout( new FlowLayout(FlowLayout.LEFT, 0, 0) );
tfXY = new TextField( ""+(float)Xc+" "+(float)Yc+"; "+(float)dx+
" "+(float)dy, 20); add(tfXY);
lbK = new Label("K", Label.RIGHT); add(lbK);
tfK = new TextField( "" + K, 2); add(tfK);
tfK.addKeyListener(this);
lbR = new Label("R", Label.RIGHT); add(lbR);
tfR = new TextField( "" + R, 2); add(tfR);
tfR.addKeyListener(this);
lbN = new Label("N", Label.RIGHT); add(lbN);
tfN = new TextField( "" + N, 1); add(tfN);
tfN.addKeyListener(this);
btPl = new Button("+"); btPl.addActionListener(this); add(btPl);
btMn = new Button("-"); btMn.addActionListener(this); add(btMn);
Draw();
addMouseListener(this);
}
public void destroy() { removeMouseListener(this); }
public void mouseClicked(MouseEvent e) {} //1.1 event handling
public void mousePressed(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {
Xc = Xc+(e.getX()-w2)*dx/w;
Yc = Yc-(e.getY()-h2)*dx/w;
if (e.isControlDown()){
dy *= 2.; if (!e.isShiftDown()) dx *= 2.;}
else {
dy /= 2.; if (!e.isShiftDown()) dx /= 2.;}
tfXY.setText( ""+(float)Xc+" "+(float)Yc+"; "+(float)dx+" "+(float)dy);
Draw();
repaint();
}
public void actionPerformed(ActionEvent e){
if ( e.getActionCommand().equals("+") ) N++;
else N--;
tfN.setText(""+N);
Draw();
repaint();
}
public void keyTyped(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {
final int keyEnter = 10;
if (e.getKeyCode() == keyEnter) {
try{
K = Double.valueOf(tfK.getText()).doubleValue();
R = Double.valueOf(tfR.getText()).doubleValue();
N = Integer.parseInt(tfN.getText());
Draw();
repaint();
}catch ( NumberFormatException ne) {}
}
e.consume();
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public void Draw(){
for (int p = h*w; p > 0;) pixArr[--p] = 0;
int pix, ix,iy;
pix = 0;
double X,Y, Xo,Yo;
for (iy=0; iy < h; iy++) {
Yo = Yc + (h2-iy)*dy/h;
for (ix=0; ix < w; ix++){
Xo = X = Xc + (ix-w2)*dx/w; Y = Yo;
for(int i=N; i>0; i--){
Y = Y + K*Math.sin(X);
X = X + Y;
while(X < -Pi) X += Pi2;
while(X > Pi) X -= Pi2;
}
if((Math.abs(X) < R)&&(Math.abs(Y) < R)) pixArr[pix] = 2;
X = Xo; Y = Yo;
for(int i=N; i>0; i--){
X = X - Y;
Y = Y - K*Math.sin(X);
while(X < -Pi) X += Pi2;
while(X > Pi) X -= Pi2;}
if((Math.abs(X) < R)&&(Math.abs(Y) < R)) pixArr[pix] = 1;
pix++;}
}
//System.out.println(""+Xc +" "+Yc);
img = createImage(new MemoryImageSource(w, h, RainbowColor, pixArr, 0, w));
}
}
(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.