Пример #1
0
 @Override
 public void colorButtonHit(Color c) {
   col = c;
   if (selectedIndex > -1) {
     Shape s = model.getShape(selectedIndex);
     s.setColor(col);
     model.setShape(selectedIndex, s);
   }
   GUIFunctions.changeSelectedColor(c);
 }
Пример #2
0
  @Override
  public void mousePressed(MouseEvent e) {
    AffineTransform viewToWorld =
        new AffineTransform(1, 0, 0, 1, viewPoint.getX(), viewPoint.getY());
    AffineTransform scale = new AffineTransform(this.getScale(), 0, 0, this.getScale(), 0, 0);
    viewToWorld.concatenate(scale);

    p1 = new Point2D.Double(e.getPoint().getX(), e.getPoint().getY());
    viewToWorld.transform(p1, p1);

    if (button == "line") {
      currentShape = new Line(col, p1, p1);
    } else if (button == "square") {
      currentShape = new Square(col, p1, 0);
    } else if (button == "rectangle") {
      currentShape = new Rectangle(col, p1, 0, 0);
    } else if (button == "circle") {
      currentShape = new Circle(col, p1, 0);
    } else if (button == "ellipse") {
      currentShape = new Ellipse(col, p1, 0, 0);
    } else if (button == "triangle") {
      if (triangleCount == 0) {
        t1 = new Point2D.Double(p1.getX(), p1.getY());
        triangleCount++;
      } else if (triangleCount == 1) {
        t2 = new Point2D.Double(p1.getX(), p1.getY());
        triangleCount++;
      } else {
        t3 = new Point2D.Double(p1.getX(), p1.getY());
        Point2D.Double center = new Point2D.Double();
        center.setLocation(
            (t1.getX() + t2.getX() + t3.getX()) / 3, (t1.getY() + t2.getY() + t3.getY()) / 3);
        triangleCount = 0;
        Shape t = new Triangle(col, center, t1, t2, t3);
        model.addShape(t);
      }
      return;
    } else if (button == "select") {
      selectedIndex = geometryTest(p1, 4);
      if (selectedIndex > -1) {
        Shape s = model.getShape(selectedIndex);
        GUIFunctions.changeSelectedColor(s.getColor());
        diff =
            new Point2D.Double(p1.getX() - s.getCenter().getX(), p1.getY() - s.getCenter().getY());
        if (doHandleCheck(p1, selectedShape)) {
          handleSelected = true;
          if (s instanceof Line) {
            Line l = (Line) s;
            Point2D.Double len =
                new Point2D.Double(
                    l.getEnd().getX() - l.getCenter().getX(),
                    l.getEnd().getY() - l.getCenter().getY());

            AffineTransform worldToObj =
                new AffineTransform(1, 0, 0, 1, -s.getCenter().getX(), -s.getCenter().getY());
            Point2D.Double objCoord = new Point2D.Double();
            worldToObj.transform(p1, objCoord);

            if (objCoord.getX() * objCoord.getX() + objCoord.getY() * objCoord.getY()
                < 100 * this.getScale()) {
              lineHandleSelected = 1;
            } else if ((objCoord.getX() - len.getX()) * (objCoord.getX() - len.getX())
                    + (objCoord.getY() - len.getY()) * (objCoord.getY() - len.getY())
                < 100 * this.getScale()) {
              lineHandleSelected = 2;
            }
          }
        }
      }
      return;
    } else {
      return;
    }
    model.addShape(currentShape);
  }