Example usage for java.awt BasicStroke BasicStroke

List of usage examples for java.awt BasicStroke BasicStroke

Introduction

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

Prototype

public BasicStroke() 

Source Link

Document

Constructs a new BasicStroke with defaults for all attributes.

Usage

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).//from ww  w.  j a  v  a2s.  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:org.owasp.benchmark.score.report.Scatter.java

private JFreeChart display(String title, int height, int width, OverallResults or) {
    JFrame f = new JFrame(title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Note: this is a little weird, since each point is a separate series
    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series = new XYSeries("Scores");
    for (OverallResult r : or.getResults()) {
        series.add(r.fpr * 100, r.tpr * 100);
    }//from  www.  ja  v a2  s.co m
    dataset.addSeries(series);

    chart = ChartFactory.createScatterPlot(title, "False Positive Rate", "True Positive Rate", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    String fontName = "Arial";
    DecimalFormat pctFormat = new DecimalFormat("0'%'");

    theme = (StandardChartTheme) org.jfree.chart.StandardChartTheme.createJFreeTheme();
    theme.setExtraLargeFont(new Font(fontName, Font.PLAIN, 24)); // title
    theme.setLargeFont(new Font(fontName, Font.PLAIN, 20)); // axis-title
    theme.setRegularFont(new Font(fontName, Font.PLAIN, 16));
    theme.setSmallFont(new Font(fontName, Font.PLAIN, 12));
    theme.setRangeGridlinePaint(Color.decode("#C0C0C0"));
    theme.setPlotBackgroundPaint(Color.white);
    theme.setChartBackgroundPaint(Color.white);
    theme.setGridBandPaint(Color.red);
    theme.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
    theme.setBarPainter(new StandardBarPainter());
    theme.setAxisLabelPaint(Color.decode("#666666"));
    theme.apply(chart);

    XYPlot xyplot = chart.getXYPlot();
    NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis();
    NumberAxis domainAxis = (NumberAxis) xyplot.getDomainAxis();

    xyplot.setOutlineVisible(true);

    rangeAxis.setRange(-9.99, 109.99);
    rangeAxis.setNumberFormatOverride(pctFormat);
    rangeAxis.setTickLabelPaint(Color.decode("#666666"));
    rangeAxis.setMinorTickCount(5);
    rangeAxis.setTickUnit(new NumberTickUnit(10));
    rangeAxis.setAxisLineVisible(true);
    rangeAxis.setMinorTickMarksVisible(true);
    rangeAxis.setTickMarksVisible(true);
    rangeAxis.setLowerMargin(10);
    rangeAxis.setUpperMargin(10);
    xyplot.setRangeGridlineStroke(new BasicStroke());
    xyplot.setRangeGridlinePaint(Color.lightGray);
    xyplot.setRangeMinorGridlinePaint(Color.decode("#DDDDDD"));
    xyplot.setRangeMinorGridlinesVisible(true);

    domainAxis.setRange(-5, 105);
    domainAxis.setNumberFormatOverride(pctFormat);
    domainAxis.setTickLabelPaint(Color.decode("#666666"));
    domainAxis.setMinorTickCount(5);
    domainAxis.setTickUnit(new NumberTickUnit(10));
    domainAxis.setAxisLineVisible(true);
    domainAxis.setTickMarksVisible(true);
    domainAxis.setMinorTickMarksVisible(true);
    domainAxis.setLowerMargin(10);
    domainAxis.setUpperMargin(10);
    xyplot.setDomainGridlineStroke(new BasicStroke());
    xyplot.setDomainGridlinePaint(Color.lightGray);
    xyplot.setDomainMinorGridlinePaint(Color.decode("#DDDDDD"));
    xyplot.setDomainMinorGridlinesVisible(true);

    chart.setTextAntiAlias(true);
    chart.setAntiAlias(true);
    chart.removeLegend();
    chart.setPadding(new RectangleInsets(20, 20, 20, 20));
    xyplot.getRenderer().setSeriesPaint(0, Color.decode("#4572a7"));

    //        // setup item labels
    //        XYItemRenderer renderer = xyplot.getRenderer();
    //        Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 7.0f, 7.0f);
    //        for ( int i = 0; i < dataset.getSeriesCount(); i++ ) {
    //            renderer.setSeriesShape(i, circle);
    //            renderer.setSeriesPaint(i, Color.blue);
    //            String letter = "" + ((String)dataset.getSeries(i).getKey()).charAt(0);
    //            StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator(letter); 
    //            renderer.setSeriesItemLabelGenerator(i, generator); 
    //            renderer.setSeriesItemLabelsVisible(i, true);
    //            ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER );
    //            renderer.setSeriesPositiveItemLabelPosition(i, position);
    //        }

    makeDataLabels(or, xyplot);
    makeLegend(or, 57, 48, dataset, xyplot);

    // put legend inside plot
    //        LegendTitle lt = new LegendTitle(xyplot);
    //        lt.setItemFont(theme.getSmallFont());
    //        lt.setPosition(RectangleEdge.RIGHT);
    //        lt.setItemFont(theme.getSmallFont());
    //        XYTitleAnnotation ta = new XYTitleAnnotation(.7, .55, lt, RectangleAnchor.TOP_LEFT);
    //        ta.setMaxWidth(0.48);
    //        xyplot.addAnnotation(ta);

    // draw guessing line
    Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 6, 3 },
            0);
    XYLineAnnotation guessing = new XYLineAnnotation(-5, -5, 105, 105, dashed, Color.red);
    xyplot.addAnnotation(guessing);

    XYPointerAnnotation worse = makePointer(75, 0, "Worse than guessing", TextAnchor.TOP_CENTER, 90);
    xyplot.addAnnotation(worse);

    XYPointerAnnotation better = makePointer(25, 100, "Better than guessing", TextAnchor.BOTTOM_CENTER, 270);
    xyplot.addAnnotation(better);

    XYTextAnnotation time = new XYTextAnnotation("Tool run time: " + or.getTime(), 12, -5.6);
    time.setTextAnchor(TextAnchor.TOP_LEFT);
    time.setFont(theme.getRegularFont());
    time.setPaint(Color.red);
    xyplot.addAnnotation(time);

    XYTextAnnotation stroketext = new XYTextAnnotation("                     Random Guess", 88, 107);
    stroketext.setTextAnchor(TextAnchor.CENTER_RIGHT);
    stroketext.setBackgroundPaint(Color.white);
    stroketext.setPaint(Color.red);
    stroketext.setFont(theme.getRegularFont());
    xyplot.addAnnotation(stroketext);

    XYLineAnnotation strokekey = new XYLineAnnotation(58, 107, 68, 107, dashed, Color.red);
    xyplot.setBackgroundPaint(Color.white);
    xyplot.addAnnotation(strokekey);

    ChartPanel cp = new ChartPanel(chart, height, width, 400, 400, 1200, 1200, false, false, false, false,
            false, false);
    f.add(cp);
    f.pack();
    f.setLocationRelativeTo(null);
    //      f.setVisible(true);
    return chart;
}

