package ChaosDemos;
import java.lang.*;
import java.awt.*;
/**
* Calculates dimesion of 2D maps by box counting algorithm, uisng a fast
* sort algorithm to arrange array of finest boxes and then successively
* coarsening and weeding the array.
* Stores number of boxes to cover points in divIndex[nDiv+1]
* @version 2 August 1997
* @author Michael Cross
*/
public class boxCalculate extends Panel implements Runnable {
/* private variables */
private int[] index; // array of current boxes in index form
private int[] x; // array of current boxes in x,y form
private int number; // numebr of current boxes
private int divisions; // Number of current boxes across axis
private int nDiv=6; // Highest number of subdivisions
private int[] copy; // Working array
private int[] copy1;
private int nOld; // Position to write in output array
private boolean stopRequested;
private Button b1;
private TextField status;
private int[] occupancy;
private int q=0;
private TextField t1;
private TextField t2;
private alertDialog alert;
/* Public varibales */
/**
* Array of size nDiv+1 containing number of boxes covering attractor at each division scale
*/
public int[] divIndex;
/**
* Total nuber of points
*/
public int total;
/**
* Array contining box locations:
*
- 0 to divIndex[0]-1 for finest subdivision
*
- divIndex[0] to divIndex[1]-1 for next etc.
*
* Position (i,j) is stored as i+j*N where there are N boxes across one direction of map at
* this scale, and i and j run from 0 to N-1.
*/
public int[] output;
/**
* Array contining number of points in boxes:
* - 0 to divIndex[0]-1 for finest subdivision
*
- divIndex[0] to divIndex[1]-1 for next etc.
*
* stored as in output.
*/
public int[] boxCount;
/**
* True if calculation of box coverage is complete
*/
public boolean completed=false;
// public int updateCount=10;
/** Default constructor
* lays out button and textBox controls
*/
public boxCalculate() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
setLayout(gridbag);
constraints.weightx = 1.0;
constraints.weighty = 1.0;
constraints.gridwidth=1;
constraints.insets = new Insets(5,5,5,5);
b1 = new Button(" Stop ");
gridbag.setConstraints(b1, constraints);
add(b1);
b1.disable();
t1 = new TextField("6",3);
Label l1 = new Label("Div",Label.RIGHT);
Panel p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.RIGHT));
gridbag.setConstraints(p1, constraints);
add(p1);
p1.add(l1);
p1.add(t1);
t2 = new TextField("0",3);
Label l2 = new Label(" q ",Label.RIGHT);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
constraints.gridwidth=GridBagConstraints.REMAINDER;
gridbag.setConstraints(p2, constraints);
add(p2);
p2.add(l2);
p2.add(t2);
status = new TextField(30);
constraints.fill=GridBagConstraints.HORIZONTAL;
gridbag.setConstraints(status, constraints);
add(status);
}
/**
* Sets up calculation. Must be called before calculation thread is started.
* @param in_x Array of data in x,y pairs of ints given by box number on finest (2^nDiv)
* scale
* @param in_nDiv Number of subdivisions to make (from 0 to nDiv)
*/
public void setup (int[] in_x) {
q=parseTextField(t2,q);
nDiv=parseTextField(t1,nDiv,true);
stopRequested=false;
number = in_x.length/2;
total = number;
x= new int[2*number];
System.arraycopy(in_x,0,x,0,2*number);
divIndex = new int[nDiv+1];
nOld=0;
index = new int[number];
output = new int[1];
divisions=(int) (Math.pow(2.,(double)nDiv));
divIndex[nDiv]=0;
if(q!=0) {
occupancy = new int[number];
for(int i=0;i0) && !positive)) {
t.setText(String.valueOf(i));
if(positive) alert = new alertDialog("Must be positive");
else alert = new alertDialog("Must be negative");
return i;
}
return iNew;
}
//*********************************************************************
/**
* Parses text field known to be integer.
* Resets old value of corresponding variable if input format
* is incorrect and brings up alertDialog warning box.
* @param t field to be parsed
* @param i old value of variable
* @return new value of parameter if textbox format is correct,
* otherwise old value.
* @see alertDialog
*/
//*********************************************************************
public int parseTextField(TextField t, int i) {
int iNew;
try {
iNew=(new Integer(t.getText())).intValue();
}
catch (NumberFormatException e) {
t.setText(String.valueOf(i));
alert = new alertDialog("Try an integer");
return i;
}
return iNew;
}
//*********************************************************************
/** returns value of dimension order
* @return dimension order q
*/
public int getQ() {
q=parseTextField(t2,q);
return q;
}
//*********************************************************************
/** sets value of dimension order
* @param in_q dimension order q
*/
public void setQ(int in_q) {
t2.setText(String.valueOf(in_q));
q=in_q;
}
//*********************************************************************
/** returns value of number of subdivisions
* @return number of subdivisions
*/
public int getNDiv() {
nDiv=parseTextField(t1,nDiv,true);
return nDiv;
}
//*********************************************************************
/** sets value of number of subdivisions
* @param in_nDiv number of subdivisions
*/
public void setNDiv(int in_nDiv) {
t1.setText(String.valueOf(in_nDiv));
nDiv=in_nDiv;
}
//*********************************************************************
/** enables TextFields */
public void textBoxEnable() {
t1.enable();
t2.enable();
}
//*********************************************************************
/** disables TextFields */
public void textBoxDisable() {
t1.disable();
t2.disable();
}
}