import java.awt.*;
import java.awt.event.*;
 
public class eutocodegraphics extends java.applet.Applet implements
                                                     ActionListener
 
// Software written by William Overington
// WOverington@ngo.globalnet.co.uk
// 12 December 2002
// Also the additions of 14 December 2002 for polygons
// Also the additions of 14 December 2002 for colour 16
// Copyright 2002 William Overington
//
// This software is an applet which is for demonstrating the use of
// eutocode graphics
 
  {
    int obeycode=1;
 
    Button go1Button = new Button("draw");
    Button go2Button = new Button("clear");
 
    TextField text1TextField = new TextField(80);
 
    String text1="";
 
    char eutocode='?';
 
// Start of the declarations for the eutocode engine
//
    int eutocode_engine_A=0;
//
    int eutocode_engine_X1L=0;
    int eutocode_engine_Y1L=0;
    int eutocode_engine_X2L=0;
    int eutocode_engine_Y2L=0;
//
    int eutocode_engine_X1H=0;
    int eutocode_engine_Y1H=0;
    int eutocode_engine_X2H=0;
    int eutocode_engine_Y2H=0;
//
    int eutocode_engine_X1=0;
    int eutocode_engine_Y1=0;
    int eutocode_engine_X2=0;
    int eutocode_engine_Y2=0;
//
    int eutocode_engine_COLOUR=0;
//
// Start of the additions of 14 December 2002 for polygons
//
    int eutocode_engine_POLYGON_POINTER=0;
    int[] eutocode_engine_POLYGON_XL= new int[101];
    int[] eutocode_engine_POLYGON_YL= new int[101];
    int[] eutocode_engine_POLYGON_XH= new int[101];
    int[] eutocode_engine_POLYGON_YH= new int[101];
    int[] eutocode_engine_POLYGON_X= new int[101];
    int[] eutocode_engine_POLYGON_Y= new int[101];
    int eutocode_engine_POLYGON_SIDES=0;
//
// End of the additions of 14 December 2002 for polygons
//
// Start of the additions of 14 December 2002 for colour 16
//
    int eutocode_engine_COLOUR16_RED_COMPONENT=0;
    int eutocode_engine_COLOUR16_GREEN_COMPONENT=0;
    int eutocode_engine_COLOUR16_BLUE_COMPONENT=0;
//
// End of the additions of 14 December 2002 for colour 16
//
// End of the declarations for the eutocode engine
 
// Start of the declarations for the additional colours
    Color brown = new Color(255,102,  0);
    Color lavender = new Color(204,153,204);
    Color mint = new Color(204,255,204);
// End of the declarations for the additional colours
 
    public void init()
      {
        setLayout(null);
        setBounds(0,0,720,576);
        go1Button.addActionListener(this);
        go1Button.setLocation(530,200);
        go1Button.setSize(110,30);
        add(go1Button);
        go2Button.addActionListener(this);
        go2Button.setLocation(530,250);
        go2Button.setSize(110,30);
        add(go2Button);
        text1TextField.setLocation(530,150);
        text1TextField.setSize(110,20);
        add(text1TextField);
 
        setBackground(Color.white);
        Font typeface = new Font("Arial", Font.PLAIN, 12);
        setFont(typeface);
 
        text1TextField.setText(" ");
 
        obeycode=1;
 
// Start of the additions of 14 December 2002 for polygons
    for(int i=0;i <= 100;i++)
      {
        eutocode_engine_POLYGON_XL[i]=0;
        eutocode_engine_POLYGON_YL[i]=0;
        eutocode_engine_POLYGON_XH[i]=0;
        eutocode_engine_POLYGON_YH[i]=0;
        eutocode_engine_POLYGON_X[i]=0;
        eutocode_engine_POLYGON_Y[i]=0;
      }
// End of the additions of 14 December 2002 for polygons
      }
 
    public void actionPerformed(ActionEvent event)
      {
        Object source = event.getSource();
 
        if (source == go1Button)
          {
            obeycode=51;
            repaint();
          }
 
        if (source == go2Button)
          {
            obeycode=52;
            repaint();
          }
      }
 
     public void paint(Graphics screen)
      {                                                             
 
        screen.setColor(Color.black); 
       
        if(obeycode == 51)
          {
            text1=text1TextField.getText();
            int length=text1.length();
            if(length > 0)
              {
                for(int i=0;i < length;i++)
                  {
                    char c;
                    c=text1.charAt(i);
                    eutocode=c;
                    process(screen,eutocode);
                  }
              }
 
            text1TextField.setText("*");
          }
        else if(obeycode == 52)
          {
            screen.setColor(Color.white);
            screen.fillRect(0,0,720,576);
          }
      
      }
 
    public void update(Graphics screen)
      {
        paint(screen);
      }
 
    private void process(Graphics screen, char eutocode)
      {
        char c;
        c=eutocode;
//
        if((c >= '\uEC00') & (c <= '\uEFFF'))
          {
            int n;
            n=((int) c) - ((int) '\uEC00');
            eutocode_engine_A=n;
          }
        else if(c == '\uEB00')
          {
            eutocode_engine_X1L=eutocode_engine_A;
            eutocode_engine_X1=1024*eutocode_engine_X1H + eutocode_engine_X1L;
          }
        else if(c == '\uEB01')
          {
            eutocode_engine_Y1L=eutocode_engine_A;
            eutocode_engine_Y1=1024*eutocode_engine_Y1H + eutocode_engine_Y1L;
          }
        else if(c == '\uEB02')
          {
            eutocode_engine_X2L=eutocode_engine_A;
            eutocode_engine_X2=1024*eutocode_engine_X2H + eutocode_engine_X2L;
          }
        else if(c == '\uEB03')
          {
            eutocode_engine_Y2L=eutocode_engine_A;
            eutocode_engine_Y2=1024*eutocode_engine_Y2H + eutocode_engine_Y2L;
          }
        else if(c == '\uEB20')
          {
            eutocode_engine_COLOUR=eutocode_engine_A;
            screen.setColor(Color.yellow);
            switch(eutocode_engine_COLOUR)
              {
                case  0:  screen.setColor(Color.black);      break;
                case  1:  screen.setColor(brown);            break;
                case  2:  screen.setColor(Color.red);        break;
                case  3:  screen.setColor(Color.orange);     break;
                case  4:  screen.setColor(Color.yellow);     break;
                case  5:  screen.setColor(Color.green);      break;
                case  6:  screen.setColor(Color.blue);       break;
                case  7:  screen.setColor(Color.magenta);    break;
                case  8:  screen.setColor(Color.gray);       break;
                case  9:  screen.setColor(Color.white);      break;
                case 10:  screen.setColor(Color.cyan);       break;
                case 11:  screen.setColor(Color.pink);       break;
                case 12:  screen.setColor(Color.darkGray);   break;
                case 13:  screen.setColor(Color.lightGray);  break;
                case 14:  screen.setColor(lavender);         break;
                case 15:  screen.setColor(mint);             break;
                default:  screen.setColor(Color.black); 
              }
// Start of the additions of 14 December 2002 for colour 16
             if(eutocode_engine_COLOUR == 16)
               {
                 Color colour16=new Color(eutocode_engine_COLOUR16_RED_COMPONENT,
                                          eutocode_engine_COLOUR16_GREEN_COMPONENT,
                                          eutocode_engine_COLOUR16_BLUE_COMPONENT);
                 screen.setColor(colour16);
               }
// End of the additions of 14 December 2002 for colour 16
          }
// Start of the additions of 14 December 2002 for colour 16
        else if(c == '\uEB21')
          {
            eutocode_engine_COLOUR16_RED_COMPONENT=eutocode_engine_A;            
          }
        else if(c == '\uEB22')
          {
            eutocode_engine_COLOUR16_GREEN_COMPONENT=eutocode_engine_A;
          }
        else if(c == '\uEB23')
          {
            eutocode_engine_COLOUR16_BLUE_COMPONENT=eutocode_engine_A;
          }
// End of the additions of 14 December 2002 for colour 16
        else if(c == '\uEB40')
          {
            screen.drawLine(eutocode_engine_X1,eutocode_engine_Y1,
                            eutocode_engine_X2,eutocode_engine_Y2);
          }
        else if(c == '\uEB41')
          {
            screen.drawRect(eutocode_engine_X1,eutocode_engine_Y1,
                            eutocode_engine_X2,eutocode_engine_Y2);
          }
        else if(c == '\uEB42')
          {
            screen.fillRect(eutocode_engine_X1,eutocode_engine_Y1,
                            eutocode_engine_X2,eutocode_engine_Y2);
          }
        else if(c == '\uEB43')
          {
            screen.drawOval(eutocode_engine_X1,eutocode_engine_Y1,
                            eutocode_engine_X2,eutocode_engine_Y2);
          }
        else if(c == '\uEB44')
          {
            screen.fillOval(eutocode_engine_X1,eutocode_engine_Y1,
                            eutocode_engine_X2,eutocode_engine_Y2);
          }
// Start of the additions of 14 December 2002 for polygons
        else if(c == '\uEB30')
// Set this pointer before entering the data for a polygon.
          {
            eutocode_engine_POLYGON_POINTER=0;
          }
        else if(c == '\uEB31')
// Use this after all of the data for a polygon point has been added, data for the first point
// is entered with the pointer value at 0; the pointer is incremented so as to be
// in a steady state when ready to receive an extra point.
          {
            eutocode_engine_POLYGON_POINTER++;
          }
        else if(c == '\uEB10')
// sets an X data value for a polygon point with a new XL value
          {
            eutocode_engine_POLYGON_XL[eutocode_engine_POLYGON_POINTER]=eutocode_engine_A;
//
            eutocode_engine_POLYGON_X[eutocode_engine_POLYGON_POINTER]=
              1024*eutocode_engine_POLYGON_XH[eutocode_engine_POLYGON_POINTER]+
                eutocode_engine_POLYGON_XL[eutocode_engine_POLYGON_POINTER];
          }
        else if(c == '\uEB11')
// sets a Y data value for a polygon point with a new YL value
          {
            eutocode_engine_POLYGON_YL[eutocode_engine_POLYGON_POINTER]=eutocode_engine_A;
//
            eutocode_engine_POLYGON_Y[eutocode_engine_POLYGON_POINTER]=
              1024*eutocode_engine_POLYGON_YH[eutocode_engine_POLYGON_POINTER]+
                eutocode_engine_POLYGON_YL[eutocode_engine_POLYGON_POINTER];
          }
        else if(c == '\uEB50')
// draws a polygon
          {
            eutocode_engine_POLYGON_SIDES=eutocode_engine_POLYGON_POINTER;
            screen.drawPolygon(eutocode_engine_POLYGON_X,
                                 eutocode_engine_POLYGON_Y,
                                   eutocode_engine_POLYGON_SIDES);
          }
        else if(c == '\uEB51')
// draws a filled polygon
          {
            eutocode_engine_POLYGON_SIDES=eutocode_engine_POLYGON_POINTER;
            screen.fillPolygon(eutocode_engine_POLYGON_X,
                                 eutocode_engine_POLYGON_Y,
                                   eutocode_engine_POLYGON_SIDES);
          }
// End of the additions of 14 December 2002 for polygons
 
      }
 
  } 


