Example usage for java.awt RenderingHints VALUE_ALPHA_INTERPOLATION_QUALITY

List of usage examples for java.awt RenderingHints VALUE_ALPHA_INTERPOLATION_QUALITY

Introduction

In this page you can find the example usage for java.awt RenderingHints VALUE_ALPHA_INTERPOLATION_QUALITY.

Prototype

Object VALUE_ALPHA_INTERPOLATION_QUALITY

To view the source code for java.awt RenderingHints VALUE_ALPHA_INTERPOLATION_QUALITY.

Click Source Link

Document

Alpha interpolation hint value -- alpha blending algorithms are chosen with a preference for precision and visual quality.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String text = "java2s.com";

    BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = img.createGraphics();
    Font font = new Font("Arial", Font.PLAIN, 48);
    g2d.setFont(font);/*from   w w w .  j  a  v  a2  s  .  co m*/
    FontMetrics fm = g2d.getFontMetrics();
    int width = fm.stringWidth(text);
    int height = fm.getHeight();
    g2d.dispose();

    img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    g2d = img.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    g2d.setFont(font);
    fm = g2d.getFontMetrics();
    g2d.setColor(Color.BLACK);
    g2d.drawString(text, 0, fm.getAscent());
    g2d.dispose();

    ImageIO.write(img, "png", new File("Text.png"));

}

From source file:com.igormaznitsa.jhexed.swing.editor.ui.exporters.PNGImageExporter.java

