// Written by Albert Lilly
// Alabama School of Mathematics and Science
// email: lilly@olympus.asms.state.k12.al.us
//

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
public class digit extends Applet{
// Set up positions for the lights
// First column: 0 means the light is off, 1 means turn on the light
// Second column: The starting column of a light
// Third column: The starting row of a light
// Fourth column: The horizontal distance between a light's boundaries
// Fifth column: The vertical distance between a light's boundaries
        static int[][] Lights = {{0,130,76,70,10}, // Top light
                                 {0,200,86,10,70}, // Clockwise, the top
                                                   // light on the right
                                 {0,200,166,10,70},// Bottom light on
                                                   // the right
                                 {0,130,236,70,10},// Bottom light
                                 {0,120,166,10,70},// Bottom light on the
                                                   // left
                                 {0,120,86,10,70}, // Top light on the left
                                 {0,130,156,70,10}}; // The middle light
        static int digit; // Digit holds the value to be displayed

// Start the program
        public void init(){
                // No specific initialization needed
        }

// Set up the screen
        public void paint(Graphics g) {
        int i;
        g = this.getGraphics();
                {
                Color temp = new Color(127+80,127+80,127+80);  // Set up gray
                                                               // box
                g.setColor(temp);
                g.fillRect(45 ,60, 
                       30, 200);
                }
                // Add the digits to the gray box
                g.setColor(Color.black);
                g.drawString("0",57,75);
                g.drawString("1",57,95);
                g.drawString("2",57,115);
                g.drawString("3",57,135);
                g.drawString("4",57,155);
                g.drawString("5",57,175);
                g.drawString("6",57,195);
                g.drawString("7",57,215);
                g.drawString("8",57,235);
                g.drawString("9",57,255);
                // Show instructions
                g.drawString("Click on a digit to display the digit"
                                ,66,28);
                // Show background for lights
                g.fillRect(89,60,150,200);

        }

                                       

// Perform the logic for each digit selected
        public boolean mouseDown(Event e, int x, int y)
        {
        int i; // For looping
        // Check to see if a digit was clicked
                if(45 <= x && x <= 75) {
                   if (60 <= y && y <= 260) {
                      digit = (y - 60) / 20;
                      Graphics  g = this.getGraphics();
                      // Repaint the background
                      g.setColor(Color.black);
                      g.fillRect(89,60,150,200);
// A, B, C, and D are binary inputs
// 'A' is the most significant digit, i.e., A = (2^3 * 1) or (2^3 * 0)
// 'B' is the second most significant digit, i.e., B = (2^2 * 1) or (2^2 * 0)
// 'C' is the third most significant digit, i.e., C = (2^1 * 1) or (2^1 * 0)
// 'D' is the least significant digit, i.e., D = (2^0 * 1) or (2^0 * 0)
                       int A, B, C, D = 0;
                       A = (digit >>> 3 << 31 >>> 31);
                       B = (digit >>> 2 << 31 >>> 31);
                       C = (digit >>> 1 << 31 >>> 31);
                       D = (digit << 31 >>> 31);

                       // Set the top light
                       Lights[0][0] =
                                      OR(
                                         OR(
                                            OR(A,C),
                                            AND(B,D)
                                            ),
                                         AND(NOT(B),NOT(D))
                                        )
                                      ;

                       // Clockwise, set the top light on the right
                       Lights[1][0] =
                                      OR(
                                         OR(
                                            OR(A,NOT(B)),
                                            AND(NOT(C),NOT(D))
                                            ),
                                         AND(C,D)
                                        )
                                      ;

                       // Set the bottom light on the right
                       Lights[2][0] =
                                      OR(
                                         OR(B,NOT(C)),
                                         AND(C,D)
                                        )
                                      ;

                       // Set the bottom light 
                       Lights[3][0] =
                                      OR(
                                         AND(
                                             AND(B,NOT(C)),
                                             D
                                             ),
                                         OR (
                                            OR (
                                               OR (A,
                                                   AND(C,NOT(D))
                                                  ),
                                               AND(NOT(B),NOT(D))
                                               ),
                                            AND(NOT(B),C)
                                            )
                                        )
                                      ;

                       // Set the bottom light on the left
                       Lights[4][0] =
                                      OR(
                                         OR(
                                            AND(NOT(B),NOT(D)),
                                            AND(A,NOT(D))
                                            ),
                                         AND(C,NOT(D))
                                        )
                                      ;
                                                
                       // Set the top light on the left
                       Lights[5][0] =
                                      OR(
                                         A,
                                         OR(
                                            B,
                                            AND(NOT(C),NOT(D))
                                           )
                                        )
                                      ;

                       // Set the middle light 
                       Lights[6][0] =
                                      OR(
                                         OR(
                                            A,
                                            OR(
                                               AND(
                                                   AND(C,D),
                                                   NOT(B)
                                                  ),
                                               AND(C,NOT(D))
                                               )
                                           ),
                                         AND(B,NOT(C))
                                        )
                                      ;
                                     
                       // Turn on the lights
                       g.setColor(Color.white);
                       for(i = 0; i < 7; i++)
                       if (Lights[i][0]==1)
                       g.fillRect(Lights[i][1],Lights[i][2],
                              Lights[i][3],Lights[i][4]);
                   }
                }
                return true;

        }

// NOT(1) = 0
// NOT(0) = 1
        public int NOT(int i){
           if (i == 1)
              return 0;
            else
              return 1;
        }
        
// AND(0,0) = 0
// AND(0,1) = 0
// AND(1,0) = 0
// AND(1,1) = 1
        public int AND(int i, int j){
              return (i * j);
        }

// OR(0,0) = 0
// OR(0,1) = 1
// OR(1,0) = 1
// OR(1,1) = 1
        public int OR(int i, int j){
           if ((i+j) == 0)
              return 0;
            else
              return 1;
        }


}
