Exemplo n.º 1
0
  /**
   * Returns a path which is cappedPath at the ends, to prevent it from drawing under the end caps.
   */
  protected BezierPath getCappedPath() {
    if (cappedPath == null) {
      cappedPath = path.clone();
      if (isClosed()) {
        cappedPath.setClosed(true);
      } else {
        if (cappedPath.size() > 1) {
          if (get(START_DECORATION) != null) {
            BezierPath.Node p0 = cappedPath.get(0);
            BezierPath.Node p1 = cappedPath.get(1);
            Point2D.Double pp;
            if ((p0.getMask() & BezierPath.C2_MASK) != 0) {
              pp = p0.getControlPoint(2);
            } else if ((p1.getMask() & BezierPath.C1_MASK) != 0) {
              pp = p1.getControlPoint(1);
            } else {
              pp = p1.getControlPoint(0);
            }
            double radius = get(START_DECORATION).getDecorationRadius(this);
            double lineLength = Geom.length(p0.getControlPoint(0), pp);
            cappedPath.set(
                0, 0, Geom.cap(pp, p0.getControlPoint(0), -Math.min(radius, lineLength)));
          }
          if (get(END_DECORATION) != null) {
            BezierPath.Node p0 = cappedPath.get(cappedPath.size() - 1);
            BezierPath.Node p1 = cappedPath.get(cappedPath.size() - 2);

            Point2D.Double pp;
            if ((p0.getMask() & BezierPath.C1_MASK) != 0) {
              pp = p0.getControlPoint(1);
            } else if ((p1.getMask() & BezierPath.C2_MASK) != 0) {
              pp = p1.getControlPoint(2);
            } else {
              pp = p1.getControlPoint(0);
            }

            double radius = get(END_DECORATION).getDecorationRadius(this);
            double lineLength = Geom.length(p0.getControlPoint(0), pp);
            cappedPath.set(
                cappedPath.size() - 1,
                0,
                Geom.cap(pp, p0.getControlPoint(0), -Math.min(radius, lineLength)));
          }
          cappedPath.invalidatePath();
        }
      }
    }
    return cappedPath;
  }
Exemplo n.º 2
0
  @Override
  public void draw(Graphics2D g) {
    double opacity = get(OPACITY);
    opacity = Math.min(Math.max(0d, opacity), 1d);
    if (opacity != 0d) {
      if (opacity != 1d) {
        Rectangle2D.Double drawingArea = getDrawingArea();

        Rectangle2D clipBounds = g.getClipBounds();
        if (clipBounds != null) {
          Rectangle2D.intersect(drawingArea, clipBounds, drawingArea);
        }

        if (!drawingArea.isEmpty()) {

          BufferedImage buf =
              new BufferedImage(
                  (int) ((2 + drawingArea.width) * g.getTransform().getScaleX()),
                  (int) ((2 + drawingArea.height) * g.getTransform().getScaleY()),
                  BufferedImage.TYPE_INT_ARGB);
          Graphics2D gr = buf.createGraphics();
          gr.scale(g.getTransform().getScaleX(), g.getTransform().getScaleY());
          gr.translate((int) -drawingArea.x, (int) -drawingArea.y);
          gr.setRenderingHints(g.getRenderingHints());
          drawFigure(gr);
          gr.dispose();
          Composite savedComposite = g.getComposite();
          g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) opacity));
          g.drawImage(
              buf,
              (int) drawingArea.x,
              (int) drawingArea.y,
              2 + (int) drawingArea.width,
              2 + (int) drawingArea.height,
              null);
          g.setComposite(savedComposite);
        }
      } else {
        drawFigure(g);
      }
    }
  }
Exemplo n.º 3
0
 public void setFontSize(float size) {
   // FONT_SIZE.basicSet(this, new Double(size));
   Point2D.Double p = new Point2D.Double(0, size);
   AffineTransform tx = TRANSFORM.get(this);
   if (tx != null) {
     try {
       tx.inverseTransform(p, p);
       Point2D.Double p0 = new Point2D.Double(0, 0);
       tx.inverseTransform(p0, p0);
       p.y -= p0.y;
     } catch (NoninvertibleTransformException ex) {
       ex.printStackTrace();
     }
   }
   FONT_SIZE.set(this, Math.abs(p.y));
 }