public BufferedImage generateImage() throws IOException {
    final int DEFAULT_CELL_WIDTH = 48;
    final int DEFAULT_CELL_HEIGHT = 48;

    final int imgWidth = this.docOptions.getImage() == null ? DEFAULT_CELL_WIDTH * this.docOptions.getColumns()
            : Math.round(this.docOptions.getImage().getSVGWidth());
    final int imgHeight = this.docOptions.getImage() == null ? DEFAULT_CELL_HEIGHT * this.docOptions.getRows()
            : Math.round(this.docOptions.getImage().getSVGHeight());

    final BufferedImage result;
    if (exportData.isBackgroundImageExport() && this.docOptions.getImage() != null) {
        result = this.docOptions.getImage().rasterize(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    } else {/*  w w w. jav  a2s  .c om*/
        result = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    }

    final Graphics2D gfx = result.createGraphics();
    gfx.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    gfx.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);

    final HexEngine<Graphics2D> engine = new HexEngine<Graphics2D>(DEFAULT_CELL_WIDTH, DEFAULT_CELL_HEIGHT,
            this.docOptions.getHexOrientation());

    final List<HexFieldLayer> reversedNormalizedStack = new ArrayList<HexFieldLayer>();
    for (int i = this.exportData.getLayers().size() - 1; i >= 0; i--) {
        final LayerExportRecord rec = this.exportData.getLayers().get(i);
        if (rec.isAllowed()) {
            reversedNormalizedStack.add(rec.getLayer());
        }
    }

    if (Thread.currentThread().isInterrupted())
        return null;

    final HexFieldValue[] stackOfValues = new HexFieldValue[reversedNormalizedStack.size()];

    engine.setModel(new HexEngineModel<HexFieldValue[]>() {

        @Override
        public int getColumnNumber() {
            return docOptions.getColumns();
        }

        @Override
        public int getRowNumber() {
            return docOptions.getRows();
        }

        @Override
        public HexFieldValue[] getValueAt(final int col, final int row) {
            Arrays.fill(stackOfValues, null);

            for (int index = 0; index < reversedNormalizedStack.size(); index++) {
                stackOfValues[index] = reversedNormalizedStack.get(index).getHexValueAtPos(col, row);
            }
            return stackOfValues;
        }

        @Override
        public HexFieldValue[] getValueAt(final HexPosition pos) {
            return this.getValueAt(pos.getColumn(), pos.getRow());
        }

        @Override
        public void setValueAt(int col, int row, HexFieldValue[] value) {
        }

        @Override
        public void setValueAt(HexPosition pos, HexFieldValue[] value) {
        }

        @Override
        public boolean isPositionValid(final int col, final int row) {
            return col >= 0 && col < docOptions.getColumns() && row >= 0 && row < docOptions.getRows();
        }

        @Override
        public boolean isPositionValid(final HexPosition pos) {
            return this.isPositionValid(pos.getColumn(), pos.getRow());
        }

        @Override
        public void attachedToEngine(final HexEngine<?> engine) {
        }

        @Override
        public void detachedFromEngine(final HexEngine<?> engine) {
        }
    });

    final HexRect2D visibleSize = engine.getVisibleSize();
    final float xcoeff = (float) result.getWidth() / visibleSize.getWidth();
    final float ycoeff = (float) result.getHeight() / visibleSize.getHeight();
    engine.setScale(xcoeff, ycoeff);

    final Image[][] cachedIcons = new Image[this.exportData.getLayers().size()][];
    engine.setRenderer(new ColorHexRender() {

        private final Stroke stroke = new BasicStroke(docOptions.getLineWidth());

        @Override
        public Stroke getStroke() {
            return this.stroke;
        }

        @Override
        public Color getFillColor(HexEngineModel<?> model, int col, int row) {
            return null;
        }

        @Override
        public Color getBorderColor(HexEngineModel<?> model, int col, int row) {
            return exportData.isExportHexBorders() ? docOptions.getColor() : null;
        }

        @Override
        public void drawExtra(HexEngine<Graphics2D> engine, Graphics2D g, int col, int row, Color borderColor,
                Color fillColor) {
        }

        @Override
        public void drawUnderBorder(final HexEngine<Graphics2D> engine, final Graphics2D g, final int col,
                final int row, final Color borderColor, final Color fillColor) {
            final HexFieldValue[] stackValues = (HexFieldValue[]) engine.getModel().getValueAt(col, row);
            for (int i = 0; i < stackValues.length; i++) {
                final HexFieldValue valueToDraw = stackValues[i];
                if (valueToDraw == null) {
                    continue;
                }
                g.drawImage(cachedIcons[i][valueToDraw.getIndex()], 0, 0, null);
            }
        }

    });

    final Path2D hexShape = ((ColorHexRender) engine.getRenderer()).getHexPath();
    final int cellWidth = hexShape.getBounds().width;
    final int cellHeight = hexShape.getBounds().height;

    for (int layerIndex = 0; layerIndex < reversedNormalizedStack.size()
            && !Thread.currentThread().isInterrupted(); layerIndex++) {
        final HexFieldLayer theLayer = reversedNormalizedStack.get(layerIndex);
        final Image[] cacheLineForLayer = new Image[theLayer.getHexValuesNumber()];
        for (int valueIndex = 1; valueIndex < theLayer.getHexValuesNumber(); valueIndex++) {
            cacheLineForLayer[valueIndex] = theLayer.getHexValueForIndex(valueIndex).makeIcon(cellWidth,
                    cellHeight, hexShape, true);
        }
        cachedIcons[layerIndex] = cacheLineForLayer;
    }

    engine.drawWithThreadInterruptionCheck(gfx);
    if (Thread.currentThread().isInterrupted())
        return null;

    if (this.exportData.isCellCommentariesExport()) {
        final Iterator<Entry<HexPosition, String>> iterator = this.cellComments.iterator();
        gfx.setFont(new Font("Arial", Font.BOLD, 12));
        while (iterator.hasNext() && !Thread.currentThread().isInterrupted()) {
            final Entry<HexPosition, String> item = iterator.next();
            final HexPosition pos = item.getKey();
            final String text = item.getValue();
            final float x = engine.calculateX(pos.getColumn(), pos.getRow());
            final float y = engine.calculateY(pos.getColumn(), pos.getRow());

            final Rectangle2D textBounds = gfx.getFontMetrics().getStringBounds(text, gfx);

            final float dx = x - ((float) textBounds.getWidth() - engine.getCellWidth()) / 2;

            gfx.setColor(Color.BLACK);
            gfx.drawString(text, dx, y);
            gfx.setColor(Color.WHITE);
            gfx.drawString(text, dx - 2, y - 2);
        }
    }

    gfx.dispose();

    return result;
}

From source file:com.igormaznitsa.mindmap.swing.panel.utils.ScalableIcon.java

public synchronized Image getImage(final double scale) {
    if (Double.compare(this.currentScaleFactor, scale) != 0) {
        this.scaledCachedImage = null;
    }//w  ww . ja v  a  2 s.c  o  m

    if (this.scaledCachedImage == null) {
        this.currentScaleFactor = scale;

        final int imgw = this.baseImage.getWidth(null);
        final int imgh = this.baseImage.getHeight(null);
        final int scaledW = (int) Math.round(imgw * this.baseScaleX * scale);
        final int scaledH = (int) Math.round(imgh * this.baseScaleY * scale);

        final BufferedImage img = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_ARGB);
        final Graphics2D g = (Graphics2D) img.getGraphics();

        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

        g.drawImage(this.baseImage, 0, 0, scaledW, scaledH, null);
        g.dispose();

        this.scaledCachedImage = img;
    }
    return this.scaledCachedImage;
}

