Applet Code
Supply Function with Varying Cost
import java.awt.*;
import java.applet.*;
import java.lang.*;
public class SupplyCostC extends Applet
{
Panel tpanel = new Panel();
private TextField tfield = new TextField("0.40");
String ttext = "0.40";
double t = 0.4;
Graphics sG;
Graphics bG;
Image bI;
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
Color back = new Color(247, 243, 234);
Color grid = new Color(64, 255, 64);
Color red = new Color(255, 64, 64);
Color blue = new Color(96, 96, 255);
Color gray = new Color(64, 64, 64);
int width = 440;
int wholewidth = 441;
int height = 200;
int wholeheight = 201;
int top = 40;
int margin = 20;
int px, py;
public void init()
{
bI = createImage(wholewidth, wholeheight + 2 * margin);
bG = bI.getGraphics();
drawback();
setBackground(back);
setFont(new Font("Courier", Font.BOLD, 12));
tpanel.setBackground(back);
tpanel.setFont(new Font("Courier", Font.BOLD, 12));
tpanel.add(new Label(" c: "));
tpanel.add(tfield);
tpanel.add(new Button("Move"));
add(tpanel);
}
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, wholewidth, wholeheight + top);
bG.setColor(black);
bG.drawLine(xscale(0.0), yscale(0.0), xscale(5.0), yscale(0.0));
bG.drawLine(xscale(0.0), yscale(0.0), xscale(0.0), yscale(5000.0));
for (int i = 0; i < 11; i = i + 1)
{
bG.drawLine(xscale(0.0) - 2, yscale(0.0 + i * 500.0),
xscale(0.0) + 2, yscale(0.0 + i * 500.0));
bG.drawLine(xscale(0.0 + i * 0.50), yscale(0.0) - 2,
xscale(0.0 + i * 0.50), yscale(0.0) + 2);
}
bG.drawLine(xscale(t), yscale(0.0), xscale(5.0),
yscale(1000.0 * (5.0 - t)));
}
public int xscale(double x)
{
int ix;
ix = (int) Math.round(margin + 80.0 * x);
return ix;
}
public int yscale(double y)
{
int iy;
iy = (int) Math.round(margin + height * (1 - y/5000));
return iy;
}
public boolean mouseDown(Event evt, int mx, int my)
{
if ((my > top + margin + height - 5) &&
(my < top + margin + height + 5))
{
t = (mx - margin)/80.0;
tfield.setText(java.lang.Double.toString(t));
}
else
{
ttext = tfield.getText().trim();
t = Double.valueOf(ttext).doubleValue();
}
drawback();
sG.drawImage(bI, 0, top, this);
return true;
}
public boolean action(Event evt, Object arg)
{
if (arg.equals("Move"))
{
ttext = tfield.getText().trim();
t = Double.valueOf(ttext).doubleValue();
drawback();
sG.drawImage(bI, 0, top, this);
}
return true;
}
}
Copyright c 1995 by
Frank Wattenberg, Department of Mathematics, Montana State University,
Bozeman, MT 59717