import java.awt.*;
import java.awt.event.*;
 
public class ast00803 extends java.applet.Applet implements
                               ActionListener,MouseListener
 
// Software written by William Overington
// WOverington@ngo.globalnet.co.uk
// 23 July 2001
// Copyright 2001 William Overington
//
 
  {
    int obeycode;
    int storedobeycode;
    int pressx,pressy;
 
    boolean SIMULATION_DVB_MHP_MOUSE_ATTACHED=false;
// this is what the simulation management environment knows
 
    boolean mouseattached;
// this is what the simulated program knows
 
    Button colourButton1  = new Button("");
    Button colourButton2  = new Button("");
    Button colourButton3  = new Button("");
    Button colourButton4  = new Button("");
    Button digitButton0   = new Button("0");
    Button digitButton1   = new Button("1");
    Button digitButton2   = new Button("2");
    Button digitButton3   = new Button("3");
    Button digitButton4   = new Button("4");
    Button digitButton5   = new Button("5");
    Button digitButton6   = new Button("6");
    Button digitButton7   = new Button("7");
    Button digitButton8   = new Button("8");
    Button digitButton9   = new Button("9");
    Button upButton       = new Button("^");
    Button downButton     = new Button("v");
    Button leftButton     = new Button("<");
    Button rightButton    = new Button(">");
    Button enterButton    = new Button("E");
    Button teletextButton = new Button("T");
 
    Font buttonsfont,displayfont;
 
// polyglot compatibility section starts
    String title="Simulation of a harmonograph.";
    String messageaboutred="up";
    String messageaboutgreen="down";
    String messageaboutyellow="draw";
    String messageaboutblue="clear number";
    String choice1="1 yellow";
    String choice2="2 green";
    String choice3="3 cyan";
    String choice4="4 orange";
    String choice5="5 white";
// polyglot compatibility section finishes
 
    int a,p,b,q;
    char focus;
 
    int displayscreen;
    int chosencolour;
 
    int sofar;
    int percentsofar;
    int oldpercentsofar=-1;
    
 
    public void init()
      {
        setLayout(null);
        setBounds(0,0,512,400);
 
        buttonsfont = new Font("SansSerif", Font.PLAIN, 18);
        displayfont = new Font("SansSerif", Font.PLAIN, 24);
        setFont(buttonsfont);
 
        colourButton1.addActionListener(this);
        colourButton1.setLocation(16,327);
        colourButton1.setSize(25,25);
        colourButton1.setBackground(Color.red);
        add(colourButton1);
        colourButton2.addActionListener(this);
        colourButton2.setLocation(48,327);
        colourButton2.setSize(25,25);
        colourButton2.setBackground(Color.green);
        add(colourButton2);
        colourButton3.addActionListener(this);
        colourButton3.setLocation(80,327);
        colourButton3.setSize(25,25);
        colourButton3.setBackground(Color.yellow);
        add(colourButton3);
        colourButton4.addActionListener(this);
        colourButton4.setLocation(112,327);
        colourButton4.setSize(25,25);
        colourButton4.setBackground(Color.blue);
        add(colourButton4);
        digitButton0.addActionListener(this);
        digitButton0.setLocation(16,359);
        digitButton0.setSize(25,25);
        digitButton0.setBackground(Color.lightGray);
        add(digitButton0);
        digitButton1.addActionListener(this);
        digitButton1.setLocation(48,359);
        digitButton1.setSize(25,25);
        digitButton1.setBackground(Color.lightGray);
        add(digitButton1);
        digitButton2.addActionListener(this);
        digitButton2.setLocation(80,359);
        digitButton2.setSize(25,25);
        digitButton2.setBackground(Color.lightGray);
        add(digitButton2);
        digitButton3.addActionListener(this);
        digitButton3.setLocation(112,359);
        digitButton3.setSize(25,25);
        digitButton3.setBackground(Color.lightGray);
        add(digitButton3);
        digitButton4.addActionListener(this);
        digitButton4.setLocation(144,359);
        digitButton4.setSize(25,25);
        digitButton4.setBackground(Color.lightGray);
        add(digitButton4);
        digitButton5.addActionListener(this);
        digitButton5.setLocation(176,359);
        digitButton5.setSize(25,25);
        digitButton5.setBackground(Color.lightGray);
        add(digitButton5);
        digitButton6.addActionListener(this);
        digitButton6.setLocation(208,359);
        digitButton6.setSize(25,25);
        digitButton6.setBackground(Color.lightGray);
        add(digitButton6);
        digitButton7.addActionListener(this);
        digitButton7.setLocation(240,359);
        digitButton7.setSize(25,25);
        digitButton7.setBackground(Color.lightGray);
        add(digitButton7);
        digitButton8.addActionListener(this);
        digitButton8.setLocation(272,359);
        digitButton8.setSize(25,25);
        digitButton8.setBackground(Color.lightGray);
        add(digitButton8);
        digitButton9.addActionListener(this);
        digitButton9.setLocation(304,359);
        digitButton9.setSize(25,25);
        digitButton9.setBackground(Color.lightGray);
        add(digitButton9);
        upButton.addActionListener(this);
        upButton.setLocation(372,327);
        upButton.setSize(25,25);
        upButton.setBackground(Color.lightGray);
        add(upButton);
        downButton.addActionListener(this);
        downButton.setLocation(372,359);
        downButton.setSize(25,25);
        downButton.setBackground(Color.lightGray);
        add(downButton);
        leftButton.addActionListener(this);
        leftButton.setLocation(340,343);
        leftButton.setSize(25,25);
        leftButton.setBackground(Color.lightGray);
        add(leftButton);
        rightButton.addActionListener(this);
        rightButton.setLocation(404,343);
        rightButton.setSize(25,25);
        rightButton.setBackground(Color.lightGray);
        add(rightButton);
        enterButton.addActionListener(this);
        enterButton.setLocation(439,359);
        enterButton.setSize(25,25);
        enterButton.setBackground(Color.lightGray);
        add(enterButton);
        teletextButton.addActionListener(this);
        teletextButton.setLocation(471,359);
        teletextButton.setSize(25,25);
        teletextButton.setBackground(Color.lightGray);
        add(teletextButton);
 
        addMouseListener(this);
 
        setBackground(Color.black);
 
        obeycode=1;
        storedobeycode=1;
 
//      default cases that give a good display
        a=1000;
        p=300000;
        b=1330;
        q=300000;
 
        focus='a';
        chosencolour=2;
 
      }
 
    public void actionPerformed(ActionEvent event)
      {
        Object source = event.getSource();
 
        if (source == colourButton1)
          {
            obeycode=51;
            repaint();
          }
        else
        if (source == colourButton2)
          {
            obeycode=52;
            repaint();
          }
        else
        if (source == colourButton3)
          {
            obeycode=53;
            repaint();
          }
        else
        if (source == colourButton4)
          {
            obeycode=54;
            repaint();
          }
        else
        if (source == digitButton0)
          {
            obeycode=20;
            repaint();
          }
        else
        if (source == digitButton1)
          {
            obeycode=21;
            repaint();
          }
        else
        if (source == digitButton2)
          {
            obeycode=22;
            repaint();
          }
        else
        if (source == digitButton3)
          {
            obeycode=23;
            repaint();
          }
        else
        if (source == digitButton4)
          {
            obeycode=24;
            repaint();
          }
        else
        if (source == digitButton5)
          {
            obeycode=25;
            repaint();
          }
        else
        if (source == digitButton6)
          {
            obeycode=26;
            repaint();
          }
        else
        if (source == digitButton7)
          {
            obeycode=27;
            repaint();
          }
        else
        if (source == digitButton8)
          {
            obeycode=28;
            repaint();
          }
        else
        if (source == digitButton9)
          {
            obeycode=29;
            repaint();
          }
        else
        if (source == upButton)
          {
            obeycode=61;
            repaint();
          }
        else
        if (source == downButton)
          {
            obeycode=62;
            repaint();
          }
        else
        if (source == leftButton)
          {
            obeycode=63;
            repaint();
          }
        else
        if (source == rightButton)
          {
            obeycode=64;
            repaint();
          }
        else
        if (source == enterButton)
          {
            obeycode=91;
            repaint();
          }
        else
        if (source == teletextButton)
          {
            obeycode=92;
            repaint();
          }
 
      }
 
    public void mousePressed(MouseEvent e)
      {
        pressx=e.getX();
        pressy=e.getY();
 
        if ((pressy > 300) & (pressy < 311))
// then a command to the simulation environment management is intended
//
// the 300 is used in the test rather than 288 so as to give a blank area below
// the simulated display screen just in case a click intended for the bottom
// of the simulated display screen is clicked a few pixels low in error 
          {
            if (pressx < 256) // simulated no DVB-MHP mouse
              {
                SIMULATION_DVB_MHP_MOUSE_ATTACHED=false;
              }
            else // simulated DVB-MHP mouse present
              {
                SIMULATION_DVB_MHP_MOUSE_ATTACHED=true;
              }
            obeycode=1;
            repaint();
          }
        else if (pressy <= 287)
// then a click within the simulation on the simulated display screen is intended
          {
            if(SIMULATION_DVB_MHP_MOUSE_ATTACHED)
              {
                obeycode=41;
                repaint();
              }
            else
              {
                donothing();
              }
          }
        else
// the mouse press is on the simulated hand held infra-red control device
// or the upper part of the white area and is disregarded
          {
            donothing();
          }
 
      }
 
    public void mouseReleased(MouseEvent e)
      {
 
      }
 
    public void mouseClicked(MouseEvent e)
      {
 
      }
 
    public void mouseEntered(MouseEvent e)
      {
       
      }
 
    public void mouseExited(MouseEvent e)
      {
        
      }
 
    public void paint(Graphics screen)
      {
 
        storedobeycode=obeycode;
 
        screen.setFont(displayfont);
 
        if(obeycode == 1)
          {
// start up for the simulation management
            screen.setColor(Color.black);
            screen.fillRect(0,0,512,288);
            screen.setColor(Color.white);
            screen.fillRect(0,288,512,112);
            screen.setColor(Color.gray);
            screen.fillRoundRect(0,311,512,89,20,20);
            screen.clipRect(0,0,512,288);
// start up for the program that is being simulated
            mouseattached=SIMULATION_FOR_MOUSE_getInputDeviceSupported();
            displayscreen=1;
            screen.setColor(Color.cyan);
            screen.drawString(title,20,50);
// title in English is "Simulation of a harmonograph."
            screen.setColor(Color.red);
            screen.fillRect(15,110,20,20);
            screen.setColor(Color.green);
            screen.fillRect(15,150,20,20);
            screen.setColor(Color.yellow);
            screen.fillRect(15,190,20,20);
            screen.setColor(Color.blue);
            screen.fillRect(15,230,20,20);
            screen.setColor(Color.cyan);
            screen.drawString(messageaboutred,40,130);
// messageaboutred in English is "up"
            screen.drawString(messageaboutgreen,40,170);
// messageaboutgreen in English is "down"
            screen.drawString(messageaboutyellow,40,210);
// messageaboutyellow in English is "draw"
            screen.drawString(messageaboutblue,40,250);
// messageaboutblue in English is "clear number"
//
            screen.drawString("a =",300,130);
            screen.drawString("p =",300,170);
            screen.drawString("b =",300,210);
            screen.drawString("q =",300,250);
//
            screen.drawString("" + a,350,130);
            screen.drawString("" + p,350,170);
            screen.drawString("" + b,350,210);
            screen.drawString("" + q,350,250);
//
// setting the display for the focus being 'a' at the start
            screen.setColor(Color.black);  // magenta during development for testing
            screen.fillRect(280,110,5,150);
            screen.setColor(Color.orange);
            screen.fillRect(280,110,5,25);
          }
        else
        if(obeycode == 51) // red button pressed
          {
            if(displayscreen == 1)
              {
                if(focus == 'a')
                  {
                    focus='q';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,230,5,25);
                  }
                else if(focus == 'p')
                  {
                    focus='a';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,110,5,25);
                  }
                else if(focus == 'b')
                  {
                    focus='p';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,150,5,25);
                  }
                else if(focus == 'q')
                  {
                    focus='b';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,190,5,25);
                  }
              }
          }
        else if(obeycode == 52) // green button pressed
          {
            if(displayscreen == 1)
              {
                if(focus == 'a')
                  {
                    focus='p';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,150,5,25);
                  }
                else if(focus == 'p')
                  {
                    focus='b';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,190,5,25);
                  }
                else if(focus == 'b')
                  {
                    focus='q';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,230,5,25);
                  }
                else if(focus == 'q')
                  {
                    focus='a';
                    screen.setColor(Color.black);  // magenta during development for testing
                    screen.fillRect(280,110,5,150);
                    screen.setColor(Color.orange);
                    screen.fillRect(280,110,5,25);
                  }
              }
          }
        else if(obeycode == 53) // yellow button pressed
          {
            if(displayscreen == 1)
              {
                displayscreen=2;
                screen.setColor(Color.black);
                screen.fillRect(0,0,512,288);
                screen.setColor(Color.yellow);
                screen.drawString(choice1,40,90);
// choice1 in English is "1 yellow"
                screen.setColor(Color.green);
                screen.drawString(choice2,40,130);
// choice2 in English is "2 green"
                screen.setColor(Color.cyan);
                screen.drawString(choice3,40,170);
// choice3 in English is "3 cyan"
                screen.setColor(Color.orange);
                screen.drawString(choice4,40,210);
// choice4 in English is "4 orange"
                screen.setColor(Color.white);
                screen.drawString(choice5,40,250);
// choice5 in English is "5 white"
              }
            else if(displayscreen == 2)
              {
                donothing();
              }
          }
        else if(obeycode == 54) // blue button pressed
          {
            if(displayscreen == 1)
              {
                if(focus == 'a')
                  {
                    a=0;
                    screen.setColor(Color.black);  // orange during development for testing
                    screen.fillRect(350,100,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + a,350,130);
                  }
                else if(focus == 'p')
                  {
                    p=0;
                    screen.setColor(Color.black);
                    screen.fillRect(350,140,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + p,350,170);
                  }
                else if(focus == 'b')
                  {
                    b=0;
                    screen.setColor(Color.black);
                    screen.fillRect(350,180,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + b,350,210);
                  }
                else if(focus == 'q')
                  {
                    q=0;
                    screen.setColor(Color.black);
                    screen.fillRect(350,220,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + q,350,250);
                  }
              }
          }
        else if((obeycode >= 20) & (obeycode <= 29)) // a digit button pushed
          {
            if(displayscreen == 1)
              {
                int value;
                value=obeycode - 20;
                if(focus == 'a')
                  {
                    a=a*10 + value;
                    screen.setColor(Color.black);
                    screen.fillRect(350,100,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + a,350,130);
                  }
                else if(focus == 'p')
                  {
                    p=p*10 + value;
                    screen.setColor(Color.black);
                    screen.fillRect(350,140,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + p,350,170);
                  }
                else if(focus == 'b')
                  {
                    b=b*10 + value;
                    screen.setColor(Color.black);
                    screen.fillRect(350,180,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + b,350,210);
                  }
                else if(focus == 'q')
                  {
                    q=q*10 + value;
                    screen.setColor(Color.black);
                    screen.fillRect(350,220,162,40);
                    screen.setColor(Color.cyan);
                    screen.drawString("" + q,350,250);
                  }
              }
            else if(displayscreen == 2)
              {
                int value;
                value=obeycode - 20;
                chosencolour=value;
                if((value >= 1) & (value <= 5))
                  {
                    screen.setColor(Color.black);
                    screen.fillRect(0,0,512,288);
                    if(chosencolour == 1)
                      {
                        screen.setColor(Color.yellow);
                      }
                    else if(chosencolour == 2)
                      {
                        screen.setColor(Color.green);
                      }
                    else if(chosencolour == 3)
                      {
                        screen.setColor(Color.cyan);
                      }
                    else if(chosencolour == 4)
                      {
                        screen.setColor(Color.orange);
                      }
                    else if(chosencolour == 5)
                      {
                        screen.setColor(Color.white);
                      }
                  }
 
                boolean pendown=false;
                double x,y;
                int xdisplay,ydisplay;
                int oldxdisplay=0;
                int oldydisplay=0;
                double aover1000,pover1000,bover1000,qover1000;
                aover1000=((double)a)/(double)1000.0;
                pover1000=((double)p)/(double)1000.0;
                bover1000=((double)b)/(double)1000.0;
                qover1000=((double)q)/(double)1000.0;
 
                screen.drawLine(6,278,506,278);
// draws a gauge line along the bottom of the screen

                for(int i=0;i < 5000000; i++)  // five million
                  {
                    double t;
                    t=((double)i)/((double)1000.0);
                    x = Math.cos(aover1000*t)*Math.exp(-t/pover1000) +
                        Math.cos(bover1000*t)*Math.exp(-t/qover1000); 
                    y = Math.sin(aover1000*t)*Math.exp(-t/pover1000); 
 
                    xdisplay=256 + (int)((double)100.0*x);
                    ydisplay=144 - (int)((double)100.0*y);
 
                    if(pendown)
                      {
                        screen.drawLine(oldxdisplay,oldydisplay,xdisplay,ydisplay);
                        sofar=i/10000;
// thus sofar should go from 0 to 499 during a run
                        percentsofar=i/50000;
// thus percentsofar should go from 0 to 99 during a run
                        if(percentsofar != oldpercentsofar)
                          {
                            oldpercentsofar=percentsofar;
                            screen.setColor(Color.black); // blue during development
                            screen.fillRect(0,210,60,40);
                            if(chosencolour == 1)
                              {
                                screen.setColor(Color.yellow);
                              }
                            else if(chosencolour == 2)
                              {
                                screen.setColor(Color.green);
                              }
                            else if(chosencolour == 3)
                              {
                                screen.setColor(Color.cyan);
                              }
                            else if(chosencolour == 4)
                              {
                                screen.setColor(Color.orange);
                              }
                            else if(chosencolour == 5)
                              {
                                screen.setColor(Color.white);
                              }
                            screen.drawString("" + percentsofar + "%",6,240);
                          }
                        screen.drawLine(sofar + 6,268,sofar + 6,268);
                      }
                    oldxdisplay=xdisplay;
                    oldydisplay=ydisplay;
                    pendown=true;
// delay loop so that the picture draws slowly on the screen
                    for(int j=0;j < 1000;j++)
                      {
                        double r;
                        r=Math.random();
                      }
                  }
              }
            else if(displayscreen == 3)
              {
                donothing();
              }
           }
        else if(obeycode == 41) // response to pressing the mouse button
          {
            donothing();
          }
 
        obeycode=1;      
      }
 
    public void update(Graphics screen)
      {
        paint(screen);
      }
 
    private void donothing()
      {
//      does nothing
      }
 
    private boolean SIMULATION_FOR_MOUSE_getInputDeviceSupported()
      {
        return SIMULATION_DVB_MHP_MOUSE_ATTACHED;
      }
 
  }