Subversion Repositories wimsdev

Rev

Blame | Last modification | View Log | RSS feed

  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. import java.awt.event.*;
  19. import java.awt.*;
  20. import java.applet.*;
  21. import java.net.*;
  22. import java.applet.*;
  23. import java.util.Vector;
  24.  
  25. public class input extends Applet{
  26.     inp_panel panel;
  27.     inp_controls controls;
  28.     Image bg;
  29.     Color bgcolor;
  30.     String replystring;
  31.     public static String prompt="OK";
  32.     public static String retry="Erase";
  33.     URL backurl;
  34.  
  35.     public void init() {
  36.         URL url;
  37.         String parmstr;
  38.         parmstr=getParameter("type");
  39.         if(parmstr!=null && parmstr.length()>0) {
  40.             parmstr.toLowerCase(); parmstr.trim();
  41.             if(parmstr.compareTo("curve")==0) inp_panel.ctype=inp_panel.CURVE;
  42.             if(parmstr.compareTo("rectangle")==0) inp_panel.ctype=inp_panel.RECT;
  43.             if(parmstr.compareTo("rect")==0) inp_panel.ctype=inp_panel.RECT;
  44.             if(parmstr.compareTo("circle")==0) inp_panel.ctype=inp_panel.CIRCLE;
  45.             if(parmstr.compareTo("lines")==0) inp_panel.ctype=inp_panel.LINES;
  46.             if(parmstr.compareTo("segments")==0) inp_panel.ctype=inp_panel.LINES;
  47.             if(parmstr.compareTo("line")==0) inp_panel.ctype=inp_panel.LINE;
  48.             if(parmstr.compareTo("sline")==0) inp_panel.ctype=inp_panel.SLINE;
  49.             if(parmstr.compareTo("semiline")==0) inp_panel.ctype=inp_panel.SLINE;
  50.             if(parmstr.compareTo("seg")==0) inp_panel.ctype=inp_panel.SEG;
  51.             if(parmstr.compareTo("segment")==0) inp_panel.ctype=inp_panel.SEG;
  52.             if(parmstr.compareTo("poly")==0) inp_panel.ctype=inp_panel.POLY;
  53.             if(parmstr.compareTo("polygon")==0) inp_panel.ctype=inp_panel.POLY;
  54.             if(parmstr.compareTo("points")==0) inp_panel.ctype=inp_panel.POINTS;
  55.             if(parmstr.compareTo("vec")==0) inp_panel.ctype=inp_panel.VEC;
  56.             if(parmstr.compareTo("vector")==0) inp_panel.ctype=inp_panel.VEC;
  57.         }
  58.        
  59.         parmstr=getParameter("background");
  60.         if (parmstr!=null && parmstr.length()>0) {
  61.             try {url=new URL(parmstr);}
  62.             catch (MalformedURLException e) {url=null;}
  63.             if(url!=null) bg=getImage(url);
  64.             else bg=null;
  65.         }
  66.         else bg=null;
  67.        
  68.         parmstr=getParameter("bgcolor");
  69.         if(parmstr!=null && parmstr.length()>0) {
  70.             bgcolor=Color.decode(parmstr);
  71.             if(bgcolor==null) bgcolor=Color.white;
  72.         }
  73.         else bgcolor=Color.white;
  74.        
  75.         parmstr=getParameter("replyurl");
  76.         if(parmstr!=null && parmstr.length()>0) {
  77.             replystring=parmstr;
  78.         }
  79.         else replystring="http://wims.unice.fr/";
  80.        
  81.         parmstr=getParameter("retry");
  82.         if(parmstr!=null && parmstr.length()>0) {
  83.             retry=parmstr;
  84.         }
  85.        
  86.         parmstr=getParameter("prompt");
  87.         if(parmstr!=null && parmstr.length()>0) {
  88.             prompt=parmstr;
  89.         }
  90.        
  91.         setLayout(new BorderLayout());
  92.         panel=new inp_panel(bgcolor,bg);
  93.         controls=new inp_controls(panel,this);
  94.         add("Center", panel);
  95.         add("South",controls);
  96.     }
  97.  
  98.     public void destroy() {
  99.         remove(panel);
  100.         remove(controls);
  101.     }
  102.  
  103.     public static void main(String args[]) {
  104.         Frame f=new Frame("input");
  105.         input input=new input();
  106.         input.init();
  107.         input.start();
  108.  
  109.         f.add("Center", input);
  110.         f.show();
  111.     }
  112.  
  113.     public void replyback() {
  114.         try {backurl=new URL(panel.points2string(replystring));}
  115.         catch (MalformedURLException e) {backurl=null;}
  116.         getAppletContext().showDocument(backurl,"_self");
  117.         destroy();
  118.     }
  119.    
  120.     public String getAppletInfo() {
  121.         return "Curve input program for WIMS.";
  122.     }
  123. }
  124.  
  125. class inp_panel extends Panel implements MouseListener, MouseMotionListener {
  126.     public static final int CURVE=0, RECT=1, CIRCLE=2, LINES=3, LINE=4,
  127.      SLINE=5,SEG=6,POLY=7,POINTS=8,VEC=9;
  128.     public static int ctype;
  129.     static int ll=4;
  130.     Image bg;
  131.     Vector lines=new Vector(16384);
  132.     int x1,y1;
  133.     int x2,y2;
  134.     int radius;
  135.     int drag;
  136.     int st;
  137.  
  138.     public inp_panel(Color bgcolor,Image gotbg) {
  139.         setBackground(bgcolor);
  140.         setForeground(Color.black);
  141.         bg=gotbg;
  142.         addMouseMotionListener(this);
  143.         addMouseListener(this);
  144.         st=0;
  145.     }
  146.    
  147.     public String points2string(String rep) {
  148.         StringBuffer buf=new StringBuffer(16384);
  149.         buf.append(rep);
  150.         switch(ctype) {
  151.             case POLY:
  152.             case POINTS:
  153.             case LINES:
  154.             case CURVE: {
  155.                 int i, np;
  156.                 Point p=null;
  157.                 np=lines.size();
  158.                 for(i=0;i<np;i++) {
  159.                     p=(Point)lines.elementAt(i);
  160.                     buf.append(p.x).append(",").append(p.y).append(";");
  161.                 }
  162.                 break;
  163.             }
  164.             case SLINE:
  165.             case LINE:
  166.             case SEG:
  167.             case VEC:
  168.             case RECT: {
  169.                 if(st>0) {
  170.                     buf.append(x1).append(",").append(y1).append(",").append(x2).append(",").append(y2);
  171.                 }
  172.                 break;
  173.             }
  174.             case CIRCLE: {
  175.                 if(st>0) {
  176.                     buf.append(x1).append(",").append(y1).append(",").append(radius);
  177.                 }
  178.                 break;
  179.             }
  180.         }
  181.         return buf.toString();
  182.     }
  183.    
  184.     public void retry() {
  185.         lines.removeAllElements();
  186.         st=0; repaint();
  187.     }
  188.  
  189.     public void mouseDragged(MouseEvent e) {
  190.         int t=lines.size();
  191.         int dr;
  192.         e.consume();
  193.         switch(ctype) {
  194.             case CURVE: {
  195.                 if(t<2000) {
  196.                     x1=e.getX(); y1=e.getY();
  197.                     lines.addElement(new Point(x1, y1));
  198.                     repaint();
  199.                 }
  200.                 st=2; return;
  201.             }
  202.             default: {
  203.                 dr=drag; mouseMoved(e); drag=dr+1;
  204.                 return;
  205.             }
  206.         }
  207.     }
  208.  
  209.     public void mouseMoved(MouseEvent e) {
  210.         e.consume(); drag=0;
  211.         switch(ctype) {
  212.             case POLY:
  213.             case POINTS:
  214.             case LINES:
  215.             case CURVE: {
  216.                 if(st==0) return; else st=1;
  217.                 x2=e.getX(); y2=e.getY(); break;
  218.             }
  219.             case CIRCLE:
  220.             case SLINE:
  221.             case LINE:
  222.             case SEG:
  223.             case VEC:
  224.             case RECT: {
  225.                 if(st!=1) return;
  226.                 x2=e.getX(); y2=e.getY();
  227.                 radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
  228.                 break;
  229.             }
  230.             default: return;
  231.         }
  232.         if(ctype!=POINTS) repaint();
  233.     }
  234.  
  235.     public void mousePressed(MouseEvent e) {
  236.         e.consume();
  237.         switch(ctype) {
  238.             case CURVE: {
  239.                 if(st>0) return;
  240.                 x1=e.getX(); y1=e.getY(); x2=x1; y2=y1;
  241.                 lines.removeAllElements();
  242.                 lines.addElement(new Point(x1,y1));
  243.                 repaint(); st=2;
  244.                 return;
  245.             }
  246.             case POLY:
  247.             case POINTS:
  248.             case LINES: {
  249.                 x2=e.getX(); y2=e.getY();
  250.                 if(st==0) lines.removeAllElements();
  251.                 lines.addElement(new Point(x2,y2));
  252.                 st=1; x1=x2; y1=y2; repaint(); return;
  253.             }
  254.             case CIRCLE:
  255.             case SLINE:
  256.             case LINE:
  257.             case SEG:
  258.             case VEC:
  259.             case RECT: {
  260.                 x2=e.getX(); y2=e.getY();
  261.                 switch(st) {
  262.                     case 2:
  263.                     case 0: x1=x2; y1=y2; radius=0; st=1; repaint(); return;
  264.                     case 1: {
  265.                         radius=(int) Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
  266.                         st=2; return;
  267.                     }
  268.                 }              
  269.                 break;
  270.             }
  271.             default: return;
  272.         }
  273.     }
  274.  
  275.     public void mouseReleased(MouseEvent e) {
  276.         e.consume();
  277.         switch(ctype) {
  278.             case CURVE: {
  279.                 if(st==2) return;
  280.                 x2=e.getX(); y2=e.getY();
  281.                 if(st==0) lines.removeAllElements();
  282.                 lines.addElement(new Point(x2,y2));
  283.                 st=1; x1=x2; y1=y2;
  284.                 return;
  285.             }
  286.             case POINTS: return;
  287.             default: {
  288.                 if(st>0 && drag>=8) mousePressed(e);
  289.                 return;
  290.             }
  291.         }
  292.     }
  293.  
  294.     public void mouseEntered(MouseEvent e) {
  295.     }
  296.  
  297.     public void mouseExited(MouseEvent e) {
  298.         if(ctype!=CURVE && ctype!=LINES && ctype!=POLY) return;
  299.         e.consume();
  300.         x2=x1; y2=y1; if(st>0) repaint();
  301.     }
  302.  
  303.     public void mouseClicked(MouseEvent e) {
  304.     }
  305.  
  306.     public void paint(Graphics g) {
  307.         int np=lines.size();
  308.         Point pp;
  309.  
  310.         if(bg!=null) g.drawImage(bg,0,0,this);
  311.         switch(ctype) {
  312.             case POINTS: {
  313.                 if(np>0) for (int i=0; i < np; i++)
  314.                   pointPaint(g,(Point)lines.elementAt(i));
  315.                 break;
  316.             }
  317.             case POLY:
  318.             case CURVE:
  319.             case LINES: {
  320.                 if(np>0) {
  321.                     pp=(Point)lines.elementAt(0);
  322.                     for (int i=1; i < np; i++) {
  323.                         Point p=(Point)lines.elementAt(i);
  324.                         g.drawLine(pp.x, pp.y, p.x, p.y);
  325.                         pp=p;
  326.                     }
  327.                     if (st==1) g.drawLine(x1, y1, x2, y2);
  328.                 }
  329.                 if(ctype==POLY && np>1) {
  330.                     Point p1=(Point)lines.elementAt(0);
  331.                     Point p2=(Point)lines.elementAt(np-1);
  332.                     if(st==0) g.drawLine(p1.x,p1.y,p2.x,p2.y);
  333.                     else g.drawLine(p1.x,p1.y,x2,y2);
  334.                 }
  335.                 break;
  336.             }
  337.             case SEG:
  338.             case VEC:
  339.             case SLINE:
  340.             case LINE:
  341.             case RECT: {
  342.                 if(st>0) {
  343.                     int xx1,yy1,xx2,yy2;
  344.                     int X1,Y1,X2,Y2,max;
  345.                     xx1=Math.min(x1,x2); yy1=Math.min(y1,y2);
  346.                     xx2=Math.max(x1,x2); yy2=Math.max(y1,y2);
  347.                     max=Math.max(Math.abs(x2-x1),Math.abs(y2-y1));
  348.                     if(max<10) max=10;
  349.                     max=500/max+1;
  350.                     switch(ctype) {
  351.                         case RECT: g.drawRect(xx1,yy1,xx2-xx1,yy2-yy1); break;
  352.                         case SLINE: {
  353.                             g.fillOval(x1-2,y1-2,4,4);
  354.                             g.drawLine(x1,y1,max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
  355.                         }
  356.                         case LINE: {
  357.                             g.fillOval(x1-2,y1-2,4,4);
  358.                             g.fillOval(x2-2,y2-2,4,4);
  359.                             g.drawLine(max*x1-(max-1)*x2,max*y1-(max-1)*y2,
  360.                                        max*x2-(max-1)*x1,max*y2-(max-1)*y1); break;
  361.                         }
  362.                         case SEG: g.drawLine(x1,y1,x2,y2); break;
  363.                         case VEC: {
  364.                             g.drawLine(x1,y1,x2,y2);
  365.                             if(radius > 5) { /* arrow head */
  366.                                 double dx,dy,dd[];
  367.                                 int xx[], yy[];
  368.                                 dd=new double[6];
  369.                                 xx=new int[3];
  370.                                 yy=new int[3];
  371.                                 double l=12;
  372.                                 double fat=0.3;
  373.                                 xx[0]=x2;yy[0]=y2;
  374.                                 dx=x1-x2; dy=y1-y2;
  375.                                 dd[0]=l*dx/radius; dd[1]=l*dy/radius;
  376.                                 dd[2]=dd[0]+dd[1]*fat; dd[3]=dd[1]-dd[0]*fat;
  377.                                 dd[4]=dd[0]-dd[1]*fat; dd[5]=dd[1]+dd[0]*fat;
  378.                                 xx[1]=(int) dd[2]+xx[0];
  379.                                 yy[1]=(int) dd[3]+yy[0];
  380.                                 xx[2]=(int) dd[4]+xx[0];
  381.                                 yy[2]=(int) dd[5]+yy[0];
  382.                                 g.fillPolygon(xx,yy,3);
  383.                             }
  384.                             break;
  385.                         }
  386.                     }
  387.                 }
  388.                 break;
  389.             }
  390.             case CIRCLE: {
  391.                 if(st>0) {
  392.                     pointPaint(g,new Point(x1,y1));
  393.                     g.drawOval(x1-radius,y1-radius,radius*2,radius*2);
  394.                 }
  395.                 break;
  396.             }
  397.         }
  398.     }
  399.    
  400.     void pointPaint(Graphics g, Point p) {
  401.         g.drawLine(p.x-ll,p.y+ll,p.x+ll,p.y-ll);
  402.         g.drawLine(p.x+ll,p.y+ll,p.x-ll,p.y-ll);
  403.     }
  404. }
  405.  
  406.  
  407. class inp_controls extends Panel implements ActionListener {
  408.    input ci;
  409.    inp_panel targ;
  410.    Button retry, ok;
  411.    
  412.    public inp_controls(inp_panel pan, input cci) {
  413.        this.ci=cci;
  414.        targ=pan;
  415.        setLayout(new GridLayout());
  416.        setBackground(Color.white);
  417.        retry=new Button(input.retry);
  418.        retry.addActionListener(this);
  419.        ok=new Button(input.prompt);
  420.        ok.addActionListener(this);
  421.        add(retry); add(ok);
  422.    }
  423.  
  424.    public void actionPerformed(ActionEvent e) {
  425.        Object src=e.getSource();
  426.        if(src == retry) targ.retry();
  427.        if(src == ok) ci.replyback();
  428.    }
  429.  
  430. }
  431.  
  432.