Subversion Repositories wimsdev

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3653 schaersvoo 1
/*    Copyright (C) 1998 XIAO, Gang of Universite de Nice - Sophia Antipolis
2
 *
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License as published by
5
 *  the Free Software Foundation; either version 2 of the License, or
6
 *  (at your option) any later version.
7
 *
8
 *  This program is distributed in the hope that it will be useful,
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 *  GNU General Public License for more details.
12
 *
13
 *  You should have received a copy of the GNU General Public License
14
 *  along with this program; if not, write to the Free Software
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
17
/*
18
    slightly modified [crippled] by jm.evers
19
    applet does not send any data to wims....
20
*/
21
 
22
import java.awt.event.*;
23
import java.awt.*;
24
import java.applet.*;
25
import java.net.*;
26
import java.applet.*;
27
import java.util.Vector;
28
 
29
public class noninput extends Applet{
30
    inp_panel panel;
31
    inp_controls controls;
32
    Image bg;
33
    Color bgcolor;
34
/*    String replystring;
35
    public static String prompt="OK";
36
    public static String retry="Erase";*/
37
    URL backurl;
38
 
39
    public void init() {
40
        URL url;
41
        String parmstr;
42
        parmstr=getParameter("type");
43
        if(parmstr!=null && parmstr.length()>0) {
44
            parmstr.toLowerCase(); parmstr.trim();
45
            if(parmstr.compareTo("curve")==0) inp_panel.ctype=inp_panel.CURVE;
46
            if(parmstr.compareTo("rectangle")==0) inp_panel.ctype=inp_panel.RECT;
47
            if(parmstr.compareTo("rect")==0) inp_panel.ctype=inp_panel.RECT;
48
            if(parmstr.compareTo("circle")==0) inp_panel.ctype=inp_panel.CIRCLE;
49
            if(parmstr.compareTo("lines")==0) inp_panel.ctype=inp_panel.LINES;
50
            if(parmstr.compareTo("segments")==0) inp_panel.ctype=inp_panel.LINES;
51
            if(parmstr.compareTo("line")==0) inp_panel.ctype=inp_panel.LINE;
52
            if(parmstr.compareTo("sline")==0) inp_panel.ctype=inp_panel.SLINE;
53
            if(parmstr.compareTo("semiline")==0) inp_panel.ctype=inp_panel.SLINE;
54
            if(parmstr.compareTo("seg")==0) inp_panel.ctype=inp_panel.SEG;
55
            if(parmstr.compareTo("segment")==0) inp_panel.ctype=inp_panel.SEG;
56
            if(parmstr.compareTo("poly")==0) inp_panel.ctype=inp_panel.POLY;
57
            if(parmstr.compareTo("polygon")==0) inp_panel.ctype=inp_panel.POLY;
58
            if(parmstr.compareTo("points")==0) inp_panel.ctype=inp_panel.POINTS;
59
        }
60
 
61
        parmstr=getParameter("background");
62
        if (parmstr!=null && parmstr.length()>0) {
63
            try {url=new URL(parmstr);}
64
            catch (MalformedURLException e) {url=null;}
65
            if(url!=null) bg=getImage(url);
66
            else bg=null;
67
        }
68
        else bg=null;
69
 
70
        parmstr=getParameter("bgcolor");
71
        if(parmstr!=null && parmstr.length()>0) {
72
            bgcolor=Color.decode(parmstr);
73
            if(bgcolor==null) bgcolor=Color.white;
74
        }
75
        else bgcolor=Color.white;
76
 
77
 
78
/*      parmstr=getParameter("retry");
79
        if(parmstr!=null && parmstr.length()>0) {
80
            retry=parmstr;
81
        }*/
82
 
83
/*      parmstr=getParameter("prompt");
84
        if(parmstr!=null && parmstr.length()>0) {
85
            prompt=parmstr;
86
        }*/
87
 
88
        setLayout(new BorderLayout());
89
        panel=new inp_panel(bgcolor,bg);
90
        controls=new inp_controls(panel,this);
91
        add("Center", panel);
92
        /*add("South",controls);*/
93
    }
94
 
95
    public void destroy() {
96
        remove(panel);
97
        /*remove(controls);*/
98
    }
99
 
100
    public static void main(String args[]) {
101
        Frame f=new Frame("input");
102
        noninput noninput=new noninput();
103
        noninput.init();
104
        noninput.start();
105
 
106
        f.add("Center", noninput);
107
        f.setVisible(true);
108
    }
109
 
110
    public String getAppletInfo() {
111
        return "Curve input program for WIMS.";
112
    }
113
}
114
 
