import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
public class ChronoApplet extends JApplet implements ActionListener
{
private JTextField chronoton1, chronoton2, k1value, k2value, similarity;
private JLabel chronoton1Label, chronoton2Label, k1valueLabel, k2valueLabel, similarityLabel;
private JButton enter1, enter2, calculate, clear;
private JPanel p1, p2, p3, p4, p5;
public int i=1, j=1, L;
public double sp, k1, k2, T1, T2, I3, I4;
public double [] F1 = new double [100];
public double [] F2 = new double [100];
public double [] T = new double [100];
public double [] I1 = new double [100];
public double [] I2 = new double [100];

public void init()

{
Container c = getContentPane();
c.setLayout(new BorderLayout());
p1 = new JPanel();
p1.setBackground(Color.red);
p1.setLayout(new GridLayout(2,2));
p2 = new JPanel();
p2.setBackground(Color. black);
p2.setLayout(new GridLayout(2,1));
p3 = new JPanel();
p3.setBackground(Color.black);
p3.setLayout(new GridLayout(2,1));
p4 = new JPanel();
p4.setBackground(Color.red);
p5 = new JPanel();
p5.setBackground(Color.black);
chronoton1 = new JTextField(8);
chronoton1.setEditable(true);
chronoton2 = new JTextField(8);
chronoton2.setEditable(true);
k1value = new JTextField(8);
k1value.setEditable(true);
k1value.setText("0.15");
k2value = new JTextField(8);
k2value.setEditable(true);
k2value.setText("1.28");
similarity = new JTextField(15);
similarity.setEditable(true);
chronoton1Label = new JLabel("Chronoton of fragment 1." + i);
chronoton2Label = new JLabel("Chronoton of fragment 2." + i);
k1valueLabel = new JLabel("value of k3 =");
k2valueLabel = new JLabel("value of k1 =");
similarityLabel = new JLabel("Predicted Similarity");
enter1 = new JButton("Enter 1");
enter1.addActionListener(this);
enter2 = new JButton("Enter 2");
enter2.addActionListener(this);
calculate = new JButton("Calculate");
calculate.addActionListener(this);
clear = new JButton("Clear");
clear.addActionListener(this);
p1.add(chronoton1Label);
p1.add(chronoton1);
p1.add(enter1);
p1.add(chronoton2Label);
p1.add(chronoton2);
p1.add(enter2);
p2.add(k2valueLabel);
p2.add(k2value);
p2.add(k1valueLabel);
p2.add(k1value);
p3.add(calculate);
p3.add(clear);
p4.add(similarityLabel);
p4.add(similarity);
c.add("North", p1);
c.add("West", p2);
c.add("Center",p5);
c.add("East",p3);
c.add("South", p4);
}

public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if(s.equals("Enter 1"))
{
F1[i] = Double.parseDouble(chronoton1.getText());
chronoton1.setText("");
i++;
chronoton1Label.setText(("Chronoton of fragment 1." + i));
}
if(s.equals("Enter 2"))
{
F2[j] = Double.parseDouble(chronoton2.getText());
chronoton2.setText("");
j++;
chronoton2Label.setText(("Chronoton of fragment 2." + j));
}
if(s.equals("Clear"))
{
i=1;
j=1;
T1=0;
I3=0;
chronoton1Label.setText(("Chronoton of fragment 1." + i));
chronoton2Label.setText(("Chronoton of fragment 2." + j));
similarity.setText("");
}
if(s.equals("Calculate"))
{
L=i-1;
for(int l=1; l<=L; l++)
{
T[l] = Math.log(F1[l]/F2[l])/Math.log(2);
}
for(int l=1; l<=L-1; l++)
{
I1[l] = Math.log(F1[l+1]/F1[l])/Math.log(2);
I2[l] = Math.log(F2[l+1]/F2[l])/Math.log(2);
}

k1 = Double.parseDouble(k1value.getText());
k2 = Double.parseDouble(k2value.getText());
for(int l=1; l<=L; l++)
{
T1 = T1 + Math.pow(Math.exp((-k1/Math.pow(L,1))*Math.pow(T[l],2)),2);
}
T2 = Math.sqrt(T1/L);
if(L<=1)
{
I4=1;
}
else
{
for(int l=1; l<=L-1; l++)
{
I3 = I3 + Math.pow(Math.exp((-k2/
Math.pow(L-1,-2))*Math.pow((I1[l] - I2[l]),2)),2);
}
I4 = Math.sqrt(I3/(L-1));
}
sp = T2*I4;
similarity.setText(Double.toString(sp));}}
}
