/*
    This class is a basic extension of the Applet class.  It would generally be
    used as the main class with a Java browser or the AppletViewer.  But an instance
    can be added to a subclass of Container.  To use this applet with a browser or
    the AppletViewer, create an html file with the following code:

    <HTML>
    <HEAD>
    <TITLE> A simple program </TITLE>
    </HEAD>
    <BODY>

    <APPLET CODE="camila97.class" WIDTH=332 HEIGHT=169></APPLET>

    </BODY>

    </HTML>

    You can add controls to camDraw with Cafe Studio.
    (Menus can be added only to subclasses of Frame.)
 */

import java.awt.*;
import java.applet.*;

class ImagePanel extends Panel {
    Image image;

    public ImagePanel(Applet applet,String image) {
        this.image = applet.getImage(applet.getDocumentBase(),image);
    }
    public void paint(Graphics g) {
        Rectangle r = bounds();

        g.drawImage(image,0,0,r.width,r.height,this);
    }
}

class ImageCanvas extends Canvas {
    public ImageCanvas() {
        reshape(0,0,501,301);
    }
    public void drawArrow(Graphics g,int x1,int y1,int x2,int y2) {
        int ARROWSIZE = 10;
        double ARROWANGLE = Math.PI/6;

	    Point AL,AR;
	    double px,py;
	    double alpha,alphaL,alphaR;
	    double Alx,Aly,Arx,Ary;

	    px = x2 - x1;
	    py = y1 - y2;
	    // Calculating alpha, alphaL, alphaR...
	    if (px == 0) alpha = (py < 0) ? Math.PI/2 : -Math.PI/2;
	    else alpha = Math.atan(py / px);
	    // Calculating alphaL, alphaR...
	    alphaL = alpha - ARROWANGLE;
	    alphaR = alpha + ARROWANGLE;
	    // Calculating arrow projections...
	    Alx = Math.cos(alphaL) * ARROWSIZE;
	    Aly = Math.sin(alphaL) * ARROWSIZE;
	    Arx = Math.cos(alphaR) * ARROWSIZE;
	    Ary = Math.sin(alphaR) * ARROWSIZE;
	    // Calculating arrow points...
	    if (px > 0) 
	    {
		    AL = new Point((int)x2 - (int)(Alx),(int)y2 + (int)(Aly));
		    AR = new Point((int)x2 - (int)(Arx),(int)y2 + (int)(Ary));
	    }
	    else
	    {
		    AL = new Point((int)x2 + (int)(Alx),(int)y2 - (int)(Aly));
		    AR = new Point((int)x2 + (int)(Arx),(int)y2 - (int)(Ary));
	    }
	    // Drawing...
	    g.drawLine(x1,y1,x2,y2);
	    g.drawLine(AL.x,AL.y,x2,y2);
	    g.drawLine(AR.x,AR.y,x2,y2);
    }
    public void drawRSC(Graphics g) {
        Rectangle r = bounds();
        //int hlines = r.height / 10;
        //int vlines = r.width / 10;
/*
        g.draw3DRect(0, 0, r.width-1, r.height-1,true);
        g.drawLine(0, 0, r.width, r.height);
        g.drawLine(r.width, 0, 0, r.height);
        g.drawLine(0, r.height / 2, r.width, r.height / 2);
        g.drawLine(r.width / 2, 0, r.width / 2, r.height);
*/
        // CAMILA source code generation
g.drawRect(150,0,200,300);
g.drawString("BREL",234,150);
g.drawString("(A*B)-set",218,225);
drawArrow(g,0,37,150,37);
g.drawString("EMPTY  2 ",0,27);
g.drawLine(0,74,150,74);
g.drawString("EQ (A*B)-set 2 ",0,64);
g.drawLine(0,111,150,111);
g.drawString("GET  A*B ",0,101);
g.drawLine(0,148,150,148);
g.drawString("IN A*B 2 ",0,138);
g.drawLine(0,185,150,185);
g.drawString("INIT   ",0,175);
g.drawLine(0,222,150,222);
g.drawString("INS A*B  ",0,212);
g.drawLine(0,259,150,259);
g.drawString("NOP   ",0,249);
g.drawLine(350,37,500,37);
g.drawString("RD  (A*B)-set ",388,27);
g.drawLine(350,74,500,74);
g.drawString("REM A*B  ",428,64);
g.drawLine(350,111,500,111);
g.drawString("REML B  ",436,101);
g.drawLine(350,148,500,148);
g.drawString("REMR A  ",436,138);
g.drawLine(350,185,500,185);
g.drawString("SELL B A-set ",396,175);
g.drawLine(350,222,500,222);
g.drawString("SELR A B-set ",396,212);
g.drawLine(350,259,500,259);
g.drawString("WR (A*B)-set  ",388,249);
    }
    public void paint(Graphics g) {
/*
        if (focus) {
            g.setColor(Color.red);
        } else {
            g.setColor(Color.darkGray);
        }
*/
        drawRSC(g);
    }
}

public class camDraw extends Applet {

    public void init() {

        super.init();

        //{{INIT_CONTROLS
        //setLayout();
        add(new ImageCanvas());
        resize(preferredSize());
        //}}
    }

    public boolean handleEvent(Event event) {
        return super.handleEvent(event);
    }

    //{{DECLARE_CONTROLS
    //Label lblCopyright;
    //}}

}
