Example usage for java.awt GradientPaint GradientPaint

List of usage examples for java.awt GradientPaint GradientPaint

Introduction

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

Prototype

public GradientPaint(float x1, float y1, Color color1, float x2, float y2, Color color2) 

Source Link

Document

Constructs a simple acyclic GradientPaint object.

Usage

From source file:DrawShapes_2008.java

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    // Paint a gradient for the sky
    GradientPaint background = new GradientPaint(0f, 0f, Color.GRAY.darker(), 0f, (float) getHeight(),
            Color.GRAY.brighter());
    g2d.setPaint(background);//from w w  w  . j  a  v  a  2s  .c o m
    g2d.fillRect(0, 0, getWidth(), 4 * getHeight() / 5);

    // Paint a gradient for the ground
    background = new GradientPaint(0f, (float) 4 * getHeight() / 5, Color.BLACK, 0f, (float) getHeight(),
            Color.GRAY.darker());
    g2d.setPaint(background);
    g2d.fillRect(0, 4 * getHeight() / 5, getWidth(), getHeight() / 5);

    // Enable anti-aliasing to get smooth outlines
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Iterate through all of the current shapes
    for (Shape shape : shapes) {
        // Get the bounds to compute the RadialGradient properties
        Rectangle rect = shape.getBounds();
        Point2D center = new Point2D.Float(rect.x + (float) rect.width / 2.0f,
                rect.y + (float) rect.height / 2.0f);
        float radius = (float) rect.width / 2.0f;
        float[] dist = { 0.1f, 0.9f };
        Color[] colors = { Color.WHITE, Color.BLACK };

        // Create and set a RadialGradient centered on the object,
        // going from white at the center to black at the edges
        RadialGradientPaint paint = new RadialGradientPaint(center, radius, dist, colors);
        g2d.setPaint(paint);

        // Finally, render our shape
        g2d.fill(shape);
    }
}

From source file:TwoStopsGradient.java

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

    GradientPaint p;/*w  w w  . ja  v  a 2s .  c om*/
    p = new GradientPaint(0, 0, new Color(0xFFFFFF), 0, getHeight(), new Color(0xC8D2DE));

    Paint oldPaint = g2.getPaint();
    g2.setPaint(p);
    g2.fillRect(0, 0, getWidth(), getHeight());
    g2.setPaint(oldPaint);

    super.paintComponent(g);
}

From source file:barChart1.BarChartDemo.java

/**
 * Creates a sample chart./*  www.j  a  va2  s  . c  o  m*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private static JFreeChart createChart(CategoryDataset dataset) {

    // create the chart...
    JFreeChart chart = ChartFactory.createBarChart("Belgium vs Italy vs Ireland ", // chart title
            "Category", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setDomainGridlinesVisible(true);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(0, 0, 64));
    GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 64, 0));
    GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(64, 0, 0));

    GradientPaint gp3 = new GradientPaint(0.0f, 0.0f, Color.yellow, 0.0f, 0.0f, new Color(64, 0, 0));
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    renderer.setSeriesPaint(2, gp2);
    renderer.setSeriesPaint(3, gp2);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}

From source file:org.getobjects.samples.HelloChart.Main.java

/**
 * This defines the direct action which can be invoked using:<pre>
 *   /HelloThumbnail/wa/Main/chart?chs=128x128&chartType=p3</pre>
 * /*from w  w  w.jav a 2s.  co  m*/
 * <p>
 * Note that the method returns a java.awt.BufferedImage. This will get
 * rendered to a GIF image by the GoDefaultRenderer.
 * (this method does not return a WOResponse, but it lets the Go machinery
 * deal with the image result object). 
 * 
 * @return a BufferedImage containing the scaled image
 */
