Example usage for java.awt Graphics2D setPaint

List of usage examples for java.awt Graphics2D setPaint

Introduction

In this page you can find the example usage for java.awt Graphics2D setPaint.

Prototype

public abstract void setPaint(Paint paint);

Source Link

Document

Sets the Paint attribute for the Graphics2D context.

Usage

From source file:au.org.ala.biocache.web.MapController.java

@RequestMapping(value = "/occurrences/legend", method = RequestMethod.GET)
public void pointLegendImage(
        @RequestParam(value = "colourby", required = false, defaultValue = "0") Integer colourby,
        @RequestParam(value = "width", required = false, defaultValue = "50") Integer widthObj,
        @RequestParam(value = "height", required = false, defaultValue = "50") Integer heightObj,
        HttpServletResponse response) {//  w  w w  .  j a  va  2s  . c  o  m
    try {
        int width = widthObj.intValue();
        int height = heightObj.intValue();

        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = (Graphics2D) img.getGraphics();

        if (colourby != null) {
            int colour = 0xFF000000 | colourby.intValue();
            Color c = new Color(colour);
            g.setPaint(c);

        } else {
            g.setPaint(Color.blue);
        }

        g.fillOval(0, 0, width, width);

        g.dispose();

        response.setContentType("image/png");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(img, "png", outputStream);
        ServletOutputStream outStream = response.getOutputStream();
        outStream.write(outputStream.toByteArray());
        outStream.flush();
        outStream.close();

    } catch (Exception e) {
        logger.error("Unable to write image", e);
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Draws an AWT image into a BufferedImage using an AWT Graphics2D implementation
 *
 * @param img the AWT image//  www . j a  v a  2  s .com
 * @param bufferedImage the AWT buffered image
 * @param width the image width
 * @param height the image height
 * @param observer the image observer
 * @return true if the image was drawn
 */
private boolean drawBufferedImage(Image img, BufferedImage bufferedImage, int width, int height,
        ImageObserver observer) {

    java.awt.Graphics2D g2d = bufferedImage.createGraphics();
    try {
        g2d.setComposite(AlphaComposite.SrcOver);

        Color color = new Color(1, 1, 1, 0);
        g2d.setBackground(color);
        g2d.setPaint(color);

        g2d.fillRect(0, 0, width, height);

        int imageWidth = bufferedImage.getWidth();
        int imageHeight = bufferedImage.getHeight();
        Rectangle clipRect = new Rectangle(0, 0, imageWidth, imageHeight);
        g2d.clip(clipRect);

        g2d.setComposite(gc.getComposite());

        return g2d.drawImage(img, 0, 0, imageWidth, imageHeight, observer);
    } finally {
        g2d.dispose(); //drawn so dispose immediately to free system resource
    }
}

From source file:org.nuclos.client.ui.resplan.header.JHeaderGrid.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;

    super.paintComponent(g2d);

    g2d.setPaint(gridColor);

    Rectangle clipBounds = g.getClipBounds();

    int[] indices = stripRangeForRect(clipBounds);
    int startIndex = indices[0];
    int endIndex = indices[1];

    Point p1, p2;//from   w ww.j ava2s. co  m
    p1 = new Point();
    p2 = new Point(getWidth(), getHeight());
    for (int i = startIndex; i <= endIndex; i++) {
        int gridLine = getGridLine(i);
        orientation.updateCoord(p1, gridLine);
        orientation.updateCoord(p2, gridLine);
        g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
    }

    p1.setLocation(0, 0);
    p2.setLocation(getWidth(), getHeight());
    int groupGridLine = 0;
    for (CategoryView v : groupings) {
        groupGridLine += v.size;
        orientation.opposite().updateCoord(p1, groupGridLine);
        orientation.opposite().updateCoord(p2, groupGridLine);
        g2d.drawLine(p1.x, p1.y, p2.x, p2.y);
        groupGridLine += GRID_SIZE;
    }

    for (int level = 0, levelCount = groupings.length; level < levelCount; level++) {
        for (HeaderCell cell : groupings[level].cells) {
            if (cell.endIndex < startIndex || cell.startIndex >= endIndex)
                continue;
            Rectangle rect = getRect(level, cell.startIndex, cell.endIndex);
            if (clipBounds != null && !clipBounds.intersects(rect))
                continue;
            paintHeaderCell(g2d, rect, cell.levelValue);
        }
    }

    if (cellResizingRange != null) {
        Rectangle rect = new Rectangle(getWidth(), getHeight());
        orientation.updateRange(rect, cellResizingRange);
        rect.grow(orientation.select(GRID_SIZE, 0), orientation.select(0, GRID_SIZE));
        g2d.setColor(new Color(0x9999ff, false));
        g2d.draw(rect);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
        g2d.fill(rect);
    }
}

From source file:org.tsho.dmc2.core.chart.AbstractDmcPlot.java

private void drawRangeGridLine(Graphics2D g2, Rectangle2D dataArea, double value) {

    Range range = rangeAxis.getRange();//  w w w .j  av a  2  s  .  co  m
    if (!range.contains(value)) {
        return;
    }

    Line2D line = null;
    double v = rangeAxis.valueToJava2D(value, dataArea, RectangleEdge.LEFT);
    line = new Line2D.Double(dataArea.getMinX(), v, dataArea.getMaxX(), v);

    g2.setPaint(gridPaint);
    g2.setStroke(gridStroke);
    g2.draw(line);

}

From source file:org.openfaces.component.chart.impl.renderers.LineFillRenderer.java

private void configureSolidAreaFill(Graphics2D g2, Paint itemPaint, SolidLineAreaFill solidLineAreaFill) {
    final Double transparency = solidLineAreaFill.getTransparency();

    if (itemPaint instanceof Color && transparency >= 0.0 && transparency <= 1.0) {
        Color itemColor = (Color) itemPaint;
        int alpha = transparency >= 0.0 && transparency <= 1.0 ? Math.round(255 * transparency.floatValue())
                : 255;/*from w ww .  j  a  v  a2s  . co  m*/
        g2.setPaint(new Color(itemColor.getRed(), itemColor.getGreen(), itemColor.getBlue(), alpha));
    } else {
        g2.setPaint(itemPaint);
    }
}

From source file:org.tsho.dmc2.core.chart.AbstractDmcPlot.java

private void drawDomainGridLine(Graphics2D g2, Rectangle2D dataArea, double value) {

    Range range = domainAxis.getRange();
    if (!range.contains(value)) {
        return;//from  ww w.j a v a  2s  . co m
    }

    Line2D line = null;
    double v = domainAxis.valueToJava2D(value, dataArea, RectangleEdge.BOTTOM);
    line = new Line2D.Double(v, dataArea.getMinY(), v, dataArea.getMaxY());

    g2.setPaint(gridPaint);
    g2.setStroke(gridStroke);
    g2.draw(line);

}

From source file:it.cnr.istc.utils.gui.ReverseGradientXYBarPainter.java

/**
 * Paints a single bar instance./*from  w w  w . j  a  va 2 s . c  o m*/
 *
 * @param g2 the graphics target.
 * @param renderer the renderer.
 * @param row the row index.
 * @param column the column index.
 * @param bar the bar
 * @param base indicates which side of the rectangle is the base of the bar.
 * @param pegShadow peg the shadow to the base of the bar?
 */
@Override
public void paintBarShadow(Graphics2D g2, XYBarRenderer renderer, int row, int column, RectangularShape bar,
        RectangleEdge base, boolean pegShadow) {

    // handle a special case - if the bar colour has alpha == 0, it is
    // invisible so we shouldn't draw any shadow
    Paint itemPaint = renderer.getItemPaint(row, column);
    if (itemPaint instanceof Color) {
        Color c = (Color) itemPaint;
        if (c.getAlpha() == 0) {
            return;
        }
    }

    RectangularShape shadow = createShadow(bar, renderer.getShadowXOffset(), renderer.getShadowYOffset(), base,
            pegShadow);
    g2.setPaint(Color.gray);
    g2.fill(shadow);

}

From source file:ucar.unidata.idv.control.chart.MyScatterPlot.java

/**
 * Draws the fast scatter plot on a Java 2D graphics device (such as the
 * screen or a printer).//  www . java 2s.  c  om
 * a
 * @param g2  the graphics device.
 * @param dataArea the data area
 * @param index which data set
 * @param info  collects chart drawing information (<code>null</code>
 *              permitted).
 * @param crosshairState crosshairState
 *
 * @return did something
 */
public boolean render(Graphics2D g2, Rectangle2D dataArea, int index, PlotRenderingInfo info,
        CrosshairState crosshairState) {

    if (index >= series.size()) {
        return false;
    }
    XYDataset dataset = getDataset(index);
    g2.setStroke(new BasicStroke());
    //                   getRendererForDataset(dataset).getSeriesStroke(0));
    ScatterPlotChartWrapper.MyRenderer renderer = (ScatterPlotChartWrapper.MyRenderer) getRendererForDataset(
            dataset);
    g2.setPaint(renderer.getSeriesPaint(0));
    int shape = renderer.shape;

    PlotOrientation orientation = getOrientation();
    int seenCnt = 0;

    int xx = (int) dataArea.getMinX();
    int ww = (int) dataArea.getWidth();
    int yy = (int) dataArea.getMaxY();
    int hh = (int) dataArea.getHeight();
    ValueAxis rangeAxis = getRangeAxisForDataset(index);
    ValueAxis domainAxis = getDomainAxisForDataset(index);
    double domainMin = domainAxis.getLowerBound();
    double domainLength = domainAxis.getUpperBound() - domainMin;
    double rangeMin = rangeAxis.getLowerBound();
    double rangeLength = rangeAxis.getUpperBound() - rangeMin;
    int boxWidth = 6;

    double[][] data = (double[][]) series.get(index);

    double[] d1 = data[0];
    double[] d2 = data[1];
    int size = d1.length;

    Hashtable seen = new Hashtable();
    int lastX = 0;
    int lastY = 0;
    //TODO: Check for clipping
    //TODO: Try to create a GeneralPath with the points
    //and cal g2.draw just once
    GeneralPath path = new GeneralPath();
    long t1 = System.currentTimeMillis();

    for (int i = 0; i < size; i++) {
        int transX = (int) (xx + ww * (d1[i] - domainMin) / domainLength);
        int transY = (int) (yy - hh * (d2[i] - rangeMin) / rangeLength);
        Object key = transX + "_" + transY;
        if (seen.get(key) != null) {
            seenCnt++;
            continue;
        }
        seen.put(key, key);
        if (crosshairState != null) {
            crosshairState.updateCrosshairPoint(d1[i], d2[i], transX, transY, orientation);
        }

        switch (shape) {

        case LineState.SHAPE_VLINE:
            if (i > 1) {
                g2.drawLine(lastX, lastY, transX, transY);
            }
            lastX = transX;
            lastY = transY;

        case LineState.SHAPE_POINT:
            path.append(new Rectangle((int) transX, (int) transY, 1, 1), false);
            break;

        case LineState.SHAPE_LARGEPOINT:
            path.append(new Rectangle((int) transX, (int) transY, 2, 2), false);
            break;

        case LineState.SHAPE_RECTANGLE:
            path.append(
                    new Rectangle((int) transX - boxWidth / 2, (int) transY - boxWidth / 2, boxWidth, boxWidth),
                    false);
            break;

        case LineState.SHAPE_X:
            g2.drawLine(transX - boxWidth / 2, transY - boxWidth / 2, transX + boxWidth - boxWidth / 2,
                    transY + boxWidth - boxWidth / 2);
            g2.drawLine(transX + boxWidth - boxWidth / 2, transY - boxWidth / 2, transX - boxWidth / 2,
                    transY + boxWidth - boxWidth / 2);
            break;

        case LineState.SHAPE_PLUS:
            g2.drawLine(transX + boxWidth / 2, transY, transX + boxWidth / 2, transY + boxWidth);
            g2.drawLine(transX, transY + boxWidth / 2, transX + boxWidth, transY + boxWidth / 2);
            break;

        }
    }
    g2.fill(path);
    long t2 = System.currentTimeMillis();
    //        System.out.println ("time:" + (t2-t1));
    return true;
}

From source file:Highlights.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

    String s = "Drag the text to highlight Java Source and Support.";
    Font font = new Font("Serif", Font.PLAIN, 32);

    if (textLayout == null) {
        FontRenderContext frc = g2.getFontRenderContext();
        textLayout = new TextLayout(s, font, frc);
    }/* w  ww. j a  v a 2  s .  c  o m*/

    // Draw the highlight.
    if (firstHit != null && secondHit != null) {
        Shape base = textLayout.getLogicalHighlightShape(firstHit.getInsertionIndex(),
                secondHit.getInsertionIndex());
        AffineTransform at = AffineTransform.getTranslateInstance(x, y);
        Shape highlight = at.createTransformedShape(base);
        g2.setPaint(Color.white);
        g2.fill(highlight);
    }

    g2.setPaint(Color.black);
    textLayout.draw(g2, x, y);
}

