Back to index

wims  3.65+svn20090927
input.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 import java.awt.event.*;
00019 import java.awt.*;
00020 import java.applet.*;
00021 import java.net.*;
00022 import java.applet.*;
00023 import java.util.Vector;
00024 
00025 public class input extends Applet{
00026     inp_panel panel;
00027     inp_controls controls;
00028     Image bg;
00029     Color bgcolor;
00030     String replystring;
00031     public static String prompt="OK";
00032     public static String retry="Erase";
00033     URL backurl;
00034 
00035     public void init() {
00036        URL url;
00037        String parmstr;
00038        parmstr=getParameter("type");
00039        if(parmstr!=null && parmstr.length()>0) {
00040            parmstr.toLowerCase(); parmstr.trim();
00041            if(parmstr.compareTo("curve")==0) inp_panel.ctype=inp_panel.CURVE;
00042            if(parmstr.compareTo("rectangle")==0) inp_panel.ctype=inp_panel.RECT;
00043            if(parmstr.compareTo("rect")==0) inp_panel.ctype=inp_panel.RECT;
00044            if(parmstr.compareTo("circle")==0) inp_panel.ctype=inp_panel.CIRCLE;
00045            if(parmstr.compareTo("lines")==0) inp_panel.ctype=inp_panel.LINES;
00046            if(parmstr.compareTo("segments")==0) inp_panel.ctype=inp_panel.LINES;
00047            if(parmstr.compareTo("line")==0) inp_panel.ctype=inp_panel.LINE;
00048            if(parmstr.compareTo("sline")==0) inp_panel.ctype=inp_panel.SLINE;
00049            if(parmstr.compareTo("semiline")==0) inp_panel.ctype=inp_panel.SLINE;
00050            if(parmstr.compareTo("seg")==0) inp_panel.ctype=inp_panel.SEG;
00051            if(parmstr.compareTo("segment")==0) inp_panel.ctype=inp_panel.SEG;
00052            if(parmstr.compareTo("poly")==0) inp_panel.ctype=inp_panel.POLY;
00053            if(parmstr.compareTo("polygon")==0) inp_panel.ctype=inp_panel.POLY;
00054            if(parmstr.compareTo("points")==0) inp_panel.ctype=inp_panel.POINTS;
00055            if(parmstr.compareTo("vec")==0) inp_panel.ctype=inp_panel.VEC;
00056            if(parmstr.compareTo("vector")==0) inp_panel.ctype=inp_panel.VEC;
00057        }
00058        
00059        parmstr=getParameter("background");
00060        if (parmstr!=null && parmstr.length()>0) {
00061            try {url=new URL(parmstr);}
00062            catch (MalformedURLException e) {url=null;}
00063            if(url!=null) bg=getImage(url);
00064            else bg=null;
00065        }
00066        else bg=null;
00067        
00068        parmstr=getParameter("bgcolor");
00069        if(parmstr!=null && parmstr.length()>0) {
00070            bgcolor=Color.decode(parmstr);
00071            if(bgcolor==null) bgcolor=Color.white;
00072        }
00073        else bgcolor=Color.white;
00074        
00075        parmstr=getParameter("replyurl");
00076        if(parmstr!=null && parmstr.length()>0) {
00077            replystring=parmstr;
00078        }
00079        else replystring="http://wims.unice.fr/";
00080        
00081        parmstr=getParameter("retry");
00082        if(parmstr!=null && parmstr.length()>0) {
00083            retry=parmstr;
00084        }
00085        
00086        parmstr=getParameter("prompt");
00087        if(parmstr!=null && parmstr.length()>0) {
00088            prompt=parmstr;
00089        }
00090        
00091        setLayout(new BorderLayout());
00092        panel=new inp_panel(bgcolor,bg);
00093         controls=new inp_controls(panel,this);
00094        add("Center", panel);
00095        add("South",controls);
00096     }
00097 
00098     public void destroy() {
00099         remove(panel);
00100         remove(controls);
00101     }
00102 
00103     public static void main(String args[]) {
00104        Frame f=new Frame("input");
00105        input input=new input();
00106        input.init();
00107        input.start();
00108 
00109        f.add("Center", input);
00110        f.show();
00111     }
00112 
00113     public void replyback() {
00114        try {backurl=new URL(panel.points2string(replystring));}
00115        catch (MalformedURLException e) {backurl=null;}
00116        getAppletContext().showDocument(backurl,"_self");
00117        destroy();
00118     }
00119     
00120     public String getAppletInfo() {
00121         return "Curve input program for WIMS.";
00122     }
00123 }
00124 
00125 class inp_panel extends Panel implements MouseListener, MouseMotionListener {
00126     public static final int CURVE=0, RECT=1, CIRCLE=2, LINES=3, LINE=4,
00127      SLINE=5,SEG=6,POLY=7,POINTS=8,VEC=9;
00128     public static int ctype;
00129     static int ll=4;
00130     Image bg;
00131     Vector lines=new Vector(16384);
00132     int x1,y1;
00133     int x2,y2;
00134     int radius;
00135     int drag;
00136     int st;
00137 
00138     public inp_panel(Color bgcolor,Image gotbg) {
00139        setBackground(bgcolor);
00140        setForeground(Color.black);
00141        bg=gotbg;
00142        addMouseMotionListener(this);
00143        addMouseListener(this);
00144        st=0;
00145     }
00146    
00147     public String points2string(String rep) {
00148        StringBuffer buf=new StringBuffer(16384);
00149        buf.append(rep);
00150        switch(ctype) {
00151            case POLY:
00152            case POINTS:
00153            case LINES:
00154            case CURVE: {
00155               int i, np;
00156               Point p=null;
00157               np=lines.size();
00158               for(i=0;i<np;i++) {
00159                   p=(Point)lines.elementAt(i);
00160                   buf.append(p.x).append(",").append(p.y).append(";");
00161               }
00162               break;
00163            }
00164            case SLINE:
00165            case LINE:
00166            case SEG:
00167            case VEC:
00168            case RECT: {
00169               if(st>0) {
00170                   buf.append(x1).append(",").append(y1).append(",").append(x2).append(",").append(y2);
00171               }
00172               break;
00173            }
00174            case CIRCLE: {
00175               if(st>0) {
00176                   buf.append(x1).append(",").append(y1).append(",").append(radius);
00177               }
00178               break;
00179            }
00180        }
00181        return buf.toString();
00182     }
00183    
00184     public void retry() {
00185        lines.removeAllElements();
00186        st=0; repaint();
00187     }
00188 
00189     public void mouseDragged(MouseEvent e) {
00190        int t=lines.size();
00191        int dr;
00192         e.consume();
00193        switch(ctype) {
00194            case CURVE: {
00195               if(t<2000) {
00196                   x1=e.getX(); y1=e.getY();
00197                   lines.addElement(new Point(x1, y1));
00198                   repaint();
00199               }
00200               st=2; return;
00201            }
00202            default: {
00203               dr=drag; mouseMoved(e); drag=dr+1;
00204               return;
00205            }
00206        }
00207     }
00208 
00209     public void mouseMoved(MouseEvent e) {
00210         e.consume(); drag=0;
00211        switch(ctype) {
00212            case POLY:
00213            case POINTS:
00214            case LINES:
00215            case CURVE: {
00216               if(st==0) return; else st=1;
00217               x2=e.getX(); y2=e.getY(); break;
00218            }
00219            case CIRCLE:
00220            case SLINE:
00221            case LINE:
00222            case SEG:
00223            case VEC:
00224            case RECT: {
00225               if(st!=1) return;
00226               x2=e.getX(); y2=e.getY();
00227               radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
00228               break;
00229            }
00230            default: return;
00231        }
00232        if(ctype!=POINTS) repaint();
00233     }
00234 
00235     public void mousePressed(MouseEvent e) {
00236         e.consume();
00237        switch(ctype) {
00238            case CURVE: {
00239               if(st>0) return;
00240               x1=e.getX(); y1=e.getY(); x2=x1; y2=y1;
00241               lines.removeAllElements();
00242               lines.addElement(new Point(x1,y1));
00243               repaint(); st=2;
00244               return;
00245            }
00246            case POLY:
00247            case POINTS:
00248            case LINES: {
00249               x2=e.getX(); y2=e.getY();
00250               if(st==0) lines.removeAllElements();
00251               lines.addElement(new Point(x2,y2));
00252               st=1; x1=x2; y1=y2; repaint(); return;
00253            }
00254            case CIRCLE:
00255            case SLINE:
00256            case LINE:
00257            case SEG:
00258            case VEC:
00259            case RECT: {
00260               x2=e.getX(); y2=e.getY();
00261               switch(st) {
00262                   case 2:
00263                   case 0: x1=x2; y1=y2; radius=0; st=1; repaint(); return;
00264                   case 1: {
00265                      radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
00266                      st=2; return;
00267                   }
00268               }             
00269               break;
00270            }
00271            default: return;
00272        }
00273     }
00274 
00275     public void mouseReleased(MouseEvent e) {
00276        e.consume();
00277        switch(ctype) {
00278            case CURVE: {
00279               if(st==2) return;
00280               x2=e.getX(); y2=e.getY();
00281               if(st==0) lines.removeAllElements();
00282               lines.addElement(new Point(x2,y2));
00283               st=1; x1=x2; y1=y2;
00284               return;
00285            }
00286            case POINTS: return;
00287            default: {
00288               if(st>0 && drag>=8) mousePressed(e);
00289               return;
00290            }
00291        }
00292     }
00293 
00294     public void mouseEntered(MouseEvent e) {
00295     }
00296 
00297     public void mouseExited(MouseEvent e) {
00298        if(ctype!=CURVE && ctype!=LINES && ctype!=POLY) return;
00299        e.consume();
00300        x2=x1; y2=y1; if(st>0) repaint();
00301     }
00302 
00303     public void mouseClicked(MouseEvent e) {
00304     }
00305 
00306     public void paint(Graphics g) {
00307        int np=lines.size();
00308        Point pp;
00309 
00310        if(bg!=null) g.drawImage(bg,0,0,this);
00311        switch(ctype) {
00312            case POINTS: {
00313               if(np>0) for (int i=0; i < np; i++) 
00314                 pointPaint(g,(Point)lines.elementAt(i));
00315               break;
00316            }
00317            case POLY:
00318            case CURVE:
00319            case LINES: {
00320               if(np>0) {
00321                   pp=(Point)lines.elementAt(0);
00322                   for (int i=1; i < np; i++) {
00323                      Point p=(Point)lines.elementAt(i);
00324                      g.drawLine(pp.x, pp.y, p.x, p.y);
00325                      pp=p;
00326                   }
00327                   if (st==1) g.drawLine(x1, y1, x2, y2);
00328               }
00329               if(ctype==POLY && np>1) {
00330                   Point p1=(Point)lines.elementAt(0);
00331                   Point p2=(Point)lines.elementAt(np-1);
00332                   if(st==0) g.drawLine(p1.x,p1.y,p2.x,p2.y);
00333                   else g.drawLine(p1.x,p1.y,x2,y2);
00334               }
00335               break;
00336            }
00337            case SEG:
00338            case VEC:
00339            case SLINE:
00340            case LINE:
00341            case RECT: {
00342               if(st>0) {
00343                   int xx1,yy1,xx2,yy2;
00344                   int X1,Y1,X2,Y2,max;
00345                   xx1=Math.min(x1,x2); yy1=Math.min(y1,y2);
00346                   xx2=Math.max(x1,x2); yy2=Math.max(y1,y2);
00347                   max=Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
00348                   if(max<10) max=10;
00349                   max=500/max+1;
00350                   switch(ctype) {
00351                      case RECT: g.drawRect(xx1,yy1,xx2-xx1,yy2-yy1); break;
00352                      case SLINE: {
00353                          g.fillOval(x1-2,y1-2,4,4);
00354                          g.drawLine(x1,y1,max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
00355                      }
00356                      case LINE: {
00357                          g.fillOval(x1-2,y1-2,4,4);
00358                          g.fillOval(x2-2,y2-2,4,4);
00359                          g.drawLine(max*x1-(max-1)*x2,max*y1-(max-1)*y2,
00360                                    max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
00361                      }
00362                      case SEG: g.drawLine(x1,y1,x2,y2); break;
00363                      case VEC: {
00364                          g.drawLine(x1,y1,x2,y2);
00365                          if(radius > 5) { /* arrow head */
00366                             double dx,dy,dd[];
00367                             int xx[], yy[];
00368                             dd=new double[6];
00369                             xx=new int[3];
00370                             yy=new int[3];
00371                             double l=12;
00372                             double fat=0.3;
00373                             xx[0]=x2;yy[0]=y2;
00374                             dx=x1-x2; dy=y1-y2;
00375                             dd[0]=l*dx/radius; dd[1]=l*dy/radius;
00376                             dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
00377                             dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
00378                             xx[1]=(int) dd[2]+xx[0];
00379                             yy[1]=(int) dd[3]+yy[0];
00380                             xx[2]=(int) dd[4]+xx[0];
00381                             yy[2]=(int) dd[5]+yy[0];
00382                             g.fillPolygon(xx,yy,3);
00383                          }
00384                          break;
00385                      }
00386                   }
00387               }
00388               break;
00389            }
00390            case CIRCLE: {
00391               if(st>0) {
00392                   pointPaint(g,new Point(x1,y1));
00393                   g.drawOval(x1-radius,y1-radius,radius*2,radius*2);
00394               }
00395               break;
00396            }
00397        }
00398     }
00399    
00400     void pointPaint(Graphics g, Point p) {
00401        g.drawLine(p.x-ll,p.y+ll,p.x+ll,p.y-ll);
00402        g.drawLine(p.x+ll,p.y+ll,p.x-ll,p.y-ll);
00403     }
00404 }
00405 
00406 
00407 class inp_controls extends Panel implements ActionListener {
00408    input ci;
00409    inp_panel targ;
00410    Button retry, ok;
00411    
00412    public inp_controls(inp_panel pan, input cci) {
00413        this.ci=cci;
00414        targ=pan;
00415        setLayout(new GridLayout());
00416        setBackground(Color.white);
00417        retry=new Button(input.retry);
00418        retry.addActionListener(this);
00419        ok=new Button(input.prompt);
00420        ok.addActionListener(this);
00421        add(retry); add(ok);
00422    }
00423 
00424    public void actionPerformed(ActionEvent e) {
00425        Object src=e.getSource();
00426        if(src == retry) targ.retry();
00427        if(src == ok) ci.replyback();
00428    }
00429   
00430 }
00431