
//Java Applet Demonstration on Time taken for complete page transfer with its objects
// based on HTTP Connection Type, Number of Objects and Delay.

//<APPLET CODE = "myapplet"  WIDTH = 850 HEIGHT = 700 ></APPLET>

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.BorderFactory;
import javax.swing.border.*;
import javax.swing.border.TitledBorder;

public class myapplet extends JApplet implements ItemListener
{
     public String[] ConnectionTypeListData = {"                 Non-Persistent Connections           ","      Non-Persistent Conn w Parallel Conn    ","          Persistent Connections w/o Pipelining     ","            Persistent Connections w Pipelining        "};
     //Beshan - did some changes here

     public String[] ParallelConnListData =   {"                                      1","                                      2","                                      3","                                      4"};
     public String[] HTMLObjectsData = {"                                      1","                                      2","                                      3","                                      4"};
     public String[] DelayListData = {"                                      0","                               0.25 RTT","                               0.50 RTT   ","                               1.00 RTT","                               2.00 RTT"};
     //This is the data for all the lists

     public int index1,index2,index3,index4;
     //These indexes are for knowing which data is selected

     public double Total_Delay;
     //Total Delay in RTTs

     public JLabel ConnectionTypeLabel, ParallelConnLabel,HTMLObjectsLabel, DelayLabel, HTMLPageLabel,HTMLPageFieldLabel;
     // The labels are for the lists

     public JLabel DummyLabel;
     // These dummy label are for bottom left panel to hold space for the panel

     public JComboBox ConnectionTypeList, ParallelConnList,HTMLObjectsList, DelayList;
     // The actual lists for selecting data

     public int startLineX,startLineY,endLineX,endLineY,serverDelay=0,rectY;
     // These are coordinates for lines drawn between client and server

     public double startLineX1, endLineX1;
     // Coordinates obtained from Screen are in double. So they are converted into int later.

     public String doubleString;
     // This string is an intermediate step in conversion from double to int

     public Dimension size;
     // For screen size.

     public JPanel topLeftPanel, bottomLeftPanel, leftPanel, basePanel, rightPanel;
     // Applet has different panels

     public Container contentPane;

     Border raisedbevel;

     public void itemStateChanged(ItemEvent e)
     {
           repaint();
     }