From source file:coolmap.canvas.datarenderer.renderer.impl.NumberToBoxPlot.java

private void updateLegend() {

    int width = DEFAULT_LEGEND_WIDTH;
    int height = DEFAULT_LEGENT_HEIGHT;
    legend = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice()
            .getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    Graphics2D g = (Graphics2D) legend.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.setPaint(UI.colorBlack2);
    g.fillRoundRect(0, 0, width, height - 12, 5, 5);

    g.setColor(barColorBelow);/*from  w ww  .  j av a 2  s. co m*/
    int boxNum = 10;
    g.setStroke(UI.stroke1_5);

    double value;
    for (int i = 0; i < boxNum; i++) {
        value = _minValue + (_maxValue - _minValue) / boxNum * i;

        if (value > disectBound) {
            g.setColor(barColorNormal);
        }

        int h = (height - 12) / boxNum * i;
        g.drawLine(i * width / boxNum, height - 12 - h, (i + 1) * width / boxNum, height - 12 - h);

    }

    g.setColor(Color.BLACK);
    g.setFont(UI.fontMono.deriveFont(10f));
    DecimalFormat format = new DecimalFormat("#.##");
    g.drawString(format.format(_minValue), 2, 23);

    g.setColor(Color.BLACK);
    String maxString = format.format(_maxValue);
    int swidth = g.getFontMetrics().stringWidth(maxString);
    g.drawString(maxString, width - 2 - swidth, 23);
    g.dispose();
}