Java Code Examples for java.awt.Graphics2D#setComposite()

The following examples show how to use java.awt.Graphics2D#setComposite() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomCompositeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void renderTest(Graphics2D g2d, int w, int h) {
    g2d.setColor(Color.yellow);
    g2d.fillRect(0, 0, w, h);

    BufferedImage image = getTestImage();
    // draw original image
    g2d.drawRenderedImage(image, null);

    // draw image with custom composite
    g2d.translate(175, 25);
    Composite currentComposite = g2d.getComposite();
    g2d.setComposite(new TestComposite());
    g2d.drawRenderedImage(image, null);
    g2d.setComposite(currentComposite);

    // draw image with XOR
    g2d.translate(175, 25);
    g2d.setXORMode(Color.red);
    g2d.drawRenderedImage(image, null);


    System.out.println("Painting is done...");
}
 
Example 2
Source File: GhostGlassPane.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void paintComponent (Graphics g)
{
    if ((draggedImage == null) || (localPoint == null)) {
        return;
    }

    Graphics2D g2 = (Graphics2D) g;

    // Use composition with display underneath
    if (!overTarget) {
        g2.setComposite(nonTargetComposite);
    } else {
        g2.setComposite(targetComposite);
    }

    Rectangle rect = getImageBounds(localPoint);
    g2.drawImage(draggedImage, null, rect.x, rect.y);
}
 
Example 3
Source File: IncorrectClipSurface2SW.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void draw(Shape clip, Shape to, Image vi, BufferedImage bi,
                         int scale) {
    Graphics2D big = bi.createGraphics();
    big.setComposite(AlphaComposite.Src);
    big.setClip(clip);
    Rectangle toBounds = to.getBounds();
    int x1 = toBounds.x;

    int y1 = toBounds.y;
    int x2 = x1 + toBounds.width;
    int y2 = y1 + toBounds.height;
    big.drawImage(vi, x1, y1, x2, y2, 0, 0, toBounds.width / scale,
                  toBounds.height / scale, null);
    big.dispose();
    vi.flush();
}
 