public Object chartAction() {
    Dimension size = UGoogleChart.getDimensions(F("chs", "128x128"), null);
    String chartType = (String) F("cht", "p");

    JFreeChart chart = null;
    if (chartType.equals("p")) {
        chart = ChartFactory.createPieChart((String) F("title", "Revenue Chart" /* default title */),
                this.getPieDataset(), UObject.boolValue(F("legend", true)) /* show legend */,
                UObject.boolValue(F("tooltips", true)) /* show tooltips */, false /* no URLs */);
    } else if (chartType.equals("p3")) {
        chart = ChartFactory.createPieChart3D((String) F("title", "Revenue Chart" /* default title */),
                this.getPieDataset(), UObject.boolValue(F("legend", true)) /* show legend */,
                UObject.boolValue(F("tooltips", true)) /* show tooltips */, false /* no URLs */);
    } else if (chartType.startsWith("b")) {
        // bhs, bvs (one bar with multiple values)
        // bhg, bvg (one bar for each row)

        PlotOrientation orientation = PlotOrientation.VERTICAL;
        if (chartType.startsWith("bh"))
            orientation = PlotOrientation.HORIZONTAL;

        if (chartType.endsWith("3")) {
            chart = ChartFactory.createBarChart3D((String) F("title", "Revenue Chart" /* default title */),
                    (String) F("xlabel", "X-Axis"), (String) F("ylabel", "Y-Axis"), getCatDataSet(),
                    orientation, UObject.boolValue(F("legend", true)) /* show legend */,
                    UObject.boolValue(F("tooltips", true)) /* show tooltips */, false /* no URLs */);
        } else {
            chart = ChartFactory.createBarChart((String) F("title", "Revenue Chart" /* default title */),
                    (String) F("xlabel", "X-Axis"), (String) F("ylabel", "Y-Axis"), getRevCatDataSet(),
                    orientation, UObject.boolValue(F("legend", true)) /* show legend */,
                    UObject.boolValue(F("tooltips", true)) /* show tooltips */, false /* no URLs */);
        }
    }

    /* style the chart */

    chart.setBorderVisible(true);
    //chart.setBorderPaint(new Paint(Color.blue));

    Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.blue);
    chart.setBackgroundPaint(p);

    /* style the plot */

    Plot plot = chart.getPlot();
    plot.setBackgroundPaint(new Color(240, 240, 250));

    /* add explosion for Pies */

    if (plot instanceof PiePlot) {
        PiePlot pplot = (PiePlot) chart.getPlot();
        pplot.setExplodePercent("Products", 0.30); // can be multiple explodes
    }

    /* create the image for HTTP delivery */

    return chart.createBufferedImage(size.width, size.height);
}

From source file:org.jfree.chart.demo.CombinedXYPlotDemo5.java

/**
 * Creates a combined XYPlot chart./*from   w w  w  .  j  ava  2 s .c  om*/
 *
 * @return the combined chart.
 */
private JFreeChart createCombinedChart() {

    // create a default chart based on some sample data...
    final TimeSeriesCollection dataset0 = new TimeSeriesCollection();
    final TimeSeries eur = DemoDatasetFactory.createEURTimeSeries();
    dataset0.addSeries(eur);

    final TimeSeriesCollection dataset1 = new TimeSeriesCollection();
    final TimeSeries mav = MovingAverage.createMovingAverage(eur, "EUR/GBP (30 Day MA)", 30, 30);
    dataset1.addSeries(eur);
    dataset1.addSeries(mav);

    final TimeSeriesCollection dataset2 = new TimeSeriesCollection();
    dataset2.addSeries(eur);

    JFreeChart chart = null;

    // make a common vertical axis for all the sub-plots
    final NumberAxis valueAxis = new NumberAxis("Value");
    valueAxis.setAutoRangeIncludesZero(false); // override default

    // make a horizontally combined plot
    final CombinedRangeXYPlot parent = new CombinedRangeXYPlot(valueAxis);

    // add subplot 1...
    final XYPlot subplot1 = new XYPlot(dataset0, new DateAxis("Date 1"), null, new StandardXYItemRenderer());
    subplot1.setDomainCrosshairVisible(true);
    subplot1.setRangeCrosshairVisible(true);
    parent.add(subplot1, 1);

    // add subplot 2...
    final XYPlot subplot2 = new XYPlot(dataset1, new DateAxis("Date 2"), null, new StandardXYItemRenderer());
    subplot2.setDomainCrosshairVisible(true);
    subplot2.setRangeCrosshairVisible(true);
    parent.add(subplot2, 1);

    // add subplot 3...
    final XYPlot subplot3 = new XYPlot(dataset2, new DateAxis("Date 3"), null, new XYBarRenderer(0.20));
    subplot3.setDomainCrosshairVisible(true);
    subplot3.setRangeCrosshairVisible(true);
    parent.add(subplot3, 1);

    // now make the top level JFreeChart
    chart = new JFreeChart("Demo Chart", JFreeChart.DEFAULT_TITLE_FONT, parent, true);

    // then customise it a little...
    final TextTitle subtitle = new TextTitle("This is a subtitle", new Font("SansSerif", Font.BOLD, 12));
    chart.addSubtitle(subtitle);
    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
    return chart;

}