From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java

@Override
public BufferedImage makeIcon(final int width, final int height, final Path2D shape, final boolean allowAlpha) {
    try {/* www .  j  a v  a2 s .c  o m*/
        final BufferedImage img = this.image.rasterize(width, height, BufferedImage.TYPE_INT_ARGB);
        if (shape == null) {
            return img;
        } else {
            final BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            final Graphics2D g = result.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
                    RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
            g.setClip(makeTransformedPathForSize(width, height, shape));
            g.drawImage(img, 0, 0, null);
            g.dispose();
            return result;
        }

    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:eu.planets_project.tb.impl.data.util.ImageThumbnail.java

/**
 * Create a scaled jpeg of an image. The width/height ratio is preserved.
 * /* w w w .  j av  a  2  s . c om*/
 * <p>
 * If image is smaller than thumbWidth x thumbHeight, it will be magnified,
 * otherwise it will be scaled down.
 * </p>
 * 
 * @param image
 *            the image to reduce
 * @param thumbWidth
 *            the maximum width of the thumbnail
 * @param thumbHeight
 *            the maximum heigth of the thumbnail
 * @param quality
 *            the jpeg quality ot the thumbnail
 * @param out
 *            a stream where the thumbnail data is written to
 */
public static void createThumb(Image image, int thumbWidth, int thumbHeight, OutputStream out)
        throws Exception {
    int imageWidth = image.getWidth(null);
    int imageHeight = image.getHeight(null);
    double thumbRatio = (double) thumbWidth / (double) thumbHeight;
    double imageRatio = (double) imageWidth / (double) imageHeight;
    if (thumbRatio < imageRatio) {
        thumbHeight = (int) (thumbWidth / imageRatio);
    } else {
        thumbWidth = (int) (thumbHeight * imageRatio);
    }
    // draw original image to thumbnail image object and
    // scale it to the new size on-the-fly
    BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = thumbImage.createGraphics();
    graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    graphics2D.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);
    // save thumbnail image to out stream
    log.debug("Start writing rescaled image...");
    ImageIO.write(thumbImage, "png", out);
    log.debug("DONE writing rescaled image...");
}

From source file:net.sf.maltcms.common.charts.api.XYChartBuilder.java

/**
 *
 *///from   w ww  .  ja  v  a 2s  .  com
public XYChartBuilder() {
    plot = new XYPlot(dataset, domainAxis, rangeAxis, renderer);
    chart = new JFreeChart(plot);
    renderingHints = new RenderingHints(null);
    renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
    renderingHints.put(RenderingHints.KEY_TEXT_LCD_CONTRAST, 100);
    renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
    renderingHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
    renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    renderingHints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
    renderingHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
}

From source file:com.sdk.connector.chart.CoherenceDomainRenderer.java

public CoherenceDomainRenderer(final String title, JPanel panel, String side) {
    sdf = Protocol.getInstance().getTimestampFormat();

    serieFFT.setKey(side + " FFT Based");
    serieLomb.setKey(side + " Lomb Based");
    serieMemse.setKey(side + " AR Based");

    dataset.addSeries(serieFFT);/* w ww. j a va2 s . c  om*/
    dataset.addSeries(serieLomb);
    dataset.addSeries(serieMemse);

    chart = ChartFactory.createTimeSeriesChart(title,
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("coherence.xlabel"),
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("coherence.ylabel"),
            dataset, true, true, false);

    chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    chart.getRenderingHints().put(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    chart.getRenderingHints().put(RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    chart.getRenderingHints().put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_LCD_CONTRAST, 100);
    //chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.GREEN));
    // chart.setBackgroundPaint(new Color(220,255,220,0));
    //chart.setBackgroundImage(new javax.swing.ImageIcon(getClass().getResource("/com/sdk/connector/resources/background.png")).getImage());
    chart.addSubtitle(rangeAnnotation);
    plot = (XYPlot) chart.getPlot();

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, sdf, new DecimalFormat("0.00")));
    renderer.setSeriesOutlinePaint(0, Color.BLACK);

    if (side.startsWith(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("side.left"))) {
        renderer.setSeriesPaint(0, Color.BLUE);
        leftSide = true;
    } else {
        renderer.setSeriesPaint(0, Color.RED);
        leftSide = false;
    }
    renderer.setSeriesShape(0, new Ellipse2D.Double(-1.0, -1.0, 3.0, 3.0));

    DateTickUnit dtUnit = new DateTickUnit(DateTickUnitType.MINUTE, 1, tickSDF);
    final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
    domainAxis.setTickUnit(dtUnit);

    //        ValueAxis axis = plot.getDomainAxis();
    //        axis = plot.getRangeAxis();
    //        ((NumberAxis) axis).setTickUnit(new NumberTickUnit(100));

    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.BLACK);
    plot.setRangeGridlinePaint(Color.BLACK);
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(1.0f));
    // plot.getRenderer().setSeriesStroke(1, new BasicStroke(1.0f));
    plot.setForegroundAlpha(0.5f);
    plot.setNoDataMessage(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("message.wait"));
    plot.setRangePannable(true);
    plot.setDomainPannable(true);
    Color color1 = new Color(0, 0, 0, 24);
    Color color2 = new Color(255, 255, 255, 24);

    GradientPaint gp = new GradientPaint(0, 0, color1, 0, 0, color2);
    plot.setBackgroundPaint(gp);

    chartPanel = new ChartPanel(chart);

    panel.setLayout(new GridLayout(0, 1));

    panel.add(chartPanel);
    panel.repaint();
    panel.revalidate();

}

