bug fixes
[IRC.git] / Robust / src / Interface / Imap.java
1 package Interface;
2 import java.net.*;
3 import java.io.*;
4 import java.util.*;
5
6 class Imap {
7     private Rectangle[] rectangles;
8     private Point[] points;
9     long THRESHOLD=400;
10
11     public Imap(String filename) {
12         FileReader fr=null;
13         try {
14             fr=new FileReader(filename);
15             parseFile(fr);
16             fr.close();
17         } catch (IOException e) {
18             System.out.println(e);
19             System.exit(-1);
20         }
21     }
22     static class Rectangle {
23         String label;
24         int x1,y1,x2,y2;
25         public Rectangle(String label, int x1,int y1, int x2, int y2) {
26             this.label=label;
27             this.x1=x1;
28             this.y1=y1;
29             this.x2=x2;
30             this.y2=y2;
31         }
32     }
33
34     String parseclick(int x,int y) {
35         System.out.println(x+","+y);
36         for(int i=0;i<rectangles.length;i++) {
37             Rectangle r=rectangles[i];
38             if ((r.x1<=x)&&(r.y1>=y)&&
39                 (r.x2>=x)&&(r.y2<=y))
40                 return r.label;
41         }
42         long mindistance=Long.MAX_VALUE;
43         int minindex=-1;
44         for(int i=0;i<points.length;i++) {
45             Point p=points[i];
46             long dx=p.x-x;
47             long dy=p.y-y;
48             if ((dx*dx+dy*dy)<mindistance) {
49                 mindistance=dx*dx+dy*dy;
50                 minindex=i;
51             }
52         }
53         if (mindistance>THRESHOLD)
54             return null;
55         else
56             return points[minindex].label;
57     }
58
59     static class Point {
60         String label;
61         int x,y;
62         public Point(String label, int x,int y) {
63             this.label=label;
64             this.x=x;
65             this.y=y;
66         }
67     }
68
69     void parseFile(FileReader fr) {
70         int firstchar=0;
71         ArrayList rectangles=new ArrayList();
72         ArrayList points=new ArrayList();
73         while(true) {
74             try {
75                 firstchar=fr.read();
76             } catch (Exception e) {
77                 e.printStackTrace();
78                 System.exit(-1);
79             }
80             /* EOF?*/
81             if (firstchar==-1)
82                 break;
83             switch(firstchar) {
84                 case'b':
85                     case'#':
86                     while(firstchar!='\n') {
87                         try {
88                             firstchar=fr.read();
89                         } catch (IOException e) {
90                             e.printStackTrace();
91                             System.exit(-1);
92                         }
93                     }
94                 break;
95                 case'r':
96                     {
97                         nexttoken(fr,false);
98                         String label=nexttoken(fr,false);
99                         String x1=nexttoken(fr,true);
100                         String y1=nexttoken(fr,true);
101                         String x2=nexttoken(fr,true);
102                         String y2=nexttoken(fr,true);
103                         Rectangle r=new Rectangle(label,Integer.parseInt(x1),Integer.parseInt(y1),
104                                                   Integer.parseInt(x2),Integer.parseInt(y2));
105                         rectangles.add(r);
106                     }
107                 break;
108                 case'p':
109                     {
110                         nexttoken(fr,false);
111                         String label=nexttoken(fr,false);
112                         String x=nexttoken(fr,true);
113                         String y=nexttoken(fr,true);
114                         Point p=new Point(label,Integer.parseInt(x),Integer.parseInt(y));
115                         points.add(p);
116                     }
117                 break;
118             }
119         }
120         this.rectangles=(Rectangle[]) rectangles.toArray(new Rectangle[rectangles.size()]);
121         this.points=(Point[]) points.toArray(new Point[points.size()]);
122     }
123
124     String nexttoken(java.io.InputStreamReader isr,boolean commas) {
125         String string="";
126         int c=0;
127         boolean looped=false;
128         while(true) {
129             try {
130                 c=isr.read();
131             } catch (IOException e) {
132                 e.printStackTrace();
133                 System.exit(-1);
134             }
135             if ((c==' ')||(c=='\n')||(commas&&c==',')) {
136                 if (!looped) {
137                     looped=true;
138                     continue;
139                 }
140                 return string;
141             }
142             string=string+new String(new char[]{(char)c});
143             looped=true;
144         }
145     }
146
147 }
148