      public void init()
       {

           startLineX = 632;
           endLineX = 782;
           startLineY = 50;
           endLineY = 70;

           // Obtaining coordinates

           ConnectionTypeLabel = new JLabel("Choose connection type");
           ConnectionTypeLabel.setForeground(Color.black);
           ParallelConnLabel = new JLabel("Choose number of parallel connections");
           ParallelConnLabel.setForeground(Color.black);
           HTMLPageLabel = new JLabel("Basic HTML file");
           HTMLPageLabel.setForeground(Color.black);
           HTMLObjectsLabel = new JLabel("Choose number of objects");
           HTMLObjectsLabel.setForeground(Color.black);
           DelayLabel = new JLabel("Choose per-object transmission delay (in RTTs)");
           DelayLabel.setForeground(Color.black);
           // Initializing labels

           DummyLabel = new JLabel(".",JLabel.LEFT);

           ConnectionTypeList = new JComboBox();
           //for( int iCtr = 0; iCtr < ConnectionTypeListData.length; iCtr++ )
		    ConnectionTypeList.addItem( ConnectionTypeListData[0] );
		    ConnectionTypeList.addItem( ConnectionTypeListData[1] );
		    ConnectionTypeList.addItem( ConnectionTypeListData[2] );
		    ConnectionTypeList.addItem( ConnectionTypeListData[3] );
		    //Beshan - did some changes here

           ConnectionTypeList.setEditable( false );
           ConnectionTypeList.setBackground(Color.lightGray);
           ParallelConnList = new JComboBox();
           for( int iCtr = 0; iCtr < ParallelConnListData.length; iCtr++ )
		    ParallelConnList.addItem( ParallelConnListData[iCtr] );
           ParallelConnList.setEditable( false );
           ParallelConnList.setBackground(Color.lightGray);
           HTMLObjectsList = new JComboBox();
           for( int iCtr = 0; iCtr < HTMLObjectsData.length; iCtr++ )
		    HTMLObjectsList.addItem( HTMLObjectsData[iCtr] );
           HTMLObjectsList.setBackground(Color.lightGray);
           HTMLObjectsList.setEditable( false );
           DelayList = new JComboBox();
           for( int iCtr = 0; iCtr < DelayListData.length; iCtr++ )
		    DelayList.addItem(DelayListData[iCtr] );
           HTMLObjectsList.setEditable( false );
           DelayList.setBackground(Color.lightGray);

           /*HTMLPageFieldLabel = new JLabel("  1");
           HTMLPageFieldLabel.setForeground(Color.black);
           Border border2 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
           HTMLPageFieldLabel.setBorder(border2);*/

           topLeftPanel  = new JPanel();
           topLeftPanel.setLayout(new GridLayout(4,2));
           topLeftPanel.add(ConnectionTypeLabel);
           topLeftPanel.add(ConnectionTypeList);
           topLeftPanel.add(ParallelConnLabel);
           topLeftPanel.add(ParallelConnList);
           topLeftPanel.add(HTMLObjectsLabel);
           topLeftPanel.add(HTMLObjectsList);
           topLeftPanel.add(DelayLabel);
           topLeftPanel.add(DelayList);
           // Adding labels and lists to topLeft panel

           ConnectionTypeList.addItemListener(this);
           ParallelConnList.addItemListener(this);
           ParallelConnList.setEnabled(false);
           HTMLObjectsList.addItemListener(this);
           DelayList.addItemListener(this);
           // Adding action listeners to lists

           bottomLeftPanel = new JPanel();
           bottomLeftPanel.setLayout(new GridLayout(30,2));
           for (int k = 0; k < 61; k++)
           {
                 bottomLeftPanel.add(DummyLabel);
           }
           // Adding dummy labels to bottom left panel

           topLeftPanel.setBackground(Colors.mgray);
           bottomLeftPanel.setBackground(Colors.mgray);

           leftPanel = new JPanel();
           leftPanel.setLayout(new BoxLayout(leftPanel,BoxLayout.Y_AXIS));
           leftPanel.add(topLeftPanel);
           leftPanel.add(bottomLeftPanel);
           // forming the left panel

           rightPanel = new JPanel();
           rightPanel.setBackground(Colors.mgray);

           basePanel = new JPanel();
           basePanel.setLayout(new BorderLayout());
           basePanel.setBorder(BorderFactory.createEtchedBorder());
           basePanel.add(leftPanel,BorderLayout.WEST);
           basePanel.add(rightPanel,BorderLayout.CENTER);

           contentPane = getContentPane();
           contentPane.add(basePanel,BorderLayout.CENTER);
           // Adding all panels to base Panel and base panel to container.
     }

     public void update(Graphics g)
     {
         paint(g);
     }

