import java.awt.*;
import java.applet.*;
import java.lang.*;
public class Sir extends Applet implements Runnable
{
Thread runner;
Graphics bG; // Graphics buffer
Graphics sG; // Display
Image bI;
Color background = new Color(0, 0, 0);
Color inside = new Color(255, 255, 255);
Color features = new Color(0, 0, 0);
Color susceptibleColor = new Color(255, 96, 96);
Color carrierColor = new Color(192, 255, 192);
Color symptomaticColor = new Color(0, 192, 0);
Color recoveredColor = new Color(192, 192, 255);
Color infectedgraph = new Color(0, 192, 0);
Color susceptiblegraph = new Color(255, 0, 0);
Color recoveredgraph = new Color(0, 0, 255);
Color black = new Color(0, 0, 0);
Font font = new Font("Helvetica", Font.BOLD, 12);
int duration = 30;
int contacts = 200;
int olds = 95;
int oldi = 5;
int oldr = 0;
int news, newi, newr;
double prob = 0.75;
int day[] = new int[101];
int i, j, k, n;
int running = 0;
public void init()
{
bI = createImage(this.size().width, this.size().height);
bG = bI.getGraphics();
bG.setFont(font);
drawbackground();
for (i = 0; i < 100; i = i + 1)
{ day[i] = 0;
}
for (i = 0; i < 100; i = i + 1)
{ drawface(i);
}
bG.setColor(black);
bG.drawString("Click anywhere to start", 275, 105);
}
public void start()
{
if (runner == null)
{
runner = new Thread(this);
runner.start();
}
}
public void run()
{
}
public void paint(Graphics g)
{
g.drawImage(bI, 0, 0, this);
sG = getGraphics();
}
public void update(Graphics g)
{
g.drawImage(bI, 0, 0, this);
}
public void drawbackground()
{
bG.setColor(background);
bG.fillRect(0, 0, 501, 201);
bG.setColor(inside);
bG.fillRect(10, 10, 181, 181);
bG.fillRect(201, 10, 291, 181);
olds = 95;
oldi = 5;
oldr = 0;
bG.setColor(infectedgraph);
bG.drawString("Infected", 430, 170);
bG.setColor(recoveredgraph);
bG.drawString("Recovered", 415, 30);
bG.setColor(susceptiblegraph);
bG.drawString("Susceptible", 230, 30);
}
public boolean mouseDown(Event evt, int x, int y)
{ if (running == 0)
{ drawbackground();
simulate();
}
return true;
}
public void simulate()
{
running = 1;
for (i = 0; i < 100; i = i + 1)
{ day[i] = 0;
}
for (i = 0; i < 5; i = i + 1)
{ j = (int) java.lang.Math.round(100 * java.lang.Math.random());
day[j] = 1;
}
for (i = 0; i < 100; i = i + 1)
{ drawface(i);
}
sG.drawImage(bI, 0, 0, this);
for (n = 0; n < duration; n = n + 1)
{
try
{ runner.sleep(200);
}
catch(InterruptedException e) {};
for (i = 0; i < 100; i = i + 1)
{ if (day[i] > 0)
{ day[i] = day[i] + 1;
}
}
for (k = 0; k < contacts; k = k + 1)
{ i = (int) java.lang.Math.round(100 * java.lang.Math.random());
j = (int) java.lang.Math.round(100 * java.lang.Math.random());
if ((day[j] == 0) && (day[i] < 5) && (day[i] > 0) &&
(java.lang.Math.random() > prob))
{ day[j] = 1;
}
}
news = 0;
newi = 0;
newr = 0;
for (i = 0; i < 100; i = i + 1)
{ drawface(i);
if (day[i] == 0)
{ news = news + 1;
}
if ((day[i] > 0) && (day[i] < 7))
{ newi = newi + 1;
}
if (day[i] > 6)
{ newr = newr + 1;
}
}
bG.setColor(susceptiblegraph);
bG.drawLine(xscale(n), yscale(olds),
xscale(n + 1), yscale(news));
bG.drawLine(xscale(n), yscale(olds)-1,
xscale(n + 1), yscale(news)-1);
bG.setColor(recoveredgraph);
bG.drawLine(xscale(n), yscale(oldr),
xscale(n + 1), yscale(newr));
bG.drawLine(xscale(n), yscale(oldr)-1,
xscale(n + 1), yscale(newr)-1);
bG.setColor(infectedgraph);
bG.drawLine(xscale(n), yscale(oldi),
xscale(n + 1), yscale(newi));
bG.drawLine(xscale(n), yscale(oldi)-1,
xscale(n + 1), yscale(newi)-1);
olds = news;
oldi = newi;
oldr = newr;
sG.drawImage(bI, 0, 0, this);
}
running = 0;
}
public int yscale(int ys)
{ return (int) java.lang.Math.round(190 - ys * 1.8);
}
public int xscale(int xs)
{ return (int) java.lang.Math.round(201 + (280.0/(duration-1)) * xs);
}
public void drawface(int person)
{
int sx, sy;
sx = person / 10;
sy = person % 10;
bG.setColor(recoveredColor);
if (day[person] < 7)
{ bG.setColor(symptomaticColor);
}
if (day[person] == 0)
{ bG.setColor(susceptibleColor);
}
if (day[person] == 1)
{ bG.setColor(carrierColor);
}
bG.fillOval(18 * sy + 14, 18 * sx + 12, 9, 13);
}
}
Copyright c 1995 by Frank Wattenberg Department of Mathematics, Montana State University, Bozeman, MT 59717.