Wednesday, June 16, 2010

Chaos Program - Julia Orbits

/*Author: Daniel Tanner
 *Julia Orbits Display Program
 *This program displays all the orbit diagrams for a given julia set
 *(which the user can change by clicking on the viewing window)
 *
 *Basically displaying a line following the points when you take a
 *pair of starting values for a complex number 'c' (IE, x + yi), plug
 *it into the function (z^2 + c) where z is the starting point, and then
 *plug the result back into the function until the result is > 2
 */

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class JuliaOrbit extends JPanel implements MouseListener, WindowListener {
 
 public Graphics g1;
 public Color c =Color.WHITE; // set colour to white to create the blanks
 public Random r = new Random();
 private double cX = .360248, cY = .100376;

 public static double degToRad(int deg)
 { //degree to radian conversion
  return deg * Math.PI / 180;
 }


 //paint method is automatically called for us by tree recursive (will be called 4 times! Once for each call to drawFractal
 public void paint(Graphics g)
 {
  this.g1 = g;
  g1.setColor(c);
  double initX, initY;
   //First call to do the drawing, this call draws the trunk: start(X,Y) at base, angle 0, len calculated)
  for(initX = -2.0; initX <= 2.0; initX += 0.01)
  {
      for(initY = -2.0; initY <= 2.0; initY += 0.01)
      {
          drawJuliaOrbit(cX, cY, initX, initY);  //first call
          g1.setColor(new Color((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random() * 255)));          //random colour (default is white)
      }
  }
 }

 //dragon curve method -- starts at the middle and applies rules to curve
 public void drawJuliaOrbit(double cX, double cY, double initX, double initY)
 {
    int m0 = (int)((initX + 1.0) * getWidth() / 2);
    int n0 = (int)((1.0 - initY) * getHeight() / 2);
   
    int iterations = 0;
    double q = Math.sqrt(initX * initX + initY * initY);
    while(q < 2 && iterations < 35)
    {
        iterations++;
        double y1 = 2.0 * (initX * initY);
        double x1 = (initX * initX) + -(initY * initY);
        initX = x1 + cX;
        initY = y1 + cY;
        int m1 = (int)((initX + 2.0) * getWidth() / 4);
        int n1 = (int)((2.0 - initY) * getHeight() / 4);
        g1.drawLine(m0, n0, m1, n1);
        q = Math.sqrt(initX * initX + initY * initY);
        m0 = m1; n0 = n1;
    }
 }

 //main method
 public static void main(String args[]) {
   // System.out.println("count begins with at " + count +"\n");
  JFrame FF = new JFrame("Drawing the Julia Orbit");
  FF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  FF.setVisible(true);   //Show the first drawing.
  JuliaOrbit F = new JuliaOrbit();  //call default constructor (not written); F is the Tree.
  FF.setBackground(Color.BLACK);
  FF.addMouseListener(F);
  FF.add(F);
  FF.setSize(200, 200);  //(width 1200,height 1000)   
 }

 public void mousePressed(MouseEvent e) { }

 public void mouseReleased(MouseEvent e)
 {
     if(e.getButton() == MouseEvent.BUTTON1)
     {
         cX = Double.parseDouble(JOptionPane.showInputDialog("Enter a new cX value:"));
         cY = Double.parseDouble(JOptionPane.showInputDialog("Enter a new cY value:"));
         this.repaint();
     }
 }

 public void mouseEntered(MouseEvent e) { }

 public void mouseExited(MouseEvent e) { }

 public void mouseClicked(MouseEvent e) {
 }


@Override
public void windowActivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowClosed(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowClosing(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowDeactivated(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowDeiconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowIconified(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}


@Override
public void windowOpened(WindowEvent arg0) {
    // TODO Auto-generated method stub
   
}
}

No comments:

Post a Comment