Back to index

moin  1.9.0~rc2
PolygonTool.java
Go to the documentation of this file.
00001 /*
00002  * Fri Feb 28 07:47:05 1997  Doug Lea  (dl at gee)
00003  * Based on ScribbleTool
00004  */
00005 
00006 package CH.ifa.draw.contrib;
00007 
00008 import java.awt.*;
00009 import java.awt.event.MouseEvent;
00010 import java.awt.event.KeyEvent;
00011 import java.util.*;
00012 import java.io.IOException;
00013 import CH.ifa.draw.framework.*;
00014 import CH.ifa.draw.util.*;
00015 import CH.ifa.draw.standard.*;
00016 
00017 
00020 public class PolygonTool extends AbstractTool {
00021 
00022   private PolygonFigure  fPolygon;
00023   private int            fLastX, fLastY;
00024 
00025   public PolygonTool(DrawingView view) {
00026     super(view);
00027   }
00028 
00029   public void activate() {
00030     super.activate();
00031     fPolygon = null;
00032   }
00033 
00034   public void deactivate() {
00035     super.deactivate();
00036     if (fPolygon != null) {
00037       fPolygon.smoothPoints();
00038       if (fPolygon.pointCount() < 3 ||
00039           fPolygon.size().width < 4 || fPolygon.size().height < 4)
00040         drawing().remove(fPolygon);
00041     }
00042     fPolygon = null;
00043   }
00044 
00045   private void addPoint(int x, int y) {
00046       if (fPolygon == null) {
00047           fPolygon = new PolygonFigure(x, y);
00048           view().add(fPolygon);
00049           fPolygon.addPoint(x, y);
00050       } else if (fLastX != x || fLastY != y)
00051           fPolygon.addPoint(x, y);
00052 
00053       fLastX = x;
00054       fLastY = y;
00055   }
00056 
00057   public void mouseDown(MouseEvent e, int x, int y) {
00058     // replace pts by actual event pts
00059     x = e.getX();
00060     y = e.getY();
00061 
00062     if (e.getClickCount() >= 2) {
00063         if (fPolygon != null) {
00064             fPolygon.smoothPoints();
00065             editor().toolDone();
00066         }
00067         fPolygon = null;
00068 
00069     } else {
00070         // use original event coordinates to avoid
00071         // supress that the scribble is constrained to
00072         // the grid
00073         addPoint(e.getX(), e.getY());
00074     }
00075   }
00076 
00077     public void keyDown(KeyEvent e, int key) {
00078        System.out.println("Key " + key);
00079        if (key == 27) {
00080            if (fPolygon != null) {
00081               fPolygon.smoothPoints();
00082               editor().toolDone();
00083            }
00084            fPolygon = null;
00085        }
00086     }
00087 
00088   public void mouseMove(MouseEvent e, int x, int y) {
00089     if (fPolygon != null) {
00090        if (fPolygon.pointCount() > 1) {
00091            fPolygon.setPointAt(new Point(x, y), fPolygon.pointCount()-1);
00092            view().checkDamage();
00093        }
00094     }
00095   }
00096 
00097   public void mouseDrag(MouseEvent e, int x, int y) {
00098     // replace pts by actual event pts
00099     x = e.getX();
00100     y = e.getY();
00101     addPoint(x, y);
00102   }
00103 
00104 
00105   public void mouseUp(MouseEvent e, int x, int y) {
00106   }
00107 
00108 }