Exemplo n.º 4
0
 public float getFontSize() {
   //   return FONT_SIZE.get(this).floatValue();
   Point2D.Double p = new Point2D.Double(0, FONT_SIZE.get(this));
   AffineTransform tx = TRANSFORM.get(this);
   if (tx != null) {
     tx.transform(p, p);
     Point2D.Double p0 = new Point2D.Double(0, 0);
     tx.transform(p0, p0);
     p.y -= p0.y;
     /*
     try {
         tx.inverseTransform(p, p);
     } catch (NoninvertibleTransformException ex) {
         ex.printStackTrace();
     }*/
   }
   return (float) Math.abs(p.y);
 }
Exemplo n.º 5
0
 @Override
 public boolean contains(Point2D.Double p) {
   double tolerance = Math.max(2f, AttributeKeys.getStrokeTotalWidth(this) / 2d);
   if (isClosed() || get(FILL_COLOR) != null && get(UNCLOSED_PATH_FILLED)) {
     if (path.contains(p)) {
       return true;
     }
     double grow = AttributeKeys.getPerpendicularHitGrowth(this) * 2d;
     GrowStroke gs =
         new GrowStroke(grow, AttributeKeys.getStrokeTotalWidth(this) * get(STROKE_MITER_LIMIT));
     if (gs.createStrokedShape(path).contains(p)) {
       return true;
     } else {
       if (isClosed()) {
         return false;
       }
     }
   }
   if (!isClosed()) {
     if (getCappedPath().outlineContains(p, tolerance)) {
       return true;
     }
     if (get(START_DECORATION) != null) {
       BezierPath cp = getCappedPath();
       Point2D.Double p1 = path.get(0, 0);
       Point2D.Double p2 = cp.get(0, 0);
       // FIXME - Check here, if caps path contains the point
       if (Geom.lineContainsPoint(p1.x, p1.y, p2.x, p2.y, p.x, p.y, tolerance)) {
         return true;
       }
     }
     if (get(END_DECORATION) != null) {
       BezierPath cp = getCappedPath();
       Point2D.Double p1 = path.get(path.size() - 1, 0);
       Point2D.Double p2 = cp.get(path.size() - 1, 0);
       // FIXME - Check here, if caps path contains the point
       if (Geom.lineContainsPoint(p1.x, p1.y, p2.x, p2.y, p.x, p.y, tolerance)) {
         return true;
       }
     }
   }
   return false;
 }
  protected void openView(final View view) {
    final Application app = getApplication();
    app.setEnabled(true);

    // If there is another view with the same URI we set the multiple open
    // id of our view to max(multiple open id) + 1.
    int multipleOpenId = 1;
    for (View aView : app.views()) {
      if (aView != view && aView.isEmpty()) {
        multipleOpenId = Math.max(multipleOpenId, aView.getMultipleOpenId() + 1);
      }
    }
    view.setMultipleOpenId(multipleOpenId);
    view.setEnabled(false);

    // Open the file
    view.execute(
        new Worker() {

          @Override
          protected Object construct() throws IOException {
            boolean exists = true;
            try {
              File f = new File(uri);
              exists = f.exists();
            } catch (IllegalArgumentException e) {
              // The URI does not denote a file, thus we can not check whether the file exists.
            }
            if (exists) {
              view.read(uri, null);
              return null;
            } else {
              ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels");
              throw new IOException(
                  labels.getFormatted("file.open.fileDoesNotExist.message", URIUtil.getName(uri)));
            }
          }

          @Override
          protected void done(Object value) {
            view.setURI(uri);
            Frame w = (Frame) SwingUtilities.getWindowAncestor(view.getComponent());
            if (w != null) {
              w.setExtendedState(w.getExtendedState() & ~Frame.ICONIFIED);
              w.toFront();
            }
            view.setEnabled(true);
            view.getComponent().requestFocus();
          }

          @Override
          protected void failed(Throwable value) {
            value.printStackTrace();
            String message = value.getMessage() != null ? value.getMessage() : value.toString();
            ResourceBundleUtil labels = ResourceBundleUtil.getBundle("org.jhotdraw.app.Labels");
            JSheet.showMessageSheet(
                view.getComponent(),
                "<html>"
                    + UIManager.getString("OptionPane.css")
                    + "<b>"
                    + labels.getFormatted("file.open.couldntOpen.message", URIUtil.getName(uri))
                    + "</b><p>"
                    + (message == null ? "" : message),
                JOptionPane.ERROR_MESSAGE,
                new SheetListener() {

                  @Override
                  public void optionSelected(SheetEvent evt) {
                    view.setEnabled(true);
                  }
                });
          }
        });
  }