Two Dimensional Divergence Applet
import java.awt.*;
import java.applet.*;
import java.lang.*;
import expr.*;
public class Divergence2 extends Applet
{
Panel controls1 = new Panel();
Panel controls2 = new Panel();
Panel controls3 = new Panel();
Panel controls4 = new Panel();
private TextField PText = new TextField("x^2", 35);
private TextField QText = new TextField("y^2", 35);
private TextField xMinText = new TextField("-1", 10);
private TextField xMaxText = new TextField("1", 10);
private TextField yMinText = new TextField("-1", 10);
private TextField yMaxText = new TextField("1", 10);
Expr Pequation;
Expr Qequation;
Variable x = Variable.make("x");
Variable y = Variable.make("y");
Variable Picon = Variable.make("Pi");
Variable PIcon = Variable.make("PI");
Variable picon = Variable.make("pi");
Variable Econ = Variable.make("E");
Variable econ = Variable.make("e");
String UserInput;
double xmin, xmax, ymin, ymax, absmax;
Graphics sG;
Graphics bG;
Image bI;
Color black = new Color(0, 0, 0);
Color back = new Color(247, 243, 234);
Color lines = new Color(128, 128, 255);
Color white = new Color(255, 255, 255);
int width = 423; // applet width and height
int top = 175; // height of text panels
int left = 90; // left margin
int steps = 50; // number of frames in animation
int n = 60; // display is 61 by 61
int grid = 4; // courseness is 4
double[][] foo = new double[61][61];
double[][] rho = new double[61][61];
double xx, yy, p1, p2, q1, q2, dx, dy;
public void init()
{
setBackground(back);
bI = createImage(this.size().width, this.size().height);
bG = bI.getGraphics();
drawback();
picon.set_value(Math.PI);
Picon.set_value(Math.PI);
PIcon.set_value(Math.PI);
econ.set_value(Math.E);
Econ.set_value(Math.E);
controls1.setBackground(back);
controls1.setFont(new Font("Courier", Font.BOLD, 12));
controls1.add(new Label(" P(x, y): "));
controls1.add(PText);
controls2.setBackground(back);
controls2.setFont(new Font("Courier", Font.BOLD, 12));
controls2.add(new Label(" Q(x, y): "));
controls2.add(QText);
controls3.setBackground(back);
controls3.setFont(new Font("Courier", Font.BOLD, 12));
controls3.add(new Label(" Min x:"));
controls3.add(xMinText);
controls3.add(new Label(" Max x:"));
controls3.add(xMaxText);
controls4.setBackground(back);
controls4.setFont(new Font("Courier", Font.BOLD, 12));
controls4.add(new Label(" Min y:"));
controls4.add(yMinText);
controls4.add(new Label(" Max y:"));
controls4.add(yMaxText);
add(controls1);
add(controls2);
add(controls3);
add(controls4);
}
public void paint(Graphics g)
{
g.drawImage(bI, 0, top, this);
sG = getGraphics();
}
public void update(Graphics g)
{
g.drawImage(bI, 0, top, this);
}
public void drawback()
{
bG.setColor(back);
bG.fillRect(0, 0, width, width - top);
bG.setColor(black);
for (int i = 0; i < (n + 1) * grid + 1; i = i + grid)
{
bG.drawLine(left, i, left + (n + 1) * grid, i);
bG.drawLine(left + i, 0, left + i, (n + 1) * grid);
}
}
public Color boxcolor(double z)
{
Color tcolor;
int red, blue;
if (z >= 0.0)
{
z = Math.min(z, 1.0);
blue = (int) Math.round(255.0 * (1 - z));
tcolor = new Color(255, blue, blue);
}
else
{
z = Math.max(z, -1.0);
red = (int) Math.round(255.0 * (z + 1));
tcolor = new Color(red, red, 255);
}
return tcolor;
}
public boolean mouseDown(Event evt, int mx, int my)
{
if (my < top) return true;
// Get Functions and Ranges from Panels
try
{
Pequation = Parser.parse(PText.getText());
}
catch (Syntax_error e)
{
System.err.println ("Syntax error: " + e);
}
try
{
Qequation = Parser.parse(QText.getText());
}
catch (Syntax_error e)
{
System.err.println ("Syntax error: " + e);
}
UserInput = xMinText.getText().trim();
xmin = Double.valueOf(UserInput).doubleValue();
UserInput = xMaxText.getText().trim();
xmax = Double.valueOf(UserInput).doubleValue();
UserInput = yMinText.getText().trim();
ymin = Double.valueOf(UserInput).doubleValue();
UserInput = yMaxText.getText().trim();
ymax = Double.valueOf(UserInput).doubleValue();
drawback();
sG.drawImage(bI, 0, top, this);
dx = (xmax - xmin)/n;
dy = (ymax - ymin)/n;
bG.setColor(boxcolor(0.0));
absmax = 0.0;
xx = xmin;
for (int i = 0; i < n + 1; i = i + 1)
{
yy = ymax;
for (int j = 0; j < n + 1; j = j + 1)
{
rho[i][j] = 0.0;
x.set_value(xx - dx/2);
y.set_value(yy);
p1 = Pequation.value();
x.set_value(xx + dx/2);
p2 = Pequation.value();
x.set_value(xx);
y.set_value(yy - dx/2);
q1 = Qequation.value();
y.set_value(yy + dx/2);
q2 = Qequation.value();
foo[i][j] = (p2 - p1 + q2 - q1)/dx;
absmax = Math.max(absmax, Math.abs(foo[i][j]));
bG.fillRect(left + 1 + grid * i, 1 + grid * j,
grid - 1, grid - 1);
yy = yy - dy;
}
sG.drawImage(bI, 0, top, this);
xx = xx + dx;
}
if (absmax > 0.0001)
{
bG.setColor(back);
bG.fillRect(left, 0, (n + 1) * grid + 1, (n + 1) * grid + 1);
for (int step = 0; step < steps; step = step + 1)
{
for (int i = 0; i < n + 1; i = i + 1)
{
for (int j = 0; j < n + 1; j = j + 1)
{
rho[i][j] = rho[i][j] + foo[i][j] / (absmax * steps);
bG.setColor(boxcolor(rho[i][j]));
bG.fillRect(left + grid * i, grid * j,
grid, grid);
}
}
sG.drawImage(bI, 0, top, this);
}
}
return true;
}
}
Copyright c 1995 by
Frank Wattenberg, Department of Mathematics, Montana State University,
Bozeman, MT 59717