From source file:io.github.dsheirer.spectrum.SpectrumPanel.java

/**
 * Draws the current fft spectrum with a line and a gradient fill.
 *//*from   www  .j av  a  2 s  . co m*/
private void drawSpectrum(Graphics2D graphics) {
    Dimension size = getSize();

    //Draw the background
    Rectangle background = new Rectangle(0, 0, size.width, size.height);
    graphics.setColor(mColorSpectrumBackground);
    graphics.draw(background);
    graphics.fill(background);

    //Define the gradient
    GradientPaint gradient = new GradientPaint(0, (getSize().height - mSpectrumInset) / 2,
            mColorSpectrumGradientTop, 0, getSize().height, mColorSpectrumGradientBottom);

    graphics.setBackground(mColorSpectrumBackground);

    GeneralPath spectrumShape = new GeneralPath();

    //Start at the lower right inset point
    spectrumShape.moveTo(size.getWidth(), size.getHeight() - mSpectrumInset);

    //Draw to the lower left
    spectrumShape.lineTo(0, size.getHeight() - mSpectrumInset);

    float[] bins = getBins();

    //If we have FFT data to display ...
    if (bins != null) {
        float insideHeight = size.height - mSpectrumInset;

        float scalor = insideHeight / -mDBScale;

        /* Calculate based on bin size - 1, since bin 0 is rendered at zero
           * and the last bin is rendered at the width */
        float binSize = (float) size.width / ((float) (bins.length));

        for (int x = 0; x < bins.length; x++) {
            float height;

            height = bins[x] * scalor;

            if (height > insideHeight) {
                height = insideHeight;
            }

            if (height < 0) {
                height = 0;
            }

            float xAxis = (float) x * binSize;

            spectrumShape.lineTo(xAxis, height);
        }
    }
    //Otherwise show an empty spectrum
    else {
        //Draw Left Size
        graphics.setPaint(gradient);
        spectrumShape.lineTo(0, size.getHeight() - mSpectrumInset);
        //Draw Middle
        spectrumShape.lineTo(size.getWidth(), size.getHeight() - mSpectrumInset);
    }

    //Draw Right Side
    spectrumShape.lineTo(size.getWidth(), size.getHeight() - mSpectrumInset);

    graphics.setPaint(gradient);
    graphics.draw(spectrumShape);
    graphics.fill(spectrumShape);

    graphics.setPaint(mColorSpectrumLine);

    //Draw the bottom line under the spectrum
    graphics.draw(new Line2D.Float(0, size.height - mSpectrumInset, size.width, size.height - mSpectrumInset));
}

From source file:grafici.StatisticheBarChart.java

/**
 * Creates a sample chart./*from  ww w  .j  ava  2 s  .  com*/
 * 
 * @param dataset
 *            the dataset.
 * 
 * @return The chart.
 */
private static JFreeChart createChart(CategoryDataset dataset, Table results, int variabile, int valore) {

    // create the chart...
    JFreeChart chart = ChartFactory.createBarChart(titolo, // chart
            // title
            "", // domain axis label
            results.getColumn(valore).getText().toUpperCase(), // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    CategoryPlot plot = (CategoryPlot) chart.getPlot();

    // ******************************************************************
    // More than 150 demo applications are included with the JFreeChart
    // Developer Guide...for more information, see:
    //
    // > http://www.object-refinery.com/jfreechart/guide.html
    //
    // ******************************************************************

    // set the range axis to display integers only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, new Color(0, 0, 64));
    GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, new Color(0, 64, 0));
    GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, new Color(64, 0, 0));
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    renderer.setSeriesPaint(2, gp2);

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 12.0));
    // OPTIONAL CUSTOMISATION COMPLETED.

    return chart;

}