From source file:com.sdk.connector.chart.TimeDomainRenderer.java

public TimeDomainRenderer(final String title, JPanel panel, String side, String type) {
    sdf = protocol.getTimestampFormat();
    this.side = side + type;
    this.type = type;
    serie.setKey(side);//w w  w .j  a  va2 s .c om
    dataset.addSeries(serie);
    String legendX = "";
    if (type.startsWith(java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("RR"))) {
        legendX = java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle")
                .getString("timeRR.ylabel");
    } else {
        legendX = java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle")
                .getString("timeBC.ylabel");
    }
    chart = ChartFactory.createTimeSeriesChart(title,
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("time.xlabel"),
            legendX, dataset, true, true, false);

    chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    chart.getRenderingHints().put(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    chart.getRenderingHints().put(RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    chart.getRenderingHints().put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_LCD_CONTRAST, 100);
    //chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.GREEN));
    // chart.setBackgroundPaint(new Color(220,255,220,0));
    //chart.setBackgroundImage(new javax.swing.ImageIcon(getClass().getResource("/com/sdk/connector/resources/background.png")).getImage());
    chart.addSubtitle(rangeAnnotation);

    plot = (XYPlot) chart.getPlot();

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesLinesVisible(0, true);
    renderer.setSeriesShapesVisible(0, true);
    renderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator(
            StandardXYToolTipGenerator.DEFAULT_TOOL_TIP_FORMAT, sdf, new DecimalFormat("0.00")));
    renderer.setSeriesOutlinePaint(0, Color.BLACK);
    if (side.startsWith(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("side.left"))) {
        renderer.setSeriesPaint(0, Color.BLUE);
    } else {
        renderer.setSeriesPaint(0, Color.RED);
    }

    renderer.setSeriesShape(0, new Ellipse2D.Double(-1.0, -1.0, 3.0, 3.0));
    if (type.startsWith(java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("BC"))) {
        renderer.setSeriesShape(0, new Rectangle2D.Double(-1.0, -1.0, 3.0, 3.0));
    }

    DateTickUnit dtUnit = new DateTickUnit(DateTickUnitType.MINUTE, 1, tickSDF);
    final DateAxis domainAxis = (DateAxis) plot.getDomainAxis();
    domainAxis.setTickUnit(dtUnit);

    //        ValueAxis axis = plot.getDomainAxis();
    //        axis = plot.getRangeAxis();
    //        ((NumberAxis) axis).setTickUnit(new NumberTickUnit(100));

    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.BLACK);
    plot.setRangeGridlinePaint(Color.BLACK);
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(1.0f));
    // plot.getRenderer().setSeriesStroke(1, new BasicStroke(1.0f));
    plot.setForegroundAlpha(0.5f);
    plot.setNoDataMessage(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("CALIBRATING..."));
    plot.setRangePannable(true);
    plot.setDomainPannable(true);
    Color color1 = new Color(0, 0, 0, 24);
    Color color2 = new Color(255, 255, 255, 24);

    GradientPaint gp = new GradientPaint(0, 0, color1, 0, 0, color2);
    plot.setBackgroundPaint(gp);

    chartPanel = new ChartPanel(chart);
    chartPanel.addChartMouseListener(this);
    panel.setLayout(new GridLayout(0, 1));

    panel.add(chartPanel);
    panel.repaint();
    panel.revalidate();

}

