Example #1
0
  /**
   * Damages the area surrounding the caret to cause it to be repainted in a new location. If
   * paint() is reimplemented, this method should also be reimplemented. This method should update
   * the caret bounds (x, y, width, and height).
   *
   * @param r the current location of the caret
   * @see #paint
   */
  @Override
  protected synchronized void damage(final Rectangle r) {
    if (r == null || fPainting) return;

    x = r.x - 4;
    y = r.y;
    width = 10;
    height = r.height;

    // Don't damage the border area.  We can't paint a partial border, so get the
    // intersection of the caret rectangle and the component less the border, if any.
    final Rectangle caretRect = new Rectangle(x, y, width, height);
    final Border border = getComponent().getBorder();
    if (border != null) {
      final Rectangle alloc = getComponent().getBounds();
      alloc.x = alloc.y = 0;
      final Insets borderInsets = border.getBorderInsets(getComponent());
      alloc.x += borderInsets.left;
      alloc.y += borderInsets.top;
      alloc.width -= borderInsets.left + borderInsets.right;
      alloc.height -= borderInsets.top + borderInsets.bottom;
      Rectangle2D.intersect(caretRect, alloc, caretRect);
    }
    x = caretRect.x;
    y = caretRect.y;
    width = Math.max(caretRect.width, 1);
    height = Math.max(caretRect.height, 1);
    repaint();
  }
 /**
  * Returns a new {@link Rectangle2D} object representing the intersection of this <code>Rectangle
  * </code> with the specified <code>Rectangle2D</code>.
  *
  * @param r the <code>Rectangle2D</code> to be intersected with this <code>Rectangle</code>
  * @return the largest <code>Rectangle2D</code> contained in both the specified <code>Rectangle2D
  *     </code> and in this <code>Rectangle</code>.
  * @since 1.2
  */
 public Rectangle2D createIntersection(Rectangle2D r) {
   if (r instanceof Rectangle) {
     return intersection((Rectangle) r);
   }
   Rectangle2D dest = new Rectangle2D.Double();
   Rectangle2D.intersect(this, r, dest);
   return dest;
 }
 public static Rectangle computeIntersection(
     final int x, final int y, final int width, final int height, final Rectangle rect) {
   auxRect.setBounds(x, y, width, height);
   Rectangle2D.intersect(auxRect, rect, rect);
   if (rect.height < 0 || rect.width < 0) {
     rect.setBounds(0, 0, 0, 0);
   }
   return rect;
 }
  @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);
      }
    }
  }
  private boolean drawComposite(Rectangle2D bounds, ImageObserver observer) {
    // Clip source to visible areas that need updating
    Rectangle2D clip = this.getClipBounds();
    Rectangle2D.intersect(bounds, clip, bounds);
    clip =
        new Rectangle(
            buffer.getMinX(), buffer.getMinY(),
            buffer.getWidth(), buffer.getHeight());
    Rectangle2D.intersect(bounds, clip, bounds);

    BufferedImage buffer2 = buffer;
    if (!bounds.equals(buffer2.getRaster().getBounds()))
      buffer2 =
          buffer2.getSubimage(
              (int) bounds.getX(),
              (int) bounds.getY(),
              (int) bounds.getWidth(),
              (int) bounds.getHeight());

    // Get destination clip to bounds
    double[] points =
        new double[] {
          bounds.getX(), bounds.getY(),
          bounds.getMaxX(), bounds.getMaxY()
        };
    transform.transform(points, 0, points, 0, 2);

    Rectangle2D deviceBounds =
        new Rectangle2D.Double(points[0], points[1], points[2] - points[0], points[3] - points[1]);

    Rectangle2D.intersect(deviceBounds, this.getClipInDevSpace(), deviceBounds);

    // Get current image on the component
    GtkImage img = grab(component);
    Graphics gr = componentBuffer.createGraphics();
    gr.drawImage(img, 0, 0, null);
    gr.dispose();

    BufferedImage cBuffer = componentBuffer;
    if (!deviceBounds.equals(cBuffer.getRaster().getBounds()))
      cBuffer =
          cBuffer.getSubimage(
              (int) deviceBounds.getX(),
              (int) deviceBounds.getY(),
              (int) deviceBounds.getWidth(),
              (int) deviceBounds.getHeight());

    // Perform actual composite operation
    compCtx.compose(buffer2.getRaster(), cBuffer.getRaster(), cBuffer.getRaster());

    // This MUST call directly into the "action" method in CairoGraphics2D,
    // not one of the wrappers, to ensure that the composite isn't processed
    // more than once!
    boolean rv =
        super.drawImage(
            cBuffer,
            AffineTransform.getTranslateInstance(bounds.getX(), bounds.getY()),
            null,
            null);
    return rv;
  }