home

shop

quotes

optical illusions

tutor

about

links

links

     
MathImage    shop_logo

    


/*
RadialWaves.java
Any portion of this code may be used without permission from BrainyPage.
www.BrainyPage.com | Created by Raul Aguilar | Last Modified 200205032153

Enjoy!
*/

import java.awt.*;
import javax.swing.*;

import java.awt.event.*;
import java.awt.geom.*;
import java.math.*;

import java.applet.*;
import com.borland.jbcl.layout.*;

import java.util.*;


public class RadialWaves extends JApplet{

  private aJFrame f;
  public static final int WIDTH = 400;
  public static final int HEIGHT = 400;

  public void init() {
    String[] args = { "80" };
    f = new aJFrame(args, getContentPane());
  }

  public void paint(Graphics g) {
    super.paint(g);
    f.redraw();
  }

  public static void main(String[] args) {
    aJFrame frame = new aJFrame(args);
    frame.setTitle("RadialWaves");
    frame.setSize(WIDTH,HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.show();
  }
}

class aJFrame extends JFrame {

  public aJFrame(String[] args) {
    buildPanels(args, getContentPane());
  }

  public aJFrame(String[] args, Container cp) {
    buildPanels(args, cp);
  }

  public void buildPanels(String[] args, Container cp) {

    panelSimulate = new Simulate(args);
    Control panelControl = new Control(args, panelSimulate);

    JPanel p = new JPanel();
    p.setLayout(new XYLayout());
    p.add(panelSimulate, new XYConstraints(5, 5, 380, 335));
    p.add(panelControl, new XYConstraints(5, 345, 380, 90));
    cp.add(p,BorderLayout.CENTER);
  }

  public void redraw() {
    panelSimulate.repaint();
  }

  Simulate panelSimulate;
}

class Simulate extends JPanel {

  private int separation;
  private String[] args;

  public Simulate(String[] args) {
    setParameters(args);

    //Initialize drawing colors
    setBackground(Color.white);
    setForeground(Color.black);
  }

  public void setParameters(String controlValues) {
    StringTokenizer st = new StringTokenizer(controlValues);
    ArrayList al = new ArrayList();
    while (st.hasMoreTokens()) {
      al.add((String) st.nextToken());
    }
    String[] s = (String[]) al.toArray(new String[0]);
    setParameters(s);
  }

  public void setParameters(String[] args) {
    separation = Integer.parseInt(args[0]);

    System.out.println("parameter values:");
    System.out.println();
    System.out.println("separation = " + separation);
  }

  public void paint(Graphics g) {

    System.out.println("separation:" + separation);

    super.paintComponent(g);

    Graphics2D g2 = (Graphics2D) g;

    /*
    // new image test

    int newColorR = (int) (java.lang.Math.random() * 255);
    int newColorG = (int) (java.lang.Math.random() * 255);
    int newColorB = (int) (java.lang.Math.random() * 255);

    g2.setPaint(new Color(newColorR, newColorG, newColorB));
    g2.fill(new Rectangle2D.Double(20, 20, 20, 20));
    */

    // find the width and height of the JPanel
    Dimension d = getSize();
    int w = d.width;
    int h = d.height;

    // the radial wave will occupy
    //    1/3 width from left edge to left radial wave center
    //    1/3 width between radial wave centers
    //    and 1/3 width between right radial wave to right edge
    double dx = w / 3.0;

    // set the center of the left radial wave
    double cx1, cy1;
    cx1 = w / 2.0 - separation / 2.0;
    cy1 = h / 2.0;

    // set the center of the right radial wave
    double cx2, cy2;
    cx2 = w / 2.0 + separation / 2.0;
    cy2 = h / 2.0;

    // set the luminance
    double low = 0.0;
    double high = 255.0;

    // set the wavelength
    double numberOfWaves = 4.0;
    double pixelsPerWave = 31;

    // waves outside of the outRadius pixel length are not shown
    double outRadius = pixelsPerWave * numberOfWaves;

    // horizontal scan
    for (int x = 0; x < w; x+=1) {

      // vertical scan
      for (int y = 0; y < h; y+=1) {

        // find left radial distance from current matrix scan
        double dx1 = x - cx1;
        double dy1 = y - cy1;
        double d1 = Math.sqrt( dx1*dx1 + dy1*dy1 );

        // find right radial distance from current matrix scan
        double dx2 = x - cx2;
        double dy2 = y - cy2;
        double d2 = Math.sqrt( dx2*dx2 + dy2*dy2 );

        if ((d1 < outRadius) || (d2 < outRadius)) {

          // normalize the [-1, 1] sine wave variations to [0, 1]
          double wave1 = Math.sin(d1 / numberOfWaves);
          double normalWave1 = (wave1 + 1.0) / 2.0;
          double wave2 = Math.sin(d2 / numberOfWaves);
          double normalWave2 = (wave2 + 1.0) / 2.0;

          // normalize the superposition of the two waves from [0, 2] to [0, 1]
          double normalWave = (normalWave1 + normalWave2) / 2.0;

          // map sine interference from [0, 1] to luminance range
          double intensity = (high - low) * normalWave + low;

          // get integer representation
          int c = (int) intensity;

          // render the color from a nominal saturation of cyan
          // (green and blue)
          g2.setPaint(new Color(c, 255, 255));
          g2.fill(new Rectangle2D.Double(x, y, 1, 1));
        }
      }
    }
  }
}

class Control extends JPanel {

  private String[] args;
  private JTextField tf;
  private Simulate s;

  public Control(String[] args, Simulate sp) {

    this.args = args;
    this.setLayout(new XYLayout());

    s = sp;

    JButton b = new JButton("Go");
    ActionListener bl = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        s.setParameters(tf.getText().trim());
        s.repaint();
      }
    };
    b.addActionListener(bl);
    add(b, new XYConstraints(0, 3, 50, 20));

    tf = new JTextField("80",5);
    add(tf, new XYConstraints(60, 3, 320, 20));
  }
}