115
class inp_panel extends Panel implements MouseListener, MouseMotionListener {
116
    public static final int CURVE=0, RECT=1, CIRCLE=2, LINES=3, LINE=4,
117
     SLINE=5,SEG=6,POLY=7,POINTS=8;
118
    public static int ctype;
119
    static int ll=4;
120
    Image bg;
121
    Vector lines=new Vector(16384);
122
    int x1,y1;
123
    int x2,y2;
124
    int radius;
125
    int drag;
126
    int st;
127
 
128
    public inp_panel(Color bgcolor,Image gotbg) {
129
        setBackground(bgcolor);
130
        setForeground(Color.black);
131
        bg=gotbg;
132
        addMouseMotionListener(this);
133
        addMouseListener(this);
134
        st=0;
135
    }
136
 
137
/*    public String points2string(String rep) {
138
        StringBuffer buf=new StringBuffer(16384);
139
        buf.append(rep);
140
        switch(ctype) {
141
            case POLY:
142
            case POINTS:
143
            case LINES:
144
            case CURVE: {
145
                int i, np;
146
                Point p=null;
147
                np=lines.size();
148
                for(i=0;i<np;i++) {
149
                    p=(Point)lines.elementAt(i);
150
                    buf.append(p.x).append(",").append(p.y).append(";");
151
                }
152
                break;
153
            }
154
            case SLINE:
155
            case LINE:
156
            case SEG:
157
            case RECT: {
158
                if(st>0) {
159
                    buf.append(x1).append(",").append(y1).append(",").append(x2).append(",").append(y2);
160
                }
161
                break;
162
            }
163
            case CIRCLE: {
164
                if(st>0) {
165
                    buf.append(x1).append(",").append(y1).append(",").append(radius);
166
                }
167
                break;
168
            }
169
        }
170
        return buf.toString();
171
    }
172
  */
173
    public void retry() {
174
        lines.removeAllElements();
175
        st=0; repaint();
176
    }
177
 
178
    public void mouseDragged(MouseEvent e) {
179
        int t=lines.size();
180
        int dr;
181
        e.consume();
182
        switch(ctype) {
183
            case CURVE: {
184
                if(t<2000) {
185
                    x1=e.getX(); y1=e.getY();
186
                    lines.addElement(new Point(x1, y1));
187
                    repaint();
188
                }
189
                st=2; return;
190
            }
191
            default: {
192
                dr=drag; mouseMoved(e); drag=dr+1;
193
                return;
194
            }
195
        }
196
    }
197
 
198
    public void mouseMoved(MouseEvent e) {
199
        e.consume(); drag=0;
200
        switch(ctype) {
201
            case POLY:
202
            case POINTS:
203
            case LINES:
204
            case CURVE: {
205
                if(st==0) return; else st=1;
206
                x2=e.getX(); y2=e.getY(); break;
207
            }
208
            case CIRCLE:
209
            case SLINE:
210
            case LINE:
211
            case SEG:
212
            case RECT: {
213
                if(st!=1) return;
214
                x2=e.getX(); y2=e.getY();
215
                radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
216
                break;
217
            }
218
            default: return;
219
        }
220
        if(ctype!=POINTS) repaint();
221
    }
222
 
223
    public void mousePressed(MouseEvent e) {
224
        e.consume();
225
        switch(ctype) {
226
            case CURVE: {
227
                if(st>0) return;
228
                x1=e.getX(); y1=e.getY(); x2=x1; y2=y1;
229
                lines.removeAllElements();
230
                lines.addElement(new Point(x1,y1));
231
                repaint(); st=2;
232
                return;
233
            }
234
            case POLY:
235
            case POINTS:
236
            case LINES: {
237
                x2=e.getX(); y2=e.getY();
238
                if(st==0) lines.removeAllElements();
239
                lines.addElement(new Point(x2,y2));
240
                st=1; x1=x2; y1=y2; repaint(); return;
241
            }
242
            case CIRCLE:
243
            case SLINE:
244
            case LINE:
245
            case SEG:
246
            case RECT: {
247
                x2=e.getX(); y2=e.getY();
248
                switch(st) {
249
                    case 2:
250
                    case 0: x1=x2; y1=y2; radius=0; st=1; repaint(); return;
251
                    case 1: {
252
                        radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
253
                        st=2; return;
254
                    }
255
                }              
256
                break;
257
            }
258
            default: return;
259
        }
260
    }
261
 
262
    public void mouseReleased(MouseEvent e) {
263
        e.consume();
264
        switch(ctype) {
265
            case CURVE: {
266
                if(st==2) return;
267
                x2=e.getX(); y2=e.getY();
268
                if(st==0) lines.removeAllElements();
269
                lines.addElement(new Point(x2,y2));
270
                st=1; x1=x2; y1=y2;
271
                return;
272
            }
273
            case POINTS: return;
274
            default: {
275
                if(st>0 && drag>=8) mousePressed(e);
276
                return;
277
            }
278
        }
279
    }
280
 
281
    public void mouseEntered(MouseEvent e) {
282
    }
283
 
284
    public void mouseExited(MouseEvent e) {
285
        if(ctype!=CURVE && ctype!=LINES && ctype!=POLY) return;
286
        e.consume();
287
        x2=x1; y2=y1; if(st>0) repaint();
288
    }
289
 
290
    public void mouseClicked(MouseEvent e) {
291
    }
292
 
293
    public void paint(Graphics g) {
294
        int np=lines.size();
295
        Point pp;
296
 
297
        if(bg!=null) g.drawImage(bg,0,0,this);
298
        switch(ctype) {
299
            case POINTS: {
300
                if(np>0) for (int i=0; i < np; i++)
301
                  pointPaint(g,(Point)lines.elementAt(i));
302
                break;
303
            }
304
            case POLY:
305
            case CURVE:
306
            case LINES: {
307
                if(np>0) {
308
                    pp=(Point)lines.elementAt(0);
309
                    for (int i=1; i < np; i++) {
310
                        Point p=(Point)lines.elementAt(i);
311
                        g.drawLine(pp.x, pp.y, p.x, p.y);
312
                        pp=p;
313
                    }
314
                    if (st==1) g.drawLine(x1, y1, x2, y2);
315
                }
316
                if(ctype==POLY && np>1) {
317
                    Point p1=(Point)lines.elementAt(0);
318
                    Point p2=(Point)lines.elementAt(np-1);
319
                    if(st==0) g.drawLine(p1.x,p1.y,p2.x,p2.y);
320
                    else g.drawLine(p1.x,p1.y,x2,y2);
321
                }
322
                break;
323
            }
324
            case SEG:
325
            case SLINE:
326
            case LINE:
327
            case RECT: {
328
                if(st>0) {
329
                    int xx1,yy1,xx2,yy2;
330
                    int X1,Y1,X2,Y2,max;
331
                    xx1=Math.min(x1,x2); yy1=Math.min(y1,y2);
332
                    xx2=Math.max(x1,x2); yy2=Math.max(y1,y2);
333
                    max=Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
334
                    if(max<10) max=10;
335
                    max=500/max+1;
336
                    switch(ctype) {
337
                        case RECT: g.drawRect(xx1,yy1,xx2-xx1,yy2-yy1); break;
338
                        case SLINE: {
339
                            g.fillOval(x1-2,y1-2,4,4);
340
                            g.drawLine(x1,y1,max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
341
                        }
342
                        case LINE: {
343
                            g.fillOval(x1-2,y1-2,4,4);
344
                            g.fillOval(x2-2,y2-2,4,4);
345
                            g.drawLine(max*x1-(max-1)*x2,max*y1-(max-1)*y2,
346
                                       max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
347
                        }
348
                        case SEG: g.drawLine(x1,y1,x2,y2); break;
349
                    }
350
                }
351
                break;
352
            }
353
            case CIRCLE: {
354
                if(st>0) {
355
                    pointPaint(g,new Point(x1,y1));
356
                    g.drawOval(x1-radius,y1-radius,radius*2,radius*2);
357
                }
358
                break;
359
            }
360
        }
361
    }
362
 
363
    void pointPaint(Graphics g, Point p) {
364
        g.drawLine(p.x-ll,p.y+ll,p.x+ll,p.y-ll);
365
        g.drawLine(p.x+ll,p.y+ll,p.x-ll,p.y-ll);
366
    }
367
}
368
 
369
 
370
class inp_controls extends Panel implements ActionListener {
371
   noninput ci;
372
   inp_panel targ;
373
   Button retry, ok;
374
 
375
   public inp_controls(inp_panel pan, noninput cci) {
376
       this.ci=cci;
377
       targ=pan;
378
       setLayout(new GridLayout());
379
       setBackground(Color.white);
380
      /* retry=new Button(noninput.retry);*/
381
      /* retry.addActionListener(this);*/
382
       /*ok=new Button(noninput.prompt);
383
       ok.addActionListener(this);
384
       add(retry); add(ok);*/
385
   }
386
   public void actionPerformed(ActionEvent e) {
387
       Object src=e.getSource();
388
       if(src == retry) targ.retry();
389
   }
390
}