media @ VU
[] readme course(s) preface I 1 2 II 3 4 III 5 6 7 IV 8 9 10 V 11 12 afterthought(s) appendix reference(s) example(s) resource(s) _

talk show tell print

applet-math-noise.jva

applet-math-noise.jva / applet-math-noise


  // correlated noise,  Evgeny Demidov, 8 Feb 2002
  import java.awt.*;
  import java.awt.event.*;
  public class applet-math-noise extends java.applet.Applet
   implements ItemListener, ActionListener{
    int  n = 300, n1 = n-1, Rc = 32, w, h, h2, h0, lbSize = 50;
    double p[], p1[];
    Choice chRc;    Label lbRc;    Button btNew;
  
  public void init() {
    w = getSize().width;
    h = getSize().height - lbSize;  h2 = h/2; h0 = h2 + lbSize;
    p = new double[n];     p1 = new double[n];
    for (int i = 0; i < n; i++) p[i] = 2*Math.random() - 1;
  //    p[150] = 1;
    String s=getParameter("Rc"); if (s != null) Rc = Integer.parseInt(s);
    lbRc = new Label("Rc", Label.RIGHT);  add(lbRc);
    chRc = new Choice();
    for (int i = 0, r = 1; i < 8; i++){
      chRc.addItem(Integer.toString(r));  r *= 2;}
    chRc.select("" + Rc);  chRc.addItemListener(this);  add(chRc);
    btNew = new Button("New");   btNew.addActionListener(this);
    add(btNew);
    averaging();
  }
  public void averaging(){
    double a = .5/Math.sqrt(2*Rc+1);
  //  for (int i = 0; i < Rc; i++)  p1[i] = p1[n1 - i] = 0;
    for (int i = Rc; i < n-Rc; i++){
      double sum = 0;
      for (int j = -Rc; j <= Rc; j++) sum += p[i+j];
      p1[i] = a*sum;}
  }
  
  public void paint(Graphics g) {
    g.setColor(Color.white);   g.fillRect(0, 0, w, h+lbSize);
    g.setColor(Color.black);   g.drawLine(0,h0 ,w,h0);
    int x = 0, x0 = 0, y, y0 = (int)(h2*p[0]) + h0;
    g.setColor(new Color(200,200,255));
    for (int i = 1; i < n; i++){
      x += 2;  y = (int)(h2*p[i]) + h0;
      g.drawLine(x0,y0 ,x,y);
      x0 = x;  y0 = y;}
    g.setColor(Color.red);
    x = x0 = 2*Rc;  y0 = (int)(h2*p1[Rc]) + h0;
    for (int i = Rc+1; i < n-Rc; i++){
      x += 2;  y = (int)(h2*p1[i]) + h0;
      g.drawLine(x0,y0 ,x,y);
      x0 = x;  y0 = y;}
  }
  
  public void itemStateChanged(ItemEvent e){
    Rc = Integer.parseInt(chRc.getSelectedItem());
    averaging();
    repaint();
  }
  public void actionPerformed(ActionEvent e){
    for (int i = 0; i < n; i++) p[i] = 2*Math.random() - 1;
    averaging();
    repaint();
  }
  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.