"Compiler" applet
Other applets
You can see this applet in work on the top of my
ComPas page. Applet draws a symbolic
paper list with the text of the program, a computer and simulates the process
of translation from program text to binary codes.
The animation can be paused.
"Compiler" applet is a modification of more simple "Welcome!" applet,
so if You want to learn, how it works, I recommed You to begin from "Welcome!".
You can use this applet source and change it free, but You must conserve the lines about the author.
Please, send me URL of Your page with applet to E-mail address
eremin@hotbot.com.
/* (C) Evgeny Eremin (Perm, Russia)
http://pages.hotbot.com/edu/eremin
You can use this applet source and change it free,
but You must conserve this lines about the author!
Add information about Your changes after these lines!
Please, send URL of Your page with applet to my E-mail
eremin@hotbot.com
*/
/* Applet draws a symbolic paper list with the text of the program,
a computer and simulates the process of translation from program text
to binary codes.
The animation can be paused.
*/
import java.applet.*;
import java.awt.*;
public class compiler extends java.applet.Applet implements Runnable{
Thread engine = null; int timeCounter=0; boolean animate=true;
int x0=300; int y0=5; //computer
int Lmon=80; int Hmon=60; int Hsys=20;
int x1=7; //listing
int Llist=70; int Hlist=80; int u=15;
Button pauseButton = new Button("Pause");
//Initialize the applet
public void init() {
setLayout(null); setBackground(Color.white);
add(pauseButton); pauseButton.reshape(165,65,50,25);
//===== Tread that makes animation =====
//create tread
engine = new Thread(this); engine.start();
engine.suspend(); //pause in thread (see start after)
}
public void start(){
engine.resume(); //restart thread
}
public void run(){ //work of the thread - animation engine
while (true)
{
//add current letter or wait
if (animate) timeCounter++;
else timeCounter=29; //no animation
repaint(); //show picture changes
if (timeCounter>=30)
timeCounter=-1; //repeat again
//delay
try {engine.sleep(1000);} catch(InterruptedException e) {}
}
}
public void stop(){
engine.suspend(); //pause the thread
}
public void destroy(){
if (engine != null) {engine.stop(); engine = null;}
}
//Get Applet information
public String getAppletInfo() {
return "E.A.Eremin: Welcome Applet";
}
public void update(Graphics g){
if (animate)
g.setColor(Color.green); //green indicator
else g.setColor(Color.red); //red indicator
g.fillRect(x0+7,y0+Hmon+9,4,2);
if ( (!animate) | (timeCounter>16 ) )
return; //no animation and "all is on screen" pause
if (timeCounter==0) clr(g); //clear all
else myDraw(g,timeCounter);
}
void clr(Graphics g){
//clear listing
g.setColor(Color.white); g.fillRect(x1+5,y0+5,Llist-u-5,Hlist-10);
//clear arrows and code
g.fillRect(90,20,200,60);
//clear display screen
g.setColor(Color.cyan); g.fillRect(x0+5,y0+5,Lmon-9,Hmon-11);
}
void myDraw(Graphics g, int n){
for (int i=1; i<=n; i++)
{switch (i) {
case 1: g.setColor(Color.black);
g.setFont(new Font("Arial",Font.BOLD,12));
g.drawString("program",x1+5,y0+15);
break;
case 2: g.drawString("begin",x1+5,y0+35);
break;
case 3: g.drawString("...",x1+5,y0+50);
break;
case 4: g.drawString("end.",x1+5,y0+Hlist-5);
break;
case 5: drawArrow(g,90,40,Color.blue);
if (timeCounter<12)
g.drawString("compiling...",160,30);
break;
case 6: drawCode(g,145,55,"0");
break;
case 7: drawCode(g,145,55,"01");
break;
case 8: drawCode(g,145,55,"010");
break;
case 9: drawCode(g,145,55,"0101");
break;
case 10:drawCode(g,145,55,"0101...");
break;
case 11:drawCode(g,145,55,"0101...0");
break;
case 12:g.setColor(Color.white);
g.fillRect(145,15,100,20);
g.setColor(Color.black);
drawArrow(g,240,40,Color.blue);
break;
case 13:g.drawString("Hello,",x0+10,y0+22);
break;
case 14:g.drawString(" world!",x0+10,y0+42);
break;
}
}
}
void drawArrow(Graphics g, int x, int y, Color c){
Color old = g.getColor(); //save color
g.setColor(c);
for (int i=0; i<30; i++) g.drawLine(x+i,y,x+i,y+15);
for (int i=0; i<18; i++) g.drawLine(x+30+i,y-10+i,x+30+i,y+25-i);
g.setColor(old); //restore color
}
void drawCode(Graphics g, int x, int y, String s){
//save color and font
Color old = g.getColor(); Font f = g.getFont();
//draw
g.setColor(Color.blue);
g.setFont(new Font("Arial",Font.BOLD,24));
g.drawString(s,x,y);
//restore color and font
g.setColor(old); g.setFont(f);
}
void drawListing(Graphics g){
int xa[] = {x1+Llist-u, x1, x1, x1+Llist, x1+Llist, x1+Llist-u, x1+Llist-u, x1+Llist};
int ya[] = {y0, y0, y0+Hlist, y0+Hlist, y0+u, y0, y0+u, y0+u};
g.drawPolygon(xa,ya,8);
for (int k=0; k<8; k++) {xa[k]++; ya[k]++;}
g.drawPolygon(xa,ya,8);
}
public void drawComp(Graphics g){
// ===== system unit =====
//system: front
g.setColor(Color.lightGray); g.fillRect(x0,y0+Hmon+3,Lmon,Hsys);
g.setColor(Color.black); g.drawRect(x0,y0+Hmon+3,Lmon,Hsys);
//shaddow
g.setColor(Color.white);
g.drawLine(x0+1,y0+Hmon+4,x0+Lmon-1,y0+Hmon+4);
g.drawLine(x0+1,y0+Hmon+4,x0+1,y0+Hmon+Hsys+2);
//indicator
g.setColor(Color.black); g.drawRect(x0+6,y0+Hmon+8,5,3);
g.setColor(Color.green); //green indicator
g.fillRect(x0+7,y0+Hmon+9,4,2);
//2 floppy
int xl=x0+Lmon-25; int y1=y0+Hmon+9;
for (int k=1; k<3; k++)
{g.setColor(Color.darkGray); g.fillRect(xl,y1,21,4);
g.setColor(Color.white); g.fillRect(xl,y1,20,3);
y1+=6;}
//system: sides
g.setColor(Color.black);
g.drawLine(x0+Lmon+1,y0+Hmon+Hsys+3,x0+Lmon+10,y0+Hmon+Hsys-6);
g.drawLine(x0,y0+Hmon+3,x0+9,y0+Hmon-6);
g.drawLine(x0+Lmon+11,y0+Hmon-5,x0+Lmon+11,y0+Hmon+Hsys-7);
g.setColor(Color.white);
g.drawLine(x0+Lmon+1,y0+Hmon+3,x0+Lmon+10,y0+Hmon-6);
//system: top
g.setColor(Color.darkGray);
for (int i2=1; i2<11; i2++)
g.drawLine(x0+1+i2,y0+Hmon+3-i2,x0+Lmon+i2,y0+Hmon+3-i2);
//system: right
g.setColor(Color.darkGray);
for (int i3=1; i3<11; i3++)
g.drawLine(x0+Lmon+i3,y0+Hmon+5-i3,x0+Lmon+i3,y0+Hmon+Hsys+3-i3);
// ===== monitor =====
//monitor: front
g.setColor(Color.lightGray); g.fillRect(x0,y0,Lmon,Hmon);
g.setColor(Color.black); g.drawRect(x0,y0,Lmon,Hmon);
//shaddow
g.setColor(Color.white);
g.drawLine(x0+1,y0+1,x0+Lmon-1,y0+1);
g.drawLine(x0+1,y0+1,x0+1,y0+Hmon-1);
//monitor: screen
g.setColor(Color.black); g.drawRect(x0+4,y0+4,Lmon-8,Hmon-10);
g.setColor(Color.cyan); g.fillRect(x0+5,y0+5,Lmon-9,Hmon-11);
//shaddow
g.setColor(Color.white);
g.drawLine(x0+Lmon-3,y0+4,x0+Lmon-3,y0+Hmon-5);
g.drawLine(x0+4,y0+Hmon-5,x0+Lmon-4,y0+Hmon-5);
//monitor: sides
g.setColor(Color.black);
g.drawLine(x0+Lmon+1,y0,x0+Lmon+10,y0+9);
g.drawLine(x0+Lmon+1,y0+Hmon,x0+Lmon+10,y0+Hmon-9);
g.drawLine(x0+Lmon+10,y0+9,x0+Lmon+10,y0+Hmon-9);
g.setColor(Color.darkGray);
for (int i1=1; i1<10; i1++)
g.drawLine(x0+Lmon+i1,y0+i1,x0+Lmon+i1,y0+Hmon-i1);
}
public void paint(Graphics g){
int h=size().height; int w=size().width;
g.setColor(Color.red);
g.drawRect(0,0,w-1,h-1); g.drawRect(1,1,w-3,h-3); //frame
drawComp(g); drawListing(g);
}
//Controls
public boolean action(Event e, Object o) {
if (e.target instanceof Button)
animate=!animate; //change to opposite
return(true);
}
}