     public void paint(Graphics g)
     {
         Total_Delay = 0;
         index1 = ConnectionTypeList.getSelectedIndex();
         index2 = ParallelConnList.getSelectedIndex();
         index3= HTMLObjectsList.getSelectedIndex();
         index4= DelayList.getSelectedIndex();

         switch (index4)
          {
              case 1: serverDelay = 10;
                      break;
              case 2: serverDelay = 20;
                      break;
              case 3: serverDelay = 40;
                      break;
              case 4: serverDelay = 80;
                      break;
              default: serverDelay = 0;
                      break;
          }
          // Selecting index of delay

          startLineX = 632;
          endLineX = 782;
          startLineY = 50;
          endLineY = 70;
          rectY = startLineY;
          // Initializing all coordinates for painting based on list selection


          // Obtaining selected list indices

          super.paint(g);
          // painting backgrounds

          g.setColor(Color.black);
          int y_for_line = 250;
          g.drawLine(0,y_for_line,535,y_for_line);
          g.drawLine(0,y_for_line+65,535,y_for_line+65);
          int y_for_rect = 360;
          g.setColor(Color.black);
          g.fillRect(50,y_for_rect,25,25);
          g.drawString("Delay due to TCP connection establishment", 95,y_for_rect+15);
          g.setColor(Colors.AGreen);
          g.fillRect(50,y_for_rect+35,25,25);
          g.setColor(Color.black);
          g.drawString("Delay due to HTML page request", 95,y_for_rect+50);
          g.setColor(Colors.MRed);
          g.fillRect(50,y_for_rect+70,25,25);
          g.setColor(Color.black);
          g.drawString("Delay due to object request", 95,y_for_rect+85);
          g.setColor(Color.blue);
          g.fillRect(50,y_for_rect+105,25,25);
          g.setColor(Color.black);
          g.drawString("Transmission Delay", 95,y_for_rect+120);
          // Painting the INDEX on bottom left of screen

          g.drawString("Client",startLineX-25,startLineY-10);
          g.drawString("Server",endLineX-5,startLineY-10);
          g.setColor(Colors.MYellow);
          g.fillRect (startLineX-20,startLineY,20,630);
          g.fillRect(endLineX,endLineY-20,20,630);
          // Drawing client and server rectangles

          g.setColor(Color.black);
          g.fillRect(startLineX-20, rectY, 20, 40);
          rectY = rectY+40;
          g.drawLine(startLineX,startLineY,endLineX,endLineY);
          startLineY = startLineY + 40;
          g.drawLine(endLineX, endLineY, startLineX, startLineY);
          endLineY = endLineY +40;
          Total_Delay = Total_Delay + 1;
          // Drawing TCP Handshake Rectangle

          g.setColor(Colors.AGreen);
          g.fillRect(startLineX-20,rectY,20,40);
          g.drawLine(startLineX, startLineY, endLineX, endLineY);
	  startLineY = startLineY+40;
          g.drawLine(endLineX, endLineY, startLineX, startLineY);
          Total_Delay = Total_Delay + 1;
          // Drawing HTML Page Request

          rectY = rectY + 40;
          g.setColor(Color.blue);
          g.fillRect(startLineX-20,rectY,20,serverDelay);
          Total_Delay = Total_Delay + 0.025 * serverDelay;
          if (serverDelay != 0)
          {
              for (int r = 0; r < serverDelay-1; r++)
                   {
                       endLineY = endLineY + 1;
                       startLineY = startLineY + 1;
          	       g.drawLine(endLineX, endLineY, startLineX, startLineY);
                   }
               rectY = rectY + serverDelay;
           }
           endLineY = endLineY + 40;


          // Drawing the rectangles based on list data selected.
          switch (index1)
          {
           case 0:
                 ParallelConnList.setEnabled(false);
                 ParallelConnList.setSelectedIndex(0);
                 for (int i=0; i< (index3 + 1); i++)
                        {
                                g.setColor(Color.black);
                                g.fillRect(startLineX-20, rectY, 20, 40);
                                rectY = rectY+40;
                                g.drawLine(startLineX,startLineY,endLineX,endLineY);
                                startLineY = startLineY + 40;
                                g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                Total_Delay = Total_Delay + 1;
                                endLineY = endLineY +40;
                                g.setColor(Colors.MRed);
                                g.fillRect(startLineX-20, rectY, 20, 40);
                                rectY = rectY+40;
                                g.drawLine(startLineX,startLineY,endLineX,endLineY);
                                startLineY = startLineY + 40;
                                g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                g.setColor(Color.blue);
                                g.fillRect(startLineX-20,rectY,20,serverDelay);
                                Total_Delay = Total_Delay + 0.025 * serverDelay;
                                for (int r = 0; r < serverDelay-1; r++)
                                  {
                                        endLineY = endLineY + 1;
                                        startLineY = startLineY + 1;
          				g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                  }
                                rectY = rectY + serverDelay;
                                Total_Delay = Total_Delay + 1;
                                endLineY = endLineY +40;
			}
                   break;

                   //Beshan - did some changes here
          /*case 1:
                  ParallelConnList.setEnabled(false);
                  ParallelConnList.setSelectedIndex(0);
                  g.setColor(Color.black);
                  g.fillRect(startLineX-20, rectY, 20, 40);
                  rectY = rectY+40;
                  g.drawLine(startLineX,startLineY,endLineX,endLineY);
                  startLineY = startLineY + 40;
                  g.drawLine(endLineX, endLineY, startLineX, startLineY);
                  Total_Delay = Total_Delay + 1;
                  endLineY = endLineY +40;
                  g.setColor(Colors.MRed);
                  g.fillRect(startLineX-20, rectY, 20, 40);
                  rectY = rectY+40;
                  g.drawLine(startLineX,startLineY,endLineX,endLineY);
                  startLineY = startLineY + 40;
                  g.drawLine(endLineX, endLineY, startLineX, startLineY);
                  g.setColor(Color.blue);
                  g.fillRect(startLineX-20,rectY,20,serverDelay);
                  Total_Delay = Total_Delay + 0.025 * serverDelay;
                  for (int r = 0; r < serverDelay-1; r++)
                    {
                          endLineY = endLineY + 1;
                          startLineY = startLineY + 1;
                          g.drawLine(endLineX, endLineY, startLineX, startLineY);
                    }
                  Total_Delay = Total_Delay + 1;
                  rectY = rectY + serverDelay;
                  endLineY = endLineY +40;
                  break;
           */
          case 1:
                 ParallelConnList.setEnabled(true);
                 int R;
                 index2 = index2 + 1;
                 index3 = index3 + 1;
                 if (index2 >= index3)
                       R = 1;
                 else
                     {
                       if (index3%index2 == 0)
                           R = index3/index2;
                       else
                           R = index3/index2 + 1;
                     }
                 for (int i=0; i < R; i++)
                        {
                                g.setColor(Color.black);
                                g.fillRect(startLineX-20, rectY, 20, 40);
                                rectY = rectY+40;
                                g.drawLine(startLineX,startLineY,endLineX,endLineY);
                                startLineY = startLineY + 40;
                                g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                Total_Delay = Total_Delay + 1;
                                endLineY = endLineY +40;
                                g.setColor(Colors.MRed);
                                g.fillRect(startLineX-20, rectY, 20, 40);
                                rectY = rectY+40;
                                g.drawLine(startLineX,startLineY,endLineX,endLineY);
                                startLineY = startLineY + 40;
                                g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                g.setColor(Color.blue);
                                g.fillRect(startLineX-20,rectY,20,serverDelay);
                                Total_Delay = Total_Delay + 0.025 * serverDelay;
                                for (int r = 0; r < serverDelay-1; r++)
                                  {
                                        endLineY = endLineY + 1;
                                        startLineY = startLineY + 1;
          				g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                  }
                                rectY = rectY + serverDelay;
                                Total_Delay = Total_Delay + 1;
                                endLineY = endLineY +40;
			}
                   break;
             case 2:
                 ParallelConnList.setEnabled(false);
                 ParallelConnList.setSelectedIndex(0);
                 for (int i=0; i< index3+1; i++)
                        {
                             g.setColor(Colors.MRed);
                             g.fillRect(startLineX-20, rectY, 20, 40);
                             rectY = rectY+40;
                             g.drawLine(startLineX,startLineY,endLineX,endLineY);
                             startLineY = startLineY + 40;
                             g.drawLine(endLineX, endLineY, startLineX, startLineY);
                             g.setColor(Color.blue);
                             g.fillRect(startLineX-20,rectY,20,serverDelay);
                             Total_Delay = Total_Delay + 0.025 * serverDelay;
                             for (int r = 0; r < serverDelay-1; r++)
                                  {
                                        endLineY = endLineY + 1;
                                        startLineY = startLineY + 1;
          				g.drawLine(endLineX, endLineY, startLineX, startLineY);
                                  }
                             Total_Delay = Total_Delay + 1;
                             rectY = rectY + serverDelay;
                             endLineY = endLineY + 40;
			}
                  break;
             case 3:
                  ParallelConnList.setEnabled(false);
                  ParallelConnList.setSelectedIndex(0);
                  g.setColor(Colors.MRed);
                  g.fillRect(startLineX-20, rectY, 20, 40);
                  rectY = rectY + 40;
                  g.drawLine(startLineX,startLineY,endLineX,endLineY);
                  startLineY = startLineY + 40;
                  g.drawLine(endLineX, endLineY, startLineX, startLineY);
                  g.setColor(Color.blue);
                  g.fillRect(startLineX-20,rectY,20,serverDelay);
                  Total_Delay = Total_Delay + 0.025 * serverDelay;
                  for (int r = 0; r < serverDelay-1; r++)
                      {
                            endLineY = endLineY + 1;
                            startLineY = startLineY + 1;
                            g.drawLine(endLineX, endLineY, startLineX, startLineY);
                      }
                  Total_Delay = Total_Delay + 1;
                  rectY = rectY + serverDelay;
                  endLineY = endLineY + 40;
                  break;
		}
       g.setColor(Colors.MBlue);
       g.drawString(" Total Delay (in RTTs) is:  " + Total_Delay,50,y_for_line+44);
     }
}

class Colors {

    static final Color MRed        = new Color(200,21,33);
    static final Color MBlue       = new Color(0,0,143);
    static final Color MGold       = new Color(204, 180, 0);
    static final Color MYellow     = new Color(242,215,180);
    static final Color ABlue       = new Color(24, 35, 87, 80);
    static final Color ARed        = new Color(102, 0, 51, 80);
    static final Color AGold       = new Color(204, 180, 0, 80);
    static final Color AGreen      = new Color(0,147,0);
    static final Color MViolet     = new Color(160,0,240);
    static final Color lgray       = new Color(105, 120, 141);
    static final Color mgray       = new Color(140, 140, 140);
    static final Color lightrose   = new Color(200, 188, 212);
    static final Color highlighter = new Color(169, 31, 93);
}