From source file:org.owasp.benchmark.score.report.ScatterScores.java

private JFreeChart display(String title, int height, int width, List<Report> toolResults) {
    JFrame f = new JFrame(title);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    XYSeriesCollection dataset = new XYSeriesCollection();
    XYSeries series = new XYSeries("Scores");
    for (int i = 0; i < toolResults.size(); i++) {
        Report toolReport = toolResults.get(i);
        OverallResults overallResults = toolReport.getOverallResults();
        series.add(overallResults.getFalsePositiveRate() * 100, overallResults.getTruePositiveRate() * 100);
    }//from ww w  .  j a  va2s .c  om
    dataset.addSeries(series);

    chart = ChartFactory.createScatterPlot(title, "False Positive Rate", "True Positive Rate", dataset,
            PlotOrientation.VERTICAL, true, true, false);
    String fontName = "Arial";
    DecimalFormat pctFormat = new DecimalFormat("0'%'");

    theme = (StandardChartTheme) org.jfree.chart.StandardChartTheme.createJFreeTheme();
    theme.setExtraLargeFont(new Font(fontName, Font.PLAIN, 24)); // title
    theme.setLargeFont(new Font(fontName, Font.PLAIN, 20)); // axis-title
    theme.setRegularFont(new Font(fontName, Font.PLAIN, 16));
    theme.setSmallFont(new Font(fontName, Font.PLAIN, 12));
    theme.setRangeGridlinePaint(Color.decode("#C0C0C0"));
    theme.setPlotBackgroundPaint(Color.white);
    theme.setChartBackgroundPaint(Color.white);
    theme.setGridBandPaint(Color.red);
    theme.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
    theme.setBarPainter(new StandardBarPainter());
    theme.setAxisLabelPaint(Color.decode("#666666"));
    theme.apply(chart);

    XYPlot xyplot = chart.getXYPlot();
    NumberAxis rangeAxis = (NumberAxis) xyplot.getRangeAxis();
    NumberAxis domainAxis = (NumberAxis) xyplot.getDomainAxis();

    xyplot.setOutlineVisible(true);

    rangeAxis.setRange(-5, 109.99);
    rangeAxis.setNumberFormatOverride(pctFormat);
    rangeAxis.setTickLabelPaint(Color.decode("#666666"));
    rangeAxis.setMinorTickCount(5);
    rangeAxis.setTickUnit(new NumberTickUnit(10));
    rangeAxis.setAxisLineVisible(true);
    rangeAxis.setMinorTickMarksVisible(true);
    rangeAxis.setTickMarksVisible(true);
    rangeAxis.setLowerMargin(10);
    rangeAxis.setUpperMargin(10);
    xyplot.setRangeGridlineStroke(new BasicStroke());
    xyplot.setRangeGridlinePaint(Color.lightGray);
    xyplot.setRangeMinorGridlinePaint(Color.decode("#DDDDDD"));
    xyplot.setRangeMinorGridlinesVisible(true);

    domainAxis.setRange(-5, 105);
    domainAxis.setNumberFormatOverride(pctFormat);
    domainAxis.setTickLabelPaint(Color.decode("#666666"));
    domainAxis.setMinorTickCount(5);
    domainAxis.setTickUnit(new NumberTickUnit(10));
    domainAxis.setAxisLineVisible(true);
    domainAxis.setTickMarksVisible(true);
    domainAxis.setMinorTickMarksVisible(true);
    domainAxis.setLowerMargin(10);
    domainAxis.setUpperMargin(10);
    xyplot.setDomainGridlineStroke(new BasicStroke());
    xyplot.setDomainGridlinePaint(Color.lightGray);
    xyplot.setDomainMinorGridlinePaint(Color.decode("#DDDDDD"));
    xyplot.setDomainMinorGridlinesVisible(true);

    chart.setTextAntiAlias(true);
    chart.setAntiAlias(true);
    chart.removeLegend();
    chart.setPadding(new RectangleInsets(20, 20, 20, 20));
    xyplot.getRenderer().setSeriesPaint(0, Color.decode("#4572a7"));

    //        // setup item labels
    //        XYItemRenderer renderer = xyplot.getRenderer();
    //        Shape circle = new Ellipse2D.Float(-2.0f, -2.0f, 7.0f, 7.0f);
    //        for ( int i = 0; i < dataset.getSeriesCount(); i++ ) {
    //            renderer.setSeriesShape(i, circle);
    //            renderer.setSeriesPaint(i, Color.blue);
    //            String label = ""+((String)dataset.getSeries(i).getKey());
    //            int idx = label.indexOf( ':');
    //            label = label.substring( 0, idx );
    //            StandardXYItemLabelGenerator generator = new StandardXYItemLabelGenerator(label); 
    //            renderer.setSeriesItemLabelGenerator(i, generator); 
    //            renderer.setSeriesItemLabelsVisible(i, true);
    //            ItemLabelPosition position = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER );
    //            renderer.setSeriesPositiveItemLabelPosition(i, position);
    //        }

    makeDataLabels(toolResults, xyplot);
    makeLegend(toolResults, 57, 48, dataset, xyplot);

    Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 6, 3 },
            0);
    for (XYDataItem item : (List<XYDataItem>) series.getItems()) {
        double x = item.getX().doubleValue();
        double y = item.getY().doubleValue();
        double z = (x + y) / 2;
        XYLineAnnotation score = new XYLineAnnotation(x, y, z, z, dashed, Color.blue);
        xyplot.addAnnotation(score);
    }

    //        // put legend inside plot
    //        LegendTitle lt = new LegendTitle(xyplot);
    //        lt.setItemFont(theme.getSmallFont());
    //        lt.setPosition(RectangleEdge.RIGHT);
    //        lt.setItemFont(theme.getSmallFont());
    //        XYTitleAnnotation ta = new XYTitleAnnotation(.7, .55, lt, RectangleAnchor.TOP_LEFT);
    //        ta.setMaxWidth(0.48);
    //        xyplot.addAnnotation(ta);

    // draw guessing line
    XYLineAnnotation guessing = new XYLineAnnotation(-5, -5, 105, 105, dashed, Color.red);
    xyplot.addAnnotation(guessing);

    XYPointerAnnotation worse = makePointer(75, 5, "Worse than guessing", TextAnchor.TOP_CENTER, 90);
    xyplot.addAnnotation(worse);

    XYPointerAnnotation better = makePointer(25, 100, "Better than guessing", TextAnchor.BOTTOM_CENTER, 270);
    xyplot.addAnnotation(better);

    XYTextAnnotation stroketext = new XYTextAnnotation("                     Random Guess", 88, 107);
    stroketext.setTextAnchor(TextAnchor.CENTER_RIGHT);
    stroketext.setBackgroundPaint(Color.white);
    stroketext.setPaint(Color.red);
    stroketext.setFont(theme.getRegularFont());
    xyplot.addAnnotation(stroketext);

    XYLineAnnotation strokekey = new XYLineAnnotation(58, 107, 68, 107, dashed, Color.red);
    xyplot.setBackgroundPaint(Color.white);
    xyplot.addAnnotation(strokekey);

    ChartPanel cp = new ChartPanel(chart, height, width, 400, 400, 1200, 1200, false, false, false, false,
            false, false);
    f.add(cp);
    f.pack();
    f.setLocationRelativeTo(null);
    //      f.setVisible(true);
    return chart;
}