import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;
/**
 * Title:        Graph Generation Program
 * Description:  Scale Free Small Worlds
 * Copyright:    Copyright (c) 2001
 * Company:      Columbia University
 * @author Angelos Stavrou
 * @version 1.0
	Last change:  AS   20 Dec 101    9:47 am
 */

 class GraphDraw extends JPanel {
  public int numberofpoints;
  public double conprob;
  public void StartG(int nn, double cp) {
    numberofpoints=nn;
    conprob = cp;
    System.out.println("Number of nodes --->"+nn);
    System.out.println("Connect Probability --->"+cp);
    repaint(0);
  }
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    int maxWidth = getWidth();
    int maxHeight = getHeight();
    Random randomv = new Random();
    int posx[] = new int [numberofpoints+1];
    int posy[] = new int [numberofpoints+1];
    int degree[] = new int [numberofpoints];
    int adjacencyM[][] = new int [numberofpoints+1][numberofpoints+1];
    g.setColor(Color.red);
    for (int i = 0; i < numberofpoints; i++){
        posx[i]=randomv.nextInt(maxWidth);
        posy[i]=randomv.nextInt(maxHeight);
        g.fillOval(posx[i],posy[i],5,5);

 //   g.drawArc(35, 50, 125, 180, 1, 360);
    }
     g.setColor(Color.blue);
    int numofedgesc = 0;
    for (int i1 = 0; i1 < numberofpoints -1 ;i1++){
      for  (int i2 = i1+1 ; i2 < numberofpoints;i2++){
        if ( conprob >= randomv.nextDouble() ){
          adjacencyM[i2][i1]=1;
          numofedgesc++;
          degree[i1]++;
          degree[i2]++;
          g.drawLine(posx[i1]+2,posy[i1]+2,posx[i2]+2,posy[i2]+2);
        }
      }
    }
    GraphR.numofedges.setText(Integer.toString(numofedgesc));
//    System.out.println("--->"+maxWidth);
  }

}
public class GraphR extends JApplet implements ActionListener {
//  public JTextField conprob,numofnodes;
  int i;
  public JButton jButton1 = new JButton();
  public JButton jButton2 = new JButton();
  public JLabel jLabel1 = new JLabel();
  public JLabel jLabel2 = new JLabel();
  public JLabel jLabel3 = new JLabel();
  public JLabel jLabel4 = new JLabel();
  public JTextField conprob = new JTextField(3);
  public JTextField numofnodes = new JTextField(4);
  public static JTextField numofedges = new JTextField(6);
  public JTextField dummy = new JTextField(10);
  public GraphDraw graphd = new GraphDraw();
  Dimension d = new Dimension (350,350);
  public JSlider zoom= new JSlider (0,100,50);

/*    public void paint(Graphics g)

    {
        String s = numofnodes.getText();
        int start = Integer.parseInt(s);
        s = conprob.getText();
        int sweep = Integer.parseInt(s);
        g.drawArc(35, 50, 125, 180, start, sweep);
    }
  */

  public void init() {
  Container cp = getContentPane();
  cp.setLayout(new FlowLayout(0,20,20));
  cp.setSize(600,500);
  jButton1.setText("Start");
  jButton2.setText("Stop");
  jLabel1.setText("Connect Probability:");
  jLabel2.setText("Number of Nodes:   ");
  jLabel3.setText("Number of Edges:   ");
  numofedges.setEditable(false);
  cp.add(jLabel2);
  cp.add(numofnodes);
  cp.add(jLabel1);
  cp.add(conprob);
  cp.add(jButton1);
  cp.add(jButton2);
  graphd.setPreferredSize(d);
  cp.add(BorderLayout.CENTER,graphd);
  cp.add(BorderLayout.SOUTH,jLabel3);
  cp.add(BorderLayout.SOUTH,numofedges);
  cp.add(BorderLayout.SOUTH,zoom);
  jButton1.addActionListener(this);
  numofnodes.addActionListener(this);
  conprob.addActionListener(this);
  zoom.setBorder(new TitledBorder("Connect Probability"));
  zoom.addChangeListener(new ChangeListener(){
    public void stateChanged(ChangeEvent e){
      System.out.println(((JSlider)e.getSource()).getValue());
      conprob.setText(Double.toString(((JSlider)e.getSource()).getValue()/100.));
      graphd.StartG(Integer.parseInt(numofnodes.getText()),Double.parseDouble(conprob.getText()));
    }
  });

  numofnodes.setText("10");
  conprob.setText("0.5");

}



 public final void update(Graphics g) {
    paint(g);
    //   System.out.println(""+graph);
  }
// A main() for the application :
  public static void  main (String[] args) {
    JApplet applet = new GraphR();
    JFrame frame = new JFrame("Scale Free Small World Graphs");
    frame.getContentPane().add(applet);
    frame.setSize(600,550);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
    public void actionPerformed(ActionEvent evt) {
    //  System.out.println(i);
    //  i++;
      graphd.StartG(Integer.parseInt(numofnodes.getText()),Double.parseDouble(conprob.getText()));
    }

}

