// The Standard map with colors, Evgeny Demidov, 31 Aug 2003 import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.StringTokenizer; public class @file extends java.applet.Applet implements MouseListener, KeyListener, ActionListener { int w,h,w2,h2, it=3000, rand=50, pixArr[], maxColor=96; double Xo=3, Po=-4, K=.3, Pi2 = Math.PI*2, Xc=Math.PI, Yc=0, dX=Pi2, Mx, My; Image img, buffImage; Graphics buffGraphics; IndexColorModel RainbowColor; boolean bClear = true; Label lbK, lbIt; TextField tfK, tfXY, tfIt, tfRand; Button btClear, btRand; public void init() { double dx = Pi2, dy = 4.; w = getSize().width; h = getSize().height; w2 = w/2; h2 = h/2; buffImage = createImage(w, h); buffGraphics = buffImage.getGraphics(); pixArr = new int[w*h]; String s=getParameter("K"); if (s != null) K = Double.valueOf(s).doubleValue(); s=getParameter("it"); if (s != null) it = Integer.parseInt(s); s=getParameter("rand"); if (s != null) rand = Integer.parseInt(s); s=getParameter("coord"); 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("MaxColor"); if (s != null) maxColor=Integer.parseInt(s); int M=maxColor/3, M2=2*M; maxColor = 3*M; // maxColor is 3*int long M4 = (long)M*M*M*M; byte rColor[] = new byte[maxColor+2], bColor[] = new byte[maxColor+2], gColor[] = new byte[maxColor+2]; for (int i = 1; i < M2; i++){ // set Color Map long dum = M - i; dum *=dum; dum *=dum; gColor[i] = bColor[(i+M) % maxColor] = rColor[(i+M2) % maxColor] = (byte)(255 - (255*dum)/M4);} rColor[0] = gColor[0] = bColor[0] = (byte)255; rColor[maxColor] = gColor[maxColor] = bColor[maxColor] = (byte)255; RainbowColor = new IndexColorModel( 8, maxColor+2, rColor,gColor,bColor); Mx = dx/w; My = dy/h; this.setLayout( new FlowLayout(FlowLayout.LEFT, 0, 0) ); tfXY = new TextField( ""+(float)Xc+" "+(float)Yc+"; "+(float)dx+ " "+(float)dy, 30); add(tfXY); lbK = new Label("K", Label.RIGHT); add(lbK); tfK = new TextField( "" + K, 5); add(tfK); tfK.addKeyListener(this); lbIt = new Label("it", Label.RIGHT); add(lbIt); tfIt = new TextField( "" + it, 5); add(tfIt); tfK.addKeyListener(this); btClear = new Button("Clear"); btClear.addActionListener(this); add(btClear); btRand = new Button("Rand"); btRand.addActionListener(this); add(btRand); tfRand = new TextField( "" + rand, 3); add(tfRand); tfRand.addKeyListener(this); addMouseListener(this); clear(); random(); } public void destroy() { removeMouseListener(this); } public void random(){ buffGraphics.setColor(Color.black); for(int i = 0; i < rand; i++){ Xo = Xc + Mx*w*(Math.random() - .5); Po = Yc + My*h*(Math.random() - .5); iterate();} } public void iterate() { double X=Xo, P=Po; for(int i = 0; i < it; i++){ P = P + K*Math.sin(X); X = X + P; while(X < 0.) X += Pi2; while(X > Pi2) X -= Pi2; int ix = w2 + (int)((X-Xc)/Mx), iy = h2 - (int)((P-Yc)/My); //System.out.println(""+X +" "+Xc+" "+dx); //System.out.println(""+kx +" "+ky); if((ix>0)&&(ix0)&&(iy 0;) pixArr[--p] %= maxColor; img = createImage(new MemoryImageSource(w, h, RainbowColor, pixArr, 0, w)); repaint(); } //System.out.println(""+Xc +" "+Yc); public void mouseClicked(MouseEvent e){} // event handling public void mousePressed(MouseEvent e) { int mx0 = e.getX() - w2, my0 = h2 - e.getY(); Xo = mx0*Mx + Xc; Po = my0*My + Yc; if ( e.isAltDown() ){ Xc = Xo; Yc = Po; My /= 2.; if ( !e.isShiftDown() ) Mx /= 2.; tfXY.setText(""+(float)Xc+" "+(float)Yc+"; "+(float)(w*Mx)+ " "+(float)(h*My) ); clear(); random(); return;} if ( e.isControlDown() ){ Xc = Xo; Yc = Po; My *= 2.; if ( !e.isShiftDown() ) Mx *= 2.; tfXY.setText(""+(float)Xc+" "+(float)Yc+"; "+(float)(w*Mx)+ " "+(float)(h*My) ); clear(); random(); return;} buffGraphics.setColor(Color.red); iterate(); } public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void actionPerformed(ActionEvent e){ if ( e.getActionCommand().equals("Clear") ){ clear(); repaint();} else random(); } public void keyTyped(KeyEvent e) {} public void keyPressed(KeyEvent e) {} public void keyReleased(KeyEvent e) { final int keyEnter = 10; if (e.getKeyCode() == keyEnter) { try{ double Ko = K; K = Double.valueOf(tfK.getText()).doubleValue(); it = Integer.parseInt(tfIt.getText()); rand = Integer.parseInt(tfRand.getText()); if (Ko != K){ clear(); random();} }catch ( NumberFormatException ne) {} } e.consume(); } public void clear(){ for (int p = h*w; p > 0;) pixArr[--p] = 0; img = createImage(new MemoryImageSource(w, h, RainbowColor, pixArr, 0, w)); buffGraphics.setColor(Color.white); buffGraphics.fillRect(0, 0, w, h); } public void paint(Graphics g) { // g.drawImage(buffImage, 0, 0, this); g.drawImage(img, 0, 0, this); showStatus("X=" + (float)Xo + " P=" + (float)Po ); } public void update(Graphics g){ paint(g); } }