Example 4
Source File: BarcodeTools.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static BufferedImage centerPicture(BufferedImage source,
        BufferedImage picture, int w, int h) {
    try {
        if (w <= 0 || h <= 0 || picture == null || picture.getWidth() == 0) {
            return source;
        }
        int width = source.getWidth();
        int height = source.getHeight();
        int ah = h, aw = w;
        if (w * 1.0f / h > picture.getWidth() * 1.0f / picture.getHeight()) {
            ah = picture.getHeight() * w / picture.getWidth();
        } else {
            aw = picture.getWidth() * h / picture.getHeight();
        }
        BufferedImage target = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = target.createGraphics();
        g.drawImage(source, 0, 0, width, height, null);
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
        g.drawImage(picture, (width - aw) / 2, (height - ah) / 2, aw, ah, null);
        g.dispose();
        return target;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}
 
Example 5
Source File: MapMarkerImg.java    From Course_Generator with GNU General Public License v3.0 6 votes vote down vote up
public void paint(Graphics g, Point position, int radio) {
	// int size_h = radio;
	// int size = img. size_h * 2;

	if (g instanceof Graphics2D && getBackColor() != null) {
		Graphics2D g2 = (Graphics2D) g;
		Composite oldComposite = g2.getComposite();
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
		g2.setPaint(getBackColor());
		g.drawImage(img, position.x - img.getWidth(null) / 2, position.y - img.getHeight(null) / 2, null);
		// g.fillOval(position.x - size_h, position.y - size_h, size, size);
		g2.setComposite(oldComposite);
	}
	g.setColor(getColor());
	g.drawImage(img, position.x - img.getWidth(null) / 2, position.y - img.getHeight(null) / 2, null);
	// g.drawOval(position.x - size_h, position.y - size_h, size, size);

	if (getLayer() == null || getLayer().isVisibleTexts())
		paintText(g, position);
}
 
Example 6
Source File: MovieScreen.java    From open-ig with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Paint a word-wrapped label.
 * @param g2 the graphics context.
 * @param x0 the X coordinate
 * @param y0 the Y coordinate
 * @param width the draw width
 * @param height the draw height
 */
public void paintLabel(Graphics2D g2, int x0, int y0, int width, int height) {
	List<String> lines = new ArrayList<>();
	int maxWidth = commons.text().wrapText(label, width, 14, lines);
	int y = height - lines.size() * 21 - 7;
	Composite cp = g2.getComposite();
	g2.setComposite(AlphaComposite.SrcOver.derive(0.8f));
	g2.fillRect(x0 + (width - maxWidth) / 2 - 3, y0 + y - 3, maxWidth + 6, lines.size() * 21 + 6);
	g2.setComposite(cp);
	for (String s : lines) {
		int tw = commons.text().getTextWidth(14, s);
		int x = (width - tw) / 2;
		commons.text().paintTo(g2, x0 + x, y0 + y, 14, TextRenderer.WHITE, s);
		y += 21;
	}
}
 
Example 7
Source File: SimpleUnmanagedImage.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void fill(final Image image) {
    final Graphics2D graphics = (Graphics2D) image.getGraphics();
    graphics.setComposite(AlphaComposite.Src);
    for (int i = 0; i < image.getHeight(null); ++i) {
        graphics.setColor(new Color(i, 0, 0));
        graphics.fillRect(0, i, image.getWidth(null), 1);
    }
    graphics.dispose();
}
 
Example 8
Source File: SpiderWebPlot.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Draws the label for one axis.
 *
 * @param g2  the graphics device.
 * @param plotArea  the plot area
 * @param value  the value of the label (ignored).
 * @param cat  the category (zero-based index).
 * @param startAngle  the starting angle.
 * @param extent  the extent of the arc.
 */
protected void drawLabel(Graphics2D g2, Rectangle2D plotArea, double value,
                         int cat, double startAngle, double extent) {
    FontRenderContext frc = g2.getFontRenderContext();

    String label;
    if (this.dataExtractOrder == TableOrder.BY_ROW) {
        // if series are in rows, then the categories are the column keys
        label = this.labelGenerator.generateColumnLabel(this.dataset, cat);
    }
    else {
        // if series are in columns, then the categories are the row keys
        label = this.labelGenerator.generateRowLabel(this.dataset, cat);
    }

    Rectangle2D labelBounds = getLabelFont().getStringBounds(label, frc);
    LineMetrics lm = getLabelFont().getLineMetrics(label, frc);
    double ascent = lm.getAscent();

    Point2D labelLocation = calculateLabelLocation(labelBounds, ascent,
            plotArea, startAngle);

    Composite saveComposite = g2.getComposite();

    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelLocation.getX(),
            (float) labelLocation.getY());
    g2.setComposite(saveComposite);
}
 
Example 9
Source File: JPEGsNotAcceleratedTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public BufferedImage getDestImage() {
    BufferedImage destBI =
        new BufferedImage(TEST_W, TEST_H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = (Graphics2D)destBI.getGraphics();
    g.setComposite(AlphaComposite.Src);
    g.setColor(Color.blue);
    g.fillRect(0, 0, TEST_W, TEST_H);
    return destBI;
}
 
Example 10
Source File: SpotlightLayerUI.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paint (Graphics g, JComponent c) {
  Graphics2D g2 = (Graphics2D)g.create();

  // Paint the view.
  super.paint (g2, c);

  if (mActive) {
   if (settlementMapPanel.isDaylightTrackingOn()) {
      // Create a radial gradient, transparent in the middle.
      java.awt.geom.Point2D center = new java.awt.geom.Point2D.Float(mX, mY);
      float radius = 60;
      float[] dist = {0.0f, 1.0f};
      //Color[] colors = {new Color(0.0f, 0.0f, 0.0f, 0.0f), Color.gray};

      Color[] colors = {
    		  //new Color(0.0f, 0.0f, 0.0f, 0.0f),
    		  //new Color(0, 0, 0, 0)
    		  //new Color(g2.getColor().getRed() * 0.25f, g2.getColor().getGreen() * 0.25f, g2.getColor().getBlue() * 0.25f, 0f),//, g.getColor().getAlpha()),
    		 //new Color(g2.getColor().getRed(), g2.getColor().getGreen(), g2.getColor().getBlue(), 0.0f)
    	  		new Color(.9f, .9f, .9f, .9f),
    	    	new Color(g2.getColor().getRed()/255f, g2.getColor().getGreen()/255f, g2.getColor().getBlue()/255f, 0.0f)

      };

      RadialGradientPaint p =
          new RadialGradientPaint(center, radius, dist, colors);
      g2.setPaint(p);
      g2.setComposite(AlphaComposite.getInstance(
          AlphaComposite.SRC_OVER, 0.4f));
      g2.fillRect(0, 0, c.getWidth(), c.getHeight());
   }
  }

  g2.dispose();
}
 
Example 11
Source File: RasterDirectLayer.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private BufferedImage makeOpaque(BufferedImage image) {
	if (image.getType() == BufferedImage.TYPE_CUSTOM) {
		log.warn("makeOpaque {} Unknown Image Type 0: ", getTitle());
		return image;
	}
	BufferedImage opaqueCopy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
	Graphics2D g1 = opaqueCopy.createGraphics();
	g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getOpacity()));
	g1.drawImage(image, null, 0, 0);
	g1.dispose();
	return opaqueCopy;
}
 