From source file:com.sdk.connector.chart.FrequencyDomainRenderer.java

/**
 * Creates a new demo application./*from w w  w  .j  ava 2  s . c o m*/
 *
 * @param title  the frame title.
 */
public FrequencyDomainRenderer(String title, JPanel panel, String side, String type) {

    this.side = side;
    this.type = type;
    serie.setKey(side);

    dataset.addSeries(serie);
    JFreeChart chart = ChartFactory.createXYAreaChart(title,
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("frequency.xlabel"),
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("frequency.ylabel2"),
            dataset, PlotOrientation.VERTICAL, true, true, false);

    //        TextTitle t1 = new TextTitle( "Espectro de Frequncia Estimado (PSD)", new Font("SansSerif", Font.BOLD, 14)  );
    //        //TextTitle t2 = new TextTitle( "valores atualizados a cada potncia de 2", new Font("SansSerif", Font.PLAIN, 11)      );
    //        chart.addSubtitle(t1);
    chart.getRenderingHints().put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    chart.getRenderingHints().put(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    chart.getRenderingHints().put(RenderingHints.KEY_FRACTIONALMETRICS,
            RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    chart.getRenderingHints().put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_LCD_CONTRAST, 100);
    //chart.addSubtitle(t2);
    plot = (XYPlot) chart.getPlot();
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.BLUE);
    plot.setRangeGridlinePaint(Color.CYAN);
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f));
    plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f));

    //plot.setRangePannable(true);
    plot.setForegroundAlpha(0.65f);

    //XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer = plot.getRenderer();
    renderer.setSeriesOutlinePaint(0, Color.BLACK);
    if (side.startsWith(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("side.left"))) {
        renderer.setSeriesPaint(0, Color.BLUE);
    } else {
        renderer.setSeriesPaint(0, Color.RED);
    }
    //        renderer.setSeriesLinesVisible(0, true);
    //        renderer.setSeriesShapesVisible(0, false);
    //        renderer.setSeriesLinesVisible(1, true);
    //        renderer.setSeriesShapesVisible(1, false);
    plot.setRenderer(renderer);
    plot.setBackgroundPaint(Color.WHITE);
    plot.setDomainGridlinePaint(Color.BLACK);
    plot.setRangeGridlinePaint(Color.BLACK);
    plot.setForegroundAlpha(0.5f);

    Color color1 = new Color(0, 0, 0, 24);
    Color color2 = new Color(255, 255, 255, 24);

    GradientPaint gp = new GradientPaint(0, 0, color1, 0, 0, color2);
    plot.setBackgroundPaint(gp);

    final ValueAxis domainAxis = plot.getDomainAxis();
    domainAxis.setTickMarkPaint(Color.black);
    domainAxis.setLowerMargin(0.0);
    domainAxis.setUpperMargin(0.0);
    NumberAxis numDomainAxis = (NumberAxis) plot.getDomainAxis();
    numDomainAxis.setAutoRangeIncludesZero(true);
    //
    //        final NumberAxis rangeAxis = new LogarithmicAxis("Log(y)");
    //
    //        plot.setRangeAxis(rangeAxis);
    final ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setTickMarkPaint(Color.black);

    numRangeAxis = (NumberAxis) plot.getRangeAxis();
    numRangeAxis.setAutoRangeIncludesZero(true);

    vlfTarget.setLabel("VLF");
    vlfTarget.setLabelFont(new Font("SansSerif", Font.ITALIC, 9));
    vlfTarget.setLabelAnchor(RectangleAnchor.CENTER);
    vlfTarget.setLabelTextAnchor(TextAnchor.CENTER);
    vlfTarget.setPaint(new Color(0, 100, 255, 128));
    // plot.addRangeMarker(target, Layer.BACKGROUND);
    plot.addDomainMarker(vlfTarget, Layer.BACKGROUND);

    lfTarget.setLabel("LF");
    lfTarget.setLabelFont(new Font("SansSerif", Font.ITALIC, 9));
    lfTarget.setLabelAnchor(RectangleAnchor.CENTER);
    lfTarget.setLabelTextAnchor(TextAnchor.CENTER);
    lfTarget.setPaint(new Color(255, 255, 0, 128));
    // plot.addRangeMarker(target, Layer.BACKGROUND);
    plot.addDomainMarker(lfTarget, Layer.BACKGROUND);

    hfTarget.setLabel("HF");
    hfTarget.setLabelFont(new Font("SansSerif", Font.ITALIC, 9));
    hfTarget.setLabelAnchor(RectangleAnchor.CENTER);
    hfTarget.setLabelTextAnchor(TextAnchor.CENTER);
    hfTarget.setPaint(new Color(255, 0, 255, 128));
    // plot.addRangeMarker(target, Layer.BACKGROUND);
    plot.addDomainMarker(hfTarget, Layer.BACKGROUND);
    plot.setNoDataMessage(
            java.util.ResourceBundle.getBundle("com/sdk/connector/chart/Bundle").getString("message.wait"));

    chartPanel = new ChartPanel(chart);

    panel.setLayout(new GridLayout(0, 1));
    panel.add(chartPanel);
    panel.repaint();
    panel.revalidate();

}