From source file:edu.ucla.stat.SOCR.chart.demo.BarChartDemo4.java

protected JFreeChart createLegend(CategoryDataset dataset) {

    //  JFreeChart chart = ChartFactory.createAreaChart(
    JFreeChart chart = ChartFactory.createBarChart(chartTitle, // chart title
            domainLabel, // domain axis label
            rangeLabel, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );//www  . ja  v a  2  s. com

    // NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
    chart.setBackgroundPaint(Color.white);
    CategoryPlot plot = chart.getCategoryPlot();

    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray);
    GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray);

    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    // renderer.setDrawOutlines(true);
    // renderer.setUseFillPaint(true);
    // renderer.setFillPaint(Color.white);
    renderer.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator());
    return chart;

}

From source file:edu.umn.natsrl.evaluation.ContourPlotter.java

private boolean createContourPlot() {

    if (chart != null) {
        return true;
    }/*from  w  ww .jav a2 s.co  m*/

    if (this.cv == null || this.ncv == 0 || this.colors == null) {
        return false;
    }

    String date = null;
    Vector<EvaluationResult> results = (Vector<EvaluationResult>) this.eval.getResult().clone();

    if (results.size() > 2) {
        date = "  [average of " + (results.size() - 1) + " days]";
    } else {
        date = "  [" + results.get(0).getName() + "]";
    }

    final String title = section.getName() + date;
    final String xAxisLabel = "";
    final String yAxisLabel = "";
    final String zAxisLabel = "";

    this.xAxis = new NumberAxis(xAxisLabel);
    this.yAxis = new NumberAxis(yAxisLabel);
    this.zColorBar = new ColorBar(zAxisLabel);

    if (this.xAxis instanceof NumberAxis) {
        ((NumberAxis) this.xAxis).setAutoRangeIncludesZero(false);
    }

    this.yAxis.setAutoRangeIncludesZero(false);

    this.yAxis.setStreetName(this.streetNames);
    ((NumberAxis) this.xAxis).setTime(this.plotTimes);
    ((NumberAxis) this.zColorBar.getAxis()).setColor(cv, unit);
    ((NumberAxis) this.xAxis).setLowerMargin(0.0);
    ((NumberAxis) this.xAxis).setUpperMargin(0.0);

    this.yAxis.setLowerMargin(0.0);
    this.yAxis.setUpperMargin(0.0);
    this.xAxis.setRange(0, numX - 1);
    this.yAxis.setRange(0, numY - 1);
    this.zColorBar.getAxis().setTickMarksVisible(true);

    final ContourDataset data = createDataset();
    //        for(Number n : data.getZValues()) {
    //            System.out.println("Data=" + n);
    //        }
    final ContourPlot plot = new ContourPlot(data, this.xAxis, this.yAxis, this.zColorBar, ncv);

    plot.SetContourValue(cv, ncv, colors, numX, numY);
    plot.setDataAreaRatio(ratio);

    chart = new JFreeChart(title, null, plot, false);
    chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.white));

    return true;
}

From source file:Client.Gui.TeamBarChart.java

/**
 * Creates a sample chart./* w  ww  .  j  ava2 s .  c  om*/
 * 
 * @param dataset  the dataset.
 * 
 * @return The chart.
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    // create the chart...
    final JFreeChart chart = ChartFactory.createBarChart("Distribution of Trainging", // chart title
            "", // domain axis label
            "", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips?
            false // URLs?
    );

    // set the background color for the chart...
    chart.setBackgroundPaint(Color.white);

    // get a reference to the plot for further customisation...
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);

    // set the range axis to display integers only...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    // disable bar outlines...
    final BarRenderer renderer = (BarRenderer) plot.getRenderer();
    renderer.setDrawBarOutline(false);

    // set up gradient paints for series...
    final GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 0.0f, 0.0f, Color.lightGray);
    final GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 0.0f, 0.0f, Color.lightGray);
    final GradientPaint gp2 = new GradientPaint(0.0f, 0.0f, Color.red, 0.0f, 0.0f, Color.lightGray);
    renderer.setSeriesPaint(0, gp0);
    renderer.setSeriesPaint(1, gp1);
    renderer.setSeriesPaint(2, gp2);

    final CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));

    return chart;

}