Example 12
Source File: WaitLayerUI.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
	int w = c.getWidth();
	int h = c.getHeight();

	// Paint the view.
	super.paint(g, c);

	if (!mIsRunning) {
		return;
	}

	Graphics2D g2 = (Graphics2D) g.create();

	float fade = (float) mFadeCount / (float) mFadeLimit;
	// Gray it out.
	Composite urComposite = g2.getComposite();
	g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5f * fade));
	g2.fillRect(0, 0, w, h);
	g2.setComposite(urComposite);

	// Paint the wait indicator.
	int s = Math.min(w, h) / 5;
	int cx = w / 2;
	int cy = h / 2;
	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	g2.setStroke(new BasicStroke(s / 4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
	g2.setPaint(Color.white);
	g2.rotate(Math.PI * mAngle / 180, cx, cy);
	for (int i = 0; i < 12; i++) {
		float scale = (11.0f - (float) i) / 11.0f;
		g2.drawLine(cx + s, cy, cx + s * 2, cy);
		g2.rotate(-Math.PI / 6, cx, cy);
		g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, scale * fade));
	}

	g2.dispose();
}
 
Example 13
Source File: SOMModelPlotter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void paintComponent(Graphics graphics) {
	super.paintComponent(graphics);
	// painting only if approved
	if (show) {
		// init graphics
		Graphics2D g = (Graphics2D) graphics;

		int pixWidth = getWidth() - 2 * MARGIN;
		int pixHeight = getHeight() - 2 * MARGIN;

		// painting background
		g.drawImage(this.image, MARGIN, MARGIN, pixWidth, pixHeight, Color.WHITE, null);

		// painting transparent class overlay
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alphaLevel));
		g.drawImage(this.classImage, MARGIN, MARGIN, pixWidth, pixHeight, Color.WHITE, null);
		g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
		// painting points
		drawPoints(g);

		// painting Legend
		drawLegend(graphics, this.dataTable, colorColumn);

		// paint Tooltip
		drawToolTip((Graphics2D) graphics);
	}
}
 
Example 14
Source File: PiePlot.java    From ccu-historian with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Draws the labels for the pie sections.
 *
 * @param g2  the graphics device.
 * @param keys  the keys.
 * @param totalValue  the total value.
 * @param plotArea  the plot area.
 * @param linkArea  the link area.
 * @param state  the state.
 */
