import mkgray.gui.*;
import mkgray.gui.util.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class ShoesBars implements Runnable {
  Frame f;
  BarCanvas canv;
  boolean initialized;
  SimpleGraph graphs[] = new SimpleGraph[15];

  public static void main(String s[]){
    new ShoesBars();
  }

  ShoesBars() {
    Thread runthis = new Thread(this);
    runthis.start();
    while(true){
    if(initialized){
	f.repaint();
     }
	try { Thread.sleep(200); } catch (Exception e) { }
   }
  }

  public void run() {
    int value;
    long lastt=0;
    long startt, endt;
    f = new Frame();
    f.add(canv=new BarCanvas());
    initialized = true;
    f.setBounds(0,0,600,520);
    f.pack();
    f.doLayout();
    f.show();
    int ct = 0;
    int n = 0;
    int graphnum = 0;
    double np, pp;
    Random rand = new Random();
    InputStreamReader isr;
    StreamTokenizer st = new StreamTokenizer(isr=new InputStreamReader(System.in));
    st.parseNumbers();
    st.eolIsSignificant(true);
    value = 0;
    System.out.println("Initialized");
    while(true) {
      System.out.print(".");
      try {
	int it = 0;
	while(!isr.ready()){
	  canv.repaint();
	  Thread.sleep(50);
	}
	st.nextToken();
      } catch (Exception e) { }
      if(st.ttype == StreamTokenizer.TT_NUMBER) {
	if((graphnum >= 0) && (graphnum < 15))
	  canv.bars[graphnum] = st.nval;
	canv.repaint();
	graphnum++;
      }
      else if(st.ttype == StreamTokenizer.TT_EOF)
	return;
      else if(st.ttype == StreamTokenizer.TT_EOL) {
	ct++;
	System.out.println("Line time: "+(System.currentTimeMillis()-lastt));
	lastt = System.currentTimeMillis();
	graphnum = 0;
      }
      if(graphnum == 0){
	if(ct%10 == 0)
	  Runtime.getRuntime().gc();
	try { Thread.sleep(50); } catch (Exception e) { }
      }
    }
  }
}

class BarCanvas extends Canvas {
  Image ui;
  DoubleBuffer db;
  double bars[] = new double[15];
  String barname[] = {"1", "2", "3", "4", "PAN", "Compass (quad)",
		      "Gyro", "Compass (iphase)",
		      "AMP accelerometer #1",
		      "AMP accelerometer #2",
		      "AMP accelerometer #3",
		      "Tilt A",
		      "Tilt B",
		      "Sonar",
		      "nothing"};
  BarCanvas() {
    ui = getToolkit().getImage("ui.jpg");
    db = new DoubleBuffer();
  }

  public void update(Graphics g){
    db.update(this, g);
//    g.setColor(Color.white);
//    g.fillRect(125, 295, 128, 16);
//    g.fillRect(103, 334, 128, 16);
//    g.fillRect(118, 382, 128, 16);
//    g.fillRect(128, 425, 128, 16);

//    g.fillRect(547, 315, 17, 170);
//    g.fillRect(475, 315, 17, 170);
//    g.setColor(Color.black);
//    paint(g);
  }

  public void paint(Graphics g){
    int llx, lly, lrx, lry, urx, ury, ulx, uly;
    int x1, x2, y1, y2;

    g.drawImage(ui, 0, 0, this);
    g.fillRect(126, 296, (int) (bars[0]/2), 14);
    g.fillRect(104, 335, (int) (bars[1]/2), 14);
    g.fillRect(119, 383, (int) (bars[2]/2), 14);
    g.fillRect(129, 426, (int) (bars[3]/2), 14);
    g.fillRect(548, (int) (485-(2*bars[4]/3)), 24, (int) (2*bars[4]/3));
    g.fillRect(476, (int) (485-(2*bars[6]/3)), 24, (int) (2*bars[6]/3));

    g.drawLine(216, 201, 
	       (int) (216+((bars[5]-128)/2)), 
	       (int) (201+((bars[7]-128)/2)));
    g.fillArc((int) (216+((bars[5]-128)/2)-5),
	      (int) (201+((bars[7]-128)/2)-5),
	      10, 10, 0, 360);

    g.drawLine(364, 331, 
	       (int) (364+((bars[8]-128)/2)), 
	       (int) (331+((bars[9]-128)/2)));
    g.fillArc((int) (364+((bars[8]-128)/2)-5),
	      (int) (331+((bars[9]-128)/2)-5),
	      10, 10, 0, 360);

    g.drawLine(391, 393, 
	       (int) (391+((bars[8]-128)/2)), 
	       (int) (393+((bars[10]-128)/2)));
    g.fillArc((int) (391+((bars[8]-128)/2)-5),
	      (int) (393+((bars[10]-128)/2)-5),
	      10, 10, 0, 360);

    double ang;

    ang = Math.atan((bars[12]-128)/(bars[11]-128));
    x1 = 390-((int) (20*Math.sin(ang)));
    x2 = 390+((int) (20*Math.sin(ang)));
    y1 = 185-((int) (20*Math.cos(ang)));
    y2 = 185+((int) (20*Math.cos(ang)));

//    ang = Math.atan((y2-y1)/(x2-x1));
    ang += (Math.PI/2);

    ulx = (int) (x1-(25*Math.sin(ang)));
    llx = (int) (x1+(25*Math.sin(ang)));
    urx = (int) (x2-(25*Math.sin(ang)));
    lrx = (int) (x2+(25*Math.sin(ang)));

    uly = (int) (y1-(25*Math.cos(ang)));
    lly = (int) (y1+(25*Math.cos(ang)));
    ury = (int) (y2-(25*Math.cos(ang)));
    lry = (int) (y2+(25*Math.cos(ang)));


    int sx[] = { llx, lrx, urx, ulx };
    int sy[] = { lly, lry, ury, uly };

//    g.fillPolygon(sx, sy, 4);
    g.drawLine(390, 185, 390+((int) ((bars[11]-128)/1)), 185+((int) ((bars[12]-128)/1)));
    g.fillArc(385+((int) ((bars[11]-128)/1)), 180+((int) ((bars[12]-128)/1)), 10, 10, 0, 360);

    g.setColor(Color.black);
//    for(int b=0;b<13;b++){
//      g.fillRect(5, 12+(b*25), (int) bars[b], 15);
//      g.drawString(barname[b], 5, 10+(b*25));
//    }
  }
}