From source file:com.piketec.jenkins.plugins.tpt.publisher.PieChart.java

/**
 * Render the pie chart with the given height
 * /*from w  w  w  . j av a2s . c  om*/
 * @param height
 *          The height of the resulting image
 * @return The pie chart rendered as an image
 */
public BufferedImage render(int height) {
    BufferedImage image = new BufferedImage(totalWidth, totalHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = image.createGraphics();
    g2.scale(zoom, zoom);
    // fill background to white
    g2.setColor(Color.WHITE);
    g2.fill(new Rectangle(totalWidth, totalHeight));
    // prepare render hints
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
            RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2.setStroke(new BasicStroke(4, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

    // draw shadow image
    g2.drawImage(pieShadow.getImage(), 0, 0, pieShadow.getImageObserver());

    double start = 0;
    List<Arc2D> pies = new ArrayList<>();
    // pie segmente erzeugen und fuellen
    if (total == 0) {
        g2.setColor(BRIGHT_GRAY);
        g2.fillOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
        g2.setColor(Color.WHITE);
        g2.drawOval(centerX - radius, centerY - radius, 2 * radius, 2 * radius);
    } else {
        for (Segment s : segments) {
            double portionDegrees = s.getPortion() / total;
            Arc2D pie = paintPieSegment(g2, start, portionDegrees, s.getColor());
            if (withSubSegments) {
                double smallRadius = radius * s.getSubSegmentRatio();
                paintPieSegment(g2, start, portionDegrees, smallRadius, s.getColor().darker());
            }
            start += portionDegrees;
            // portion degree jetzt noch als String (z.B. "17.3%" oder "20%" zusammenbauen)
            String p = String.format(Locale.ENGLISH, "%.1f", Math.rint(portionDegrees * 1000) / 10.0);
            p = removeSuffix(p, ".0"); // evtl. ".0" bei z.B. "25.0" abschneiden (-> "25")
            s.setPercent(p + "%");
            pies.add(pie);
        }
        // weissen Rahmen um die pie segmente zeichen
        g2.setColor(Color.WHITE);
        for (Arc2D pie : pies) {
            g2.draw(pie);
        }
    }
    // Legende zeichnen
    renderLegend(g2);
    // "xx%" Label direkt auf die pie segmente zeichen
    g2.setColor(Color.WHITE);
    float fontSize = 32f;
    g2.setFont(NORMALFONT.deriveFont(fontSize).deriveFont(Font.BOLD));
    start = 0;
    for (Segment s : segments) {
        if (s.getPortion() < 1E-6) {
            continue; // ignore segments with portions that are extremely small
        }
        double portionDegrees = s.getPortion() / total;
        double angle = start + portionDegrees / 2; // genau in der Mitte des Segments
        double xOffsetForCenteredTxt = 8 * s.getPercent().length(); // assume roughly 8px per char
        int x = (int) (centerX + 0.6 * radius * Math.sin(2 * Math.PI * angle) - xOffsetForCenteredTxt);
        int y = (int) (centerY - 0.6 * radius * Math.cos(2 * Math.PI * angle) + fontSize / 2);
        g2.drawString(s.getPercent(), x, y);
        start += portionDegrees;
    }
    return image;
}