protected void drawLabels(Graphics2D g2, List keys, double totalValue,
                          Rectangle2D plotArea, Rectangle2D linkArea,
                          PiePlotState state) {

    Composite originalComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
            1.0f));

    // classify the keys according to which side the label will appear...
    DefaultKeyedValues leftKeys = new DefaultKeyedValues();
    DefaultKeyedValues rightKeys = new DefaultKeyedValues();

    double runningTotal = 0.0;
    Iterator iterator = keys.iterator();
    while (iterator.hasNext()) {
        Comparable key = (Comparable) iterator.next();
        boolean include;
        double v = 0.0;
        Number n = this.dataset.getValue(key);
        if (n == null) {
            include = !this.ignoreNullValues;
        }
        else {
            v = n.doubleValue();
            include = this.ignoreZeroValues ? v > 0.0 : v >= 0.0;
        }

        if (include) {
            runningTotal = runningTotal + v;
            // work out the mid angle (0 - 90 and 270 - 360) = right,
            // otherwise left
            double mid = this.startAngle + (this.direction.getFactor()
                    * ((runningTotal - v / 2.0) * 360) / totalValue);
            if (Math.cos(Math.toRadians(mid)) < 0.0) {
                leftKeys.addValue(key, new Double(mid));
            }
            else {
                rightKeys.addValue(key, new Double(mid));
            }
        }
    }

    g2.setFont(getLabelFont());

    // calculate the max label width from the plot dimensions, because
    // a circular pie can leave a lot more room for labels...
    double marginX = plotArea.getX();
    double gap = plotArea.getWidth() * this.labelGap;
    double ww = linkArea.getX() - gap - marginX;
    float labelWidth = (float) this.labelPadding.trimWidth(ww);

    // draw the labels...
    if (this.labelGenerator != null) {
        drawLeftLabels(leftKeys, g2, plotArea, linkArea, labelWidth,
                state);
        drawRightLabels(rightKeys, g2, plotArea, linkArea, labelWidth,
                state);
    }
    g2.setComposite(originalComposite);

}
 
Example 15
Source File: 1_AbstractCategoryItemRenderer.java    From SimFix with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws a marker for the domain axis.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param plot  the plot (not <code>null</code>).
 * @param axis  the range axis (not <code>null</code>).
 * @param marker  the marker to be drawn (not <code>null</code>).
 * @param dataArea  the area inside the axes (not <code>null</code>).
 *
 * @see #drawRangeMarker(Graphics2D, CategoryPlot, ValueAxis, Marker,
 *     Rectangle2D)
 */
public void drawDomainMarker(Graphics2D g2,
                             CategoryPlot plot,
                             CategoryAxis axis,
                             CategoryMarker marker,
                             Rectangle2D dataArea) {

    Comparable category = marker.getKey();
    CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
    int columnIndex = dataset.getColumnIndex(category);
    if (columnIndex < 0) {
        return;
    }

    final Composite savedComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, marker.getAlpha()));

    PlotOrientation orientation = plot.getOrientation();
    Rectangle2D bounds = null;
    if (marker.getDrawAsLine()) {
        double v = axis.getCategoryMiddle(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Line2D line = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line = new Line2D.Double(dataArea.getMinX(), v,
                    dataArea.getMaxX(), v);
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            line = new Line2D.Double(v, dataArea.getMinY(), v,
                    dataArea.getMaxY());
        }
        g2.setPaint(marker.getPaint());
        g2.setStroke(marker.getStroke());
        g2.draw(line);
        bounds = line.getBounds2D();
    }
    else {
        double v0 = axis.getCategoryStart(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        double v1 = axis.getCategoryEnd(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Rectangle2D area = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            area = new Rectangle2D.Double(dataArea.getMinX(), v0,
                    dataArea.getWidth(), (v1 - v0));
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            area = new Rectangle2D.Double(v0, dataArea.getMinY(),
                    (v1 - v0), dataArea.getHeight());
        }
        g2.setPaint(marker.getPaint());
        g2.fill(area);
        bounds = area;
    }

    String label = marker.getLabel();
    RectangleAnchor anchor = marker.getLabelAnchor();
    if (label != null) {
        Font labelFont = marker.getLabelFont();
        g2.setFont(labelFont);
        g2.setPaint(marker.getLabelPaint());
        Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
                g2, orientation, dataArea, bounds, marker.getLabelOffset(),
                marker.getLabelOffsetType(), anchor);
        TextUtilities.drawAlignedString(label, g2,
                (float) coordinates.getX(), (float) coordinates.getY(),
                marker.getLabelTextAnchor());
    }
    g2.setComposite(savedComposite);
}
 
