import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;


public class QueueSimulation extends Applet implements ActionListener
{
   private Label title;
   private TextField input;
   private Button insert,remove,peekFront,size;
   private double number,return1,return2;
   private Queue theQueue;
   private boolean full,removed,peeked;

   public void init()
   {
      title=new Label("Enter a number:");
      add(title);

      input=new TextField(10);
      add(input);

      insert=new Button("Insert");
      add(insert);
      insert.addActionListener(this);

      remove=new Button("Remove");
      add(remove);
      remove.addActionListener(this);

      peekFront=new Button("Peek Front");
      add(peekFront);
      peekFront.addActionListener(this);

      theQueue = new Queue(10);
   }

   public void actionPerformed(ActionEvent e)
   {
      if(e.getSource()==insert)
      {
         number = Double.valueOf(input.getText()).doubleValue();
         if(!theQueue.isFull())
         {
            theQueue.insert(number);
            input.setText("");
            input.requestFocus();
         }
         else
            full = true;
            removed = false;
            peeked = false;

      }

      if(e.getSource()==remove)
      {
         if(!theQueue.isEmpty())
         {
            return1 = theQueue.remove();
            theQueue.delete();
         }
            full = false;
            removed = true;
            peeked = false;
      }
      if(e.getSource()==peekFront)
      {
         if(!theQueue.isEmpty())
         {
            return2 = theQueue.peekFront();
            peeked = true;
            removed = false;
         }
      }
      repaint();
   }

   public void paint(Graphics g)
   {
      if(full==true)
      {
         g.setColor(Color.red);
         g.drawString("Queue is Full",180,100);
         g.setColor(Color.black);
      }
      if(removed==true)
      {
         g.drawString("Just removed="+return1,180,120);
      }
      if(peeked==true)
      {
         g.drawString("Front="+return2,180,120);
      }
      g.drawString("Size="+theQueue.size(),180,160);
      theQueue.display(g);
   }
}

class Queue
{
   private double[] queueArray;
   private int front,rear;
   private int maxSize,nItems;

   private int startX=50,startY=250;
   private int boxHeight = 20,boxWidth = 40;
   private int y,height2 = boxHeight*3/4;
   private int b;

   int y1 = startY+boxHeight;
                
   public Queue(int q)
   {
      maxSize = q;
      queueArray = new double[maxSize];
      front = -1;
      rear = -1;
      nItems = 0;
   }

   public void insert(double j)
   {
      if(front==-1)
         ++front;

      if(rear==maxSize-1)
         rear=-1;
      queueArray[++rear]=j;
      nItems++;
   }

   public double remove()
   {
      double temp=queueArray[front++];
      if(front==maxSize)
         front=0;
      nItems--;
      return temp;
   }

   public void delete()
   {
      queueArray[front-1]=0;
   }

   public double peekFront()
   {
      return queueArray[front];
   }

   public boolean isEmpty()
   {
      return (nItems==0);
   }

   public boolean isFull()
   {
      return (nItems==maxSize);
   }

   public int size()
   {
      return nItems;
   }

   public void display(Graphics g)
   {
      y = startY;
      for(int i=0;i<maxSize;i++)
      {
         g.setColor(Color.yellow);
         g.fillRect(startX,y,boxWidth,boxHeight);
         g.setColor(Color.black);
         g.drawRect(startX,y,boxWidth,boxHeight);
         g.drawString(""+queueArray[i],startX,y+boxHeight*3/4);
         y = y-boxHeight;
      }
      if(!(front==rear))  
      {
         if(front>-1)  
         {
            b = (2*rear)+1;
            g.drawLine(100,y1-(boxHeight*b/2),150,y1-(boxHeight*b/2));
            g.drawString("rear",130,y1-(boxHeight*b/2));
         }
         if(front>-1)
         {
            int c =(2*front)+1;
            g.drawLine(100,y1-(boxHeight*c/2),150,y1-(boxHeight*c/2));
            g.drawString("front",130,y1-(boxHeight*c/2));
         }
      }
      else if((front==rear)&&(!(front==-1)))
      {
         int a=(2*front)+1;
         g.drawLine(100,y1-(boxHeight*a/2),150,y1-(boxHeight*a/2));
         g.drawString("front/rear",100,y1-(boxHeight*a/2));
      }
   }
}
