Back to index

wims  3.65+svn20090927
noninput.java
Go to the documentation of this file.
00001 /*    Copyright (C) 1998 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 /*
00018     slightly modified [crippled] by jm.evers 
00019     applet does not send any data to wims....
00020 */
00021 
00022 import java.awt.event.*;
00023 import java.awt.*;
00024 import java.applet.*;
00025 import java.net.*;
00026 import java.applet.*;
00027 import java.util.Vector;
00028 
00029 public class noninput extends Applet{
00030     inp_panel panel;
00031     inp_controls controls;
00032     Image bg;
00033     Color bgcolor;
00034 /*    String replystring;
00035     public static String prompt="OK";
00036     public static String retry="Erase";*/
00037     URL backurl;
00038 
00039     public void init() {
00040        URL url;
00041        String parmstr;
00042        parmstr=getParameter("type");
00043        if(parmstr!=null && parmstr.length()>0) {
00044            parmstr.toLowerCase(); parmstr.trim();
00045            if(parmstr.compareTo("curve")==0) inp_panel.ctype=inp_panel.CURVE;
00046            if(parmstr.compareTo("rectangle")==0) inp_panel.ctype=inp_panel.RECT;
00047            if(parmstr.compareTo("rect")==0) inp_panel.ctype=inp_panel.RECT;
00048            if(parmstr.compareTo("circle")==0) inp_panel.ctype=inp_panel.CIRCLE;
00049            if(parmstr.compareTo("lines")==0) inp_panel.ctype=inp_panel.LINES;
00050            if(parmstr.compareTo("segments")==0) inp_panel.ctype=inp_panel.LINES;
00051            if(parmstr.compareTo("line")==0) inp_panel.ctype=inp_panel.LINE;
00052            if(parmstr.compareTo("sline")==0) inp_panel.ctype=inp_panel.SLINE;
00053            if(parmstr.compareTo("semiline")==0) inp_panel.ctype=inp_panel.SLINE;
00054            if(parmstr.compareTo("seg")==0) inp_panel.ctype=inp_panel.SEG;
00055            if(parmstr.compareTo("segment")==0) inp_panel.ctype=inp_panel.SEG;
00056            if(parmstr.compareTo("poly")==0) inp_panel.ctype=inp_panel.POLY;
00057            if(parmstr.compareTo("polygon")==0) inp_panel.ctype=inp_panel.POLY;
00058            if(parmstr.compareTo("points")==0) inp_panel.ctype=inp_panel.POINTS;
00059        }
00060        
00061        parmstr=getParameter("background");
00062        if (parmstr!=null && parmstr.length()>0) {
00063            try {url=new URL(parmstr);}
00064            catch (MalformedURLException e) {url=null;}
00065            if(url!=null) bg=getImage(url);
00066            else bg=null;
00067        }
00068        else bg=null;
00069        
00070        parmstr=getParameter("bgcolor");
00071        if(parmstr!=null && parmstr.length()>0) {
00072            bgcolor=Color.decode(parmstr);
00073            if(bgcolor==null) bgcolor=Color.white;
00074        }
00075        else bgcolor=Color.white;
00076        
00077        
00078 /*     parmstr=getParameter("retry");
00079        if(parmstr!=null && parmstr.length()>0) {
00080            retry=parmstr;
00081        }*/
00082        
00083 /*     parmstr=getParameter("prompt");
00084        if(parmstr!=null && parmstr.length()>0) {
00085            prompt=parmstr;
00086        }*/
00087        
00088        setLayout(new BorderLayout());
00089        panel=new inp_panel(bgcolor,bg);
00090         controls=new inp_controls(panel,this);
00091        add("Center", panel);
00092        /*add("South",controls);*/
00093     }
00094 
00095     public void destroy() {
00096         remove(panel);
00097         /*remove(controls);*/
00098     }
00099 
00100     public static void main(String args[]) {
00101        Frame f=new Frame("input");
00102        noninput noninput=new noninput();
00103        noninput.init();
00104        noninput.start();
00105 
00106        f.add("Center", noninput);
00107        f.show();
00108     }
00109 
00110     public String getAppletInfo() {
00111         return "Curve input program for WIMS.";
00112     }
00113 }
00114 
00115 class inp_panel extends Panel implements MouseListener, MouseMotionListener {
00116     public static final int CURVE=0, RECT=1, CIRCLE=2, LINES=3, LINE=4,
00117      SLINE=5,SEG=6,POLY=7,POINTS=8;
00118     public static int ctype;
00119     static int ll=4;
00120     Image bg;
00121     Vector lines=new Vector(16384);
00122     int x1,y1;
00123     int x2,y2;
00124     int radius;
00125     int drag;
00126     int st;
00127 
00128     public inp_panel(Color bgcolor,Image gotbg) {
00129        setBackground(bgcolor);
00130        setForeground(Color.black);
00131        bg=gotbg;
00132        addMouseMotionListener(this);
00133        addMouseListener(this);
00134        st=0;
00135     }
00136    
00137 /*    public String points2string(String rep) {
00138        StringBuffer buf=new StringBuffer(16384);
00139        buf.append(rep);
00140        switch(ctype) {
00141            case POLY:
00142            case POINTS:
00143            case LINES:
00144            case CURVE: {
00145               int i, np;
00146               Point p=null;
00147               np=lines.size();
00148               for(i=0;i<np;i++) {
00149                   p=(Point)lines.elementAt(i);
00150                   buf.append(p.x).append(",").append(p.y).append(";");
00151               }
00152               break;
00153            }
00154            case SLINE:
00155            case LINE:
00156            case SEG:
00157            case RECT: {
00158               if(st>0) {
00159                   buf.append(x1).append(",").append(y1).append(",").append(x2).append(",").append(y2);
00160               }
00161               break;
00162            }
00163            case CIRCLE: {
00164               if(st>0) {
00165                   buf.append(x1).append(",").append(y1).append(",").append(radius);
00166               }
00167               break;
00168            }
00169        }
00170        return buf.toString();
00171     }
00172   */ 
00173     public void retry() {
00174        lines.removeAllElements();
00175        st=0; repaint();
00176     }
00177 
00178     public void mouseDragged(MouseEvent e) {
00179        int t=lines.size();
00180        int dr;
00181         e.consume();
00182        switch(ctype) {
00183            case CURVE: {
00184               if(t<2000) {
00185                   x1=e.getX(); y1=e.getY();
00186                   lines.addElement(new Point(x1, y1));
00187                   repaint();
00188               }
00189               st=2; return;
00190            }
00191            default: {
00192               dr=drag; mouseMoved(e); drag=dr+1;
00193               return;
00194            }
00195        }
00196     }
00197 
00198     public void mouseMoved(MouseEvent e) {
00199         e.consume(); drag=0;
00200        switch(ctype) {
00201            case POLY:
00202            case POINTS:
00203            case LINES:
00204            case CURVE: {
00205               if(st==0) return; else st=1;
00206               x2=e.getX(); y2=e.getY(); break;
00207            }
00208            case CIRCLE:
00209            case SLINE:
00210            case LINE:
00211            case SEG:
00212            case RECT: {
00213               if(st!=1) return;
00214               x2=e.getX(); y2=e.getY();
00215               radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
00216               break;
00217            }
00218            default: return;
00219        }
00220        if(ctype!=POINTS) repaint();
00221     }
00222 
00223     public void mousePressed(MouseEvent e) {
00224         e.consume();
00225        switch(ctype) {
00226            case CURVE: {
00227               if(st>0) return;
00228               x1=e.getX(); y1=e.getY(); x2=x1; y2=y1;
00229               lines.removeAllElements();
00230               lines.addElement(new Point(x1,y1));
00231               repaint(); st=2;
00232               return;
00233            }
00234            case POLY:
00235            case POINTS:
00236            case LINES: {
00237               x2=e.getX(); y2=e.getY();
00238               if(st==0) lines.removeAllElements();
00239               lines.addElement(new Point(x2,y2));
00240               st=1; x1=x2; y1=y2; repaint(); return;
00241            }
00242            case CIRCLE:
00243            case SLINE:
00244            case LINE:
00245            case SEG:
00246            case RECT: {
00247               x2=e.getX(); y2=e.getY();
00248               switch(st) {
00249                   case 2:
00250                   case 0: x1=x2; y1=y2; radius=0; st=1; repaint(); return;
00251                   case 1: {
00252                      radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
00253                      st=2; return;
00254                   }
00255               }             
00256               break;
00257            }
00258            default: return;
00259        }
00260     }
00261 
00262     public void mouseReleased(MouseEvent e) {
00263        e.consume();
00264        switch(ctype) {
00265            case CURVE: {
00266               if(st==2) return;
00267               x2=e.getX(); y2=e.getY();
00268               if(st==0) lines.removeAllElements();
00269               lines.addElement(new Point(x2,y2));
00270               st=1; x1=x2; y1=y2;
00271               return;
00272            }
00273            case POINTS: return;
00274            default: {
00275               if(st>0 && drag>=8) mousePressed(e);
00276               return;
00277            }
00278        }
00279     }
00280 
00281     public void mouseEntered(MouseEvent e) {
00282     }
00283 
00284     public void mouseExited(MouseEvent e) {
00285        if(ctype!=CURVE && ctype!=LINES && ctype!=POLY) return;
00286        e.consume();
00287        x2=x1; y2=y1; if(st>0) repaint();
00288     }
00289 
00290     public void mouseClicked(MouseEvent e) {
00291     }
00292 
00293     public void paint(Graphics g) {
00294        int np=lines.size();
00295        Point pp;
00296 
00297        if(bg!=null) g.drawImage(bg,0,0,this);
00298        switch(ctype) {
00299            case POINTS: {
00300               if(np>0) for (int i=0; i < np; i++) 
00301                 pointPaint(g,(Point)lines.elementAt(i));
00302               break;
00303            }
00304            case POLY:
00305            case CURVE:
00306            case LINES: {
00307               if(np>0) {
00308                   pp=(Point)lines.elementAt(0);
00309                   for (int i=1; i < np; i++) {
00310                      Point p=(Point)lines.elementAt(i);
00311                      g.drawLine(pp.x, pp.y, p.x, p.y);
00312                      pp=p;
00313                   }
00314                   if (st==1) g.drawLine(x1, y1, x2, y2);
00315               }
00316               if(ctype==POLY && np>1) {
00317                   Point p1=(Point)lines.elementAt(0);
00318                   Point p2=(Point)lines.elementAt(np-1);
00319                   if(st==0) g.drawLine(p1.x,p1.y,p2.x,p2.y);
00320                   else g.drawLine(p1.x,p1.y,x2,y2);
00321               }
00322               break;
00323            }
00324            case SEG:
00325            case SLINE:
00326            case LINE:
00327            case RECT: {
00328               if(st>0) {
00329                   int xx1,yy1,xx2,yy2;
00330                   int X1,Y1,X2,Y2,max;
00331                   xx1=Math.min(x1,x2); yy1=Math.min(y1,y2);
00332                   xx2=Math.max(x1,x2); yy2=Math.max(y1,y2);
00333                   max=Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
00334                   if(max<10) max=10;
00335                   max=500/max+1;
00336                   switch(ctype) {
00337                      case RECT: g.drawRect(xx1,yy1,xx2-xx1,yy2-yy1); break;
00338                      case SLINE: {
00339                          g.fillOval(x1-2,y1-2,4,4);
00340                          g.drawLine(x1,y1,max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
00341                      }
00342                      case LINE: {
00343                          g.fillOval(x1-2,y1-2,4,4);
00344                          g.fillOval(x2-2,y2-2,4,4);
00345                          g.drawLine(max*x1-(max-1)*x2,max*y1-(max-1)*y2,
00346                                    max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
00347                      }
00348                      case SEG: g.drawLine(x1,y1,x2,y2); break;
00349                   }
00350               }
00351               break;
00352            }
00353            case CIRCLE: {
00354               if(st>0) {
00355                   pointPaint(g,new Point(x1,y1));
00356                   g.drawOval(x1-radius,y1-radius,radius*2,radius*2);
00357               }
00358               break;
00359            }
00360        }
00361     }
00362    
00363     void pointPaint(Graphics g, Point p) {
00364        g.drawLine(p.x-ll,p.y+ll,p.x+ll,p.y-ll);
00365        g.drawLine(p.x+ll,p.y+ll,p.x-ll,p.y-ll);
00366     }
00367 }
00368 
00369 
00370 class inp_controls extends Panel implements ActionListener {
00371    noninput ci;
00372    inp_panel targ;
00373    Button retry, ok;
00374    
00375    public inp_controls(inp_panel pan, noninput cci) {
00376        this.ci=cci;
00377        targ=pan;
00378        setLayout(new GridLayout());
00379        setBackground(Color.white);
00380       /* retry=new Button(noninput.retry);*/
00381       /* retry.addActionListener(this);*/
00382        /*ok=new Button(noninput.prompt);
00383        ok.addActionListener(this);
00384        add(retry); add(ok);*/
00385    }
00386    public void actionPerformed(ActionEvent e) {
00387        Object src=e.getSource();
00388        if(src == retry) targ.retry();
00389    }
00390 }