Example 16
Source File: patch1-Chart-1-jMutRepair_patch1-Chart-1-jMutRepair_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Draws a marker for the domain axis.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param plot  the plot (not <code>null</code>).
 * @param axis  the range axis (not <code>null</code>).
 * @param marker  the marker to be drawn (not <code>null</code>).
 * @param dataArea  the area inside the axes (not <code>null</code>).
 *
 * @see #drawRangeMarker(Graphics2D, CategoryPlot, ValueAxis, Marker,
 *     Rectangle2D)
 */
public void drawDomainMarker(Graphics2D g2,
                             CategoryPlot plot,
                             CategoryAxis axis,
                             CategoryMarker marker,
                             Rectangle2D dataArea) {

    Comparable category = marker.getKey();
    CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
    int columnIndex = dataset.getColumnIndex(category);
    if (columnIndex < 0) {
        return;
    }

    final Composite savedComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, marker.getAlpha()));

    PlotOrientation orientation = plot.getOrientation();
    Rectangle2D bounds = null;
    if (marker.getDrawAsLine()) {
        double v = axis.getCategoryMiddle(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Line2D line = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line = new Line2D.Double(dataArea.getMinX(), v,
                    dataArea.getMaxX(), v);
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            line = new Line2D.Double(v, dataArea.getMinY(), v,
                    dataArea.getMaxY());
        }
        g2.setPaint(marker.getPaint());
        g2.setStroke(marker.getStroke());
        g2.draw(line);
        bounds = line.getBounds2D();
    }
    else {
        double v0 = axis.getCategoryStart(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        double v1 = axis.getCategoryEnd(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Rectangle2D area = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            area = new Rectangle2D.Double(dataArea.getMinX(), v0,
                    dataArea.getWidth(), (v1 - v0));
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            area = new Rectangle2D.Double(v0, dataArea.getMinY(),
                    (v1 - v0), dataArea.getHeight());
        }
        g2.setPaint(marker.getPaint());
        g2.fill(area);
        bounds = area;
    }

    String label = marker.getLabel();
    RectangleAnchor anchor = marker.getLabelAnchor();
    if (label != null) {
        Font labelFont = marker.getLabelFont();
        g2.setFont(labelFont);
        g2.setPaint(marker.getLabelPaint());
        Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
                g2, orientation, dataArea, bounds, marker.getLabelOffset(),
                marker.getLabelOffsetType(), anchor);
        TextUtilities.drawAlignedString(label, g2,
                (float) coordinates.getX(), (float) coordinates.getY(),
                marker.getLabelTextAnchor());
    }
    g2.setComposite(savedComposite);
}
 
Example 17
Source File: FastScatterPlot.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Draws the fast scatter plot on a Java 2D graphics device (such as the 
 * screen or a printer).
 *
 * @param g2  the graphics device.
 * @param area   the area within which the plot (including axis labels)
 *                   should be drawn.
 * @param anchor  the anchor point (<code>null</code> permitted).
 * @param parentState  the state from the parent plot (ignored).
 * @param info  collects chart drawing information (<code>null</code> 
 *              permitted).
 */
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor,
                 PlotState parentState,
                 PlotRenderingInfo info) {

    // set up info collection...
    if (info != null) {
        info.setPlotArea(area);
    }

    // adjust the drawing area for plot insets (if any)...
    RectangleInsets insets = getInsets();
    insets.trim(area);

    AxisSpace space = new AxisSpace();
    space = this.domainAxis.reserveSpace(g2, this, area, 
            RectangleEdge.BOTTOM, space);
    space = this.rangeAxis.reserveSpace(g2, this, area, RectangleEdge.LEFT, 
            space);
    Rectangle2D dataArea = space.shrink(area, null);

    if (info != null) {
        info.setDataArea(dataArea);
    }

    // draw the plot background and axes...
    drawBackground(g2, dataArea);

    AxisState domainAxisState = this.domainAxis.draw(g2, 
            dataArea.getMaxY(), area, dataArea, RectangleEdge.BOTTOM, info);
    AxisState rangeAxisState = this.rangeAxis.draw(g2, dataArea.getMinX(), 
            area, dataArea, RectangleEdge.LEFT, info);
    drawDomainGridlines(g2, dataArea, domainAxisState.getTicks());
    drawRangeGridlines(g2, dataArea, rangeAxisState.getTicks());
    
    Shape originalClip = g2.getClip();
    Composite originalComposite = g2.getComposite();

    g2.clip(dataArea);
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 
            getForegroundAlpha()));

    render(g2, dataArea, info, null);

    g2.setClip(originalClip);
    g2.setComposite(originalComposite);
    drawOutline(g2, dataArea);

}
 
