import java.awt.*;
import java.applet.*;
import java.io.*;

public class Dcms extends Frame 
  {
  public static void main(String [] args){

    Dcms h= new Dcms();
    Dcms1 DApplet = new Dcms1();
    h.add("Center", DApplet);
    h.pack();
    h.show();
  }

  Dcms() {
    setTitle("Dcms");
  }

 }
 class Dcms1 extends Applet {
   double x0[] = {-1416,-1371,-1070,-1063,-1684,-184,-3097,-1597,97,1597,
                -2917,-917,-1083,917,
                -1060,-940,-2417,-917,-1083,417,
                -2597,97};
    double r[]  = {4111,4459,4923,5272    , 5995,5995, 7018, 7018,7018,7018,
                7018,7018,7018,7018,
                7018,7018,7018,7018,7018,7018,
                7018,7018};  
    double dx[] = {2060,2060,2480,2480,    1480, 1480,1480,1480,1480,1480,
                1980,1980,1980,1980,
                2060,2060,1480,1980,1480,1480,
                2480,2480 };
    int type[] =  {   1,   1,   1,   1,       1,    1,   2,   2,   2,   2,
                   4,   4,   3,   3 ,
                   5,   6,   8,   8,   7,   7,
                   9,   9};
    String codice[] = {"RB1%20in", "RB1%20out", "RB2%20in","RB2%20out","RB3/n%2b","RB3/n-","RB4/4%2b%2b","RB4/4%2b","RB4/4-","RB4/4--",
                    "RB4/5%2b","RB4/5-","RB4/1%2b","RB4/1-",
                    "RB4/11","RB4/9","RB4/8%2b","RB4/8-","RB4/12%2b","RB4/12-",
                     "RB4/10%2b","RB4/10-"};  
    String wheel[] = {"W-2/","W-1/","W%200/","W%2b1/","W%2b2/"};
   int j;
   String url = "http://www.ba.infn.it/%7eabbresci/FaceMySQL/chamber_search.php3?ChName=";
   PrintStream myOutput;
public Dimension getPreferredSize(){ 
                        return (new Dimension(250,250)); 
                      } 

public void paint(Graphics g){
   String nome;

   Polygon c;
  
   //g.drawString("Wheel -2",getSize().width/2,getSize().height/2-20);
   try {

    FileOutputStream out = new FileOutputStream("temp.out");
    myOutput = new PrintStream(out);                      
      g.setColor(Color.white);
      g.fillRect(0,0,getSize().width,getSize().height);
      for( j=0;j < wheel.length;j++){
      myOutput.println(
      "<img src=wheel1.gif usemap=#wheel"+j+"> <map name=wheel"+j+">");     
   g.setColor(Color.red);
      for(int i=0;i < x0.length;i++){
        nome = codice[i];
        switch (type[i]) {
        case  1:
        for(int k=0;k<12;k++){
           int k1 = k;
           k1 = k +4;if(k1>12)k1=k1-12;
           if(nome.indexOf("RB3")==0)
            nome = codice[i].substring(0,4)+k1+codice[i].substring(5,codice[i].length()); 
           else  nome = codice[i] +"/"+k1;
           c = conv(x0[i],r[i],dx[i],k,nome);g.fillPolygon(c);         } 
          break;        
        case  2:c = conv(x0[i],r[i],dx[i],0,nome);g.fillPolygon(c);          
         break;        
        case  3:
        for(int k=9;k<12;k++){
           int k2 = k-8;
           if(k>9)nome=codice[i].substring(0,4)+k2+codice[i].substring(5,codice[i].length());
           c = conv(x0[i],r[i],dx[i],k,nome);g.fillPolygon(c);         } 
          break;        
        case  4:
        for(int k=1;k<4;k++){
           int k3 = k+4;
           if(k>1)nome=codice[i].substring(0,4)+k3+codice[i].substring(5,codice[i].length());
           c = conv(x0[i],r[i],dx[i],k,nome);g.fillPolygon(c);         } 
          break;        
        case  5:c = conv(x0[i],r[i],dx[i],7,nome);g.fillPolygon(c);          
         break;        
        case  6:c = conv(x0[i],r[i],dx[i],5,nome);g.fillPolygon(c);          
         break;        
        case  7:c = conv(x0[i],r[i],dx[i],8,nome);g.fillPolygon(c);          
         break;        
        case  8:c = conv(x0[i],r[i],dx[i],4,nome);g.fillPolygon(c);          
         break;        
        case  9:c = conv(x0[i],r[i],dx[i],6,nome);g.fillPolygon(c);          
         break;        
        }
     }
       g.setColor(Color.black);
       g.drawLine(0,getSize().height/2,getSize().width,getSize().height/2);
       g.drawLine(getSize().width/2,0,getSize().width/2,getSize().height);
      myOutput.println("</map>");
      }
   }
                          catch (IOException e) {
                          System.out.println("Error: "+e);
                          System.exit(1);
                          }      
  }
  Polygon conv(double x0,double y0,double dx,int rot,String codice){
   double x1 = - 7500, y1 = -7500, x2 = 7500, y2 = 7500;
   double xp[]=new double[4],yp[]=new double[4];
   double xp1,yp1;
   int x,y;
   Polygon c = new Polygon();
    xp[0] = x0;
    yp[0] = y0;
   xp[1] = x0+dx-100.;
   yp[1] = y0; 
   xp[2] = x0+dx-100.;
   yp[2] = y0+250.; 
   xp[3] = x0;
   yp[3] = y0+250.; 
   myOutput.print("<area shape=poly coords=");
   for(int i=0;i<4;i++){
   if(rot > 0){
    xp1 = xp[i]*Math.cos(Math.PI/180.*30.*rot)-yp[i]*Math.sin(Math.PI/180.*30.*rot); 
    yp1 = xp[i]*Math.sin(Math.PI/180.*30.*rot)+yp[i]*Math.cos(Math.PI/180.*30.*rot); 
    xp[i] = xp1;yp[i]=yp1;
    }
   x = (int)((double)((xp[i]-x1)/(x2-x1))*getSize().width);
   y = getSize().height-(int)((double)((yp[i]-y1)/(y2-y1))*getSize().height);
   c.addPoint(x,y);
   if(i==0)myOutput.print(x+","+y);
   if(i!=0)myOutput.print(","+x+","+y);
    }
   myOutput.println(" href="+url+wheel[j]+codice+">");
   return c;
   }
 }