Example 18
Source File: AnnotationDrawer.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates an image of the given annotation and caches it with the specified cache id.
 *
 * @param anno
 *            the annotation to cache
 * @param cacheId
 *            the cache id for the given annotation
 * @return the cached image
 */
private Image cacheAnnotationImage(final WorkflowAnnotation anno, final int cacheId) {
	Rectangle2D loc = anno.getLocation();
	// paint each annotation with the same JEditorPane
	Dimension size = new Dimension((int) Math.round(loc.getWidth() * rendererModel.getZoomFactor()), (int) Math.round(loc.getHeight() * rendererModel.getZoomFactor()));
	pane.setSize(size);
	float originalSize = AnnotationDrawUtils.ANNOTATION_FONT.getSize();
	// without this, scaling is off even more when zooming out..
	if (rendererModel.getZoomFactor() < 1.0d) {
		originalSize -= 1f;
	}
	float fontSize = (float) (originalSize * rendererModel.getZoomFactor());
	Font annotationFont = AnnotationDrawUtils.ANNOTATION_FONT.deriveFont(fontSize);
	pane.setFont(annotationFont);
	pane.setText(AnnotationDrawUtils.createStyledCommentString(anno));
	pane.setCaretPosition(0);

	// while caching, update the hyperlink bounds visible in this annotation
	HTMLDocument htmlDocument = (HTMLDocument) pane.getDocument();
	HTMLDocument.Iterator linkIterator = htmlDocument.getIterator(HTML.Tag.A);
	List<Pair<String, Rectangle>> hyperlinkBounds = new LinkedList<>();
	while (linkIterator.isValid()) {
		AttributeSet attributes = linkIterator.getAttributes();
		String url = (String) attributes.getAttribute(HTML.Attribute.HREF);
		int startOffset = linkIterator.getStartOffset();
		int endOffset = linkIterator.getEndOffset();
		try {
			// rectangle for leftmost character
			Rectangle rectangleLeft = pane.getUI().modelToView(pane, startOffset, Position.Bias.Forward);
			// rectangle for rightmost character
			Rectangle rectangleRight = pane.getUI().modelToView(pane, endOffset, Position.Bias.Backward);
			// merge both rectangles to get full bounds of hyperlink
			// also remove the zoom factor to not get distorted bounds when zoomed in/out
			int x = (int) (rectangleLeft.getX() / rendererModel.getZoomFactor());
			int y = (int) (rectangleLeft.getY() / rendererModel.getZoomFactor());
			int w = (int) ((rectangleRight.getX() - rectangleLeft.getX() + rectangleRight.getWidth()) / rendererModel.getZoomFactor());
			int h = (int) ((rectangleRight.getY() - rectangleLeft.getY() + rectangleRight.getHeight()) / rendererModel.getZoomFactor());
			hyperlinkBounds.add(new Pair<>(url, new Rectangle(x, y, w, h)));
		} catch (BadLocationException e) {
			// silently ignored because at worst you cannot click a hyperlink, nothing to spam the log with
		}
		linkIterator.next();
	}
	model.setHyperlinkBoundsForAnnotation(anno.getId(), hyperlinkBounds);

	// draw annotation area to image and then to graphics
	// otherwise heavyweight JEdiorPane draws over everything and outside of panel
	BufferedImage img = new BufferedImage((int) (loc.getWidth() * rendererModel.getZoomFactor()), (int) (loc.getHeight() * rendererModel.getZoomFactor()), BufferedImage.TYPE_INT_ARGB);
	Graphics2D gImg = img.createGraphics();
	gImg.setRenderingHints(ProcessDrawer.HI_QUALITY_HINTS);
	// without this, the text is pixelated on half opaque backgrounds
	gImg.setComposite(AlphaComposite.SrcOver);
	// paint JEditorPane to image
	pane.paint(gImg);
	displayCache.put(anno.getId(), new WeakReference<>(img));
	cachedID.put(anno.getId(), cacheId);

	return img;
}
 
Example 19
Source File: TICPlotRenderer.java    From mzmine3 with GNU General Public License v2.0 4 votes vote down vote up
protected void drawFirstPassShape(Graphics2D g2, int pass, int series, int item, Shape shape) {
  g2.setComposite(makeComposite(transparency));
  g2.setStroke(getItemStroke(series, item));
  g2.setPaint(getItemPaint(series, item));
  g2.draw(shape);
}
 
Example 20
Source File: jMutRepair_0020_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Draws a marker for the domain axis.
 *
 * @param g2  the graphics device (not <code>null</code>).
 * @param plot  the plot (not <code>null</code>).
 * @param axis  the range axis (not <code>null</code>).
 * @param marker  the marker to be drawn (not <code>null</code>).
 * @param dataArea  the area inside the axes (not <code>null</code>).
 *
 * @see #drawRangeMarker(Graphics2D, CategoryPlot, ValueAxis, Marker,
 *     Rectangle2D)
 */
public void drawDomainMarker(Graphics2D g2,
                             CategoryPlot plot,
                             CategoryAxis axis,
                             CategoryMarker marker,
                             Rectangle2D dataArea) {

    Comparable category = marker.getKey();
    CategoryDataset dataset = plot.getDataset(plot.getIndexOf(this));
    int columnIndex = dataset.getColumnIndex(category);
    if (columnIndex < 0) {
        return;
    }

    final Composite savedComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(
            AlphaComposite.SRC_OVER, marker.getAlpha()));

    PlotOrientation orientation = plot.getOrientation();
    Rectangle2D bounds = null;
    if (marker.getDrawAsLine()) {
        double v = axis.getCategoryMiddle(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Line2D line = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            line = new Line2D.Double(dataArea.getMinX(), v,
                    dataArea.getMaxX(), v);
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            line = new Line2D.Double(v, dataArea.getMinY(), v,
                    dataArea.getMaxY());
        }
        g2.setPaint(marker.getPaint());
        g2.setStroke(marker.getStroke());
        g2.draw(line);
        bounds = line.getBounds2D();
    }
    else {
        double v0 = axis.getCategoryStart(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        double v1 = axis.getCategoryEnd(columnIndex,
                dataset.getColumnCount(), dataArea,
                plot.getDomainAxisEdge());
        Rectangle2D area = null;
        if (orientation == PlotOrientation.HORIZONTAL) {
            area = new Rectangle2D.Double(dataArea.getMinX(), v0,
                    dataArea.getWidth(), (v1 - v0));
        }
        else if (orientation == PlotOrientation.VERTICAL) {
            area = new Rectangle2D.Double(v0, dataArea.getMinY(),
                    (v1 - v0), dataArea.getHeight());
        }
        g2.setPaint(marker.getPaint());
        g2.fill(area);
        bounds = area;
    }

    String label = marker.getLabel();
    RectangleAnchor anchor = marker.getLabelAnchor();
    if (label != null) {
        Font labelFont = marker.getLabelFont();
        g2.setFont(labelFont);
        g2.setPaint(marker.getLabelPaint());
        Point2D coordinates = calculateDomainMarkerTextAnchorPoint(
                g2, orientation, dataArea, bounds, marker.getLabelOffset(),
                marker.getLabelOffsetType(), anchor);
        TextUtilities.drawAlignedString(label, g2,
                (float) coordinates.getX(), (float) coordinates.getY(),
                marker.getLabelTextAnchor());
    }
    g2.setComposite(savedComposite);
}