Example usage for java.awt Polygon Polygon

List of usage examples for java.awt Polygon Polygon

Introduction

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

Prototype

public Polygon(int[] xpoints, int[] ypoints, int npoints) 

Source Link

Document

Constructs and initializes a Polygon from the specified parameters.

Usage

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

/**
 * Creates a sample chart.// www .ja v  a  2 s .  c o m
 * 
 * @param dataset  the dataset.
 * 
 * @return a chart.
 */
private JFreeChart createChart(final CategoryDataset dataset) {

    final JFreeChart chart = ChartFactory.createLineChart("Line Chart Demo 5", // chart title
            "Type", // domain axis label
            "Value", // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            true, // include legend
            true, // tooltips
            false // urls
    );

    //        final StandardLegend legend = (StandardLegend) chart.getLegend();
    //      legend.setDisplaySeriesShapes(true);

    final Shape[] shapes = new Shape[3];
    int[] xpoints;
    int[] ypoints;

    // right-pointing triangle
    xpoints = new int[] { -3, 3, -3 };
    ypoints = new int[] { -3, 0, 3 };
    shapes[0] = new Polygon(xpoints, ypoints, 3);

    // vertical rectangle
    shapes[1] = new Rectangle2D.Double(-2, -3, 3, 6);

    // left-pointing triangle
    xpoints = new int[] { -3, 3, 3 };
    ypoints = new int[] { 0, -3, 3 };
    shapes[2] = new Polygon(xpoints, ypoints, 3);

    final DrawingSupplier supplier = new DefaultDrawingSupplier(DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, shapes);
    final CategoryPlot plot = chart.getCategoryPlot();
    plot.setDrawingSupplier(supplier);

    chart.setBackgroundPaint(Color.yellow);

    // set the stroke for each series...
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 10.0f, 6.0f }, 0.0f));
    plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 6.0f, 6.0f }, 0.0f));
    plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 2.0f, 6.0f }, 0.0f));

    // customise the renderer...
    final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    //        renderer.setDrawShapes(true);
    renderer.setItemLabelsVisible(true);
    //      renderer.setLabelGenerator(new StandardCategoryLabelGenerator());

    // customise the range axis...
    final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(false);
    rangeAxis.setUpperMargin(0.12);

    return chart;

}

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

/**
 * Creates a sample chart.//from   w w  w  .j  a v  a  2  s .c  o m
 * 
 * @param dataset  the dataset.
 * 
 * @return a chart.
 */
protected JFreeChart createChart(CategoryDataset dataset) {

    JFreeChart chart = ChartFactory.createLineChart(chartTitle, // chart title
            domainLabel, // domain axis label
            rangeLabel, // range axis label
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            !legendPanelOn, // include legend
            true, // tooltips
            false // urls
    );

    chart.setBackgroundPaint(Color.white);

    Shape[] shapes = new Shape[3];
    int[] xpoints;
    int[] ypoints;

    // right-pointing triangle
    xpoints = new int[] { -3, 3, -3 };
    ypoints = new int[] { -3, 0, 3 };
    shapes[0] = new Polygon(xpoints, ypoints, 3);

    // vertical rectangle
    shapes[1] = new Rectangle2D.Double(-2, -3, 3, 6);

    // left-pointing triangle
    xpoints = new int[] { -3, 3, 3 };
    ypoints = new int[] { 0, -3, 3 };
    shapes[2] = new Polygon(xpoints, ypoints, 3);

    DrawingSupplier supplier = new DefaultDrawingSupplier(DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE, shapes);
    CategoryPlot plot = chart.getCategoryPlot();
    plot.setOrientation(PlotOrientation.HORIZONTAL);
    plot.setBackgroundPaint(Color.lightGray);
    plot.setDomainGridlinePaint(Color.white);
    plot.setRangeGridlinePaint(Color.white);
    plot.setDrawingSupplier(supplier);

    // set the stroke for each series...
    plot.getRenderer().setSeriesStroke(0, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 10.0f, 6.0f }, 0.0f));
    plot.getRenderer().setSeriesStroke(1, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 6.0f, 6.0f }, 0.0f));
    plot.getRenderer().setSeriesStroke(2, new BasicStroke(2.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND,
            1.0f, new float[] { 2.0f, 6.0f }, 0.0f));

    // customise the renderer...
    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setBaseShapesVisible(true);
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setLegendItemLabelGenerator(new SOCRCategorySeriesLabelGenerator());

    // customise the range axis...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setAutoRangeIncludesZero(false);
    rangeAxis.setUpperMargin(0.12);

    setCategorySummary(dataset);
    return chart;

}

From source file:orchestration.path.RectShape.java

protected Object clone() throws CloneNotSupportedException {
    RectShape clone = (RectShape) super.clone();

    clone.poly = new Polygon(clone.poly.xpoints.clone(), clone.poly.ypoints.clone(), clone.poly.npoints);

    clone.centerPt = new Point(clone.centerPt.x, clone.centerPt.y);
    return clone;
}

From source file:no.oddsor.simulator3.json.SensorReader.java

public Collection<Sensor> getSensors() {
    Collection<Sensor> sensors = new ArrayList<>();
    if (object == null)
        return sensors;

    JSONArray sensorList = (JSONArray) object.get("Sensors");
    for (Object sensorOb : sensorList) {
        JSONObject sensorObject = (JSONObject) sensorOb;
        Sensor sensor = null;//from   ww w  . jav  a 2  s  .c o  m
        String type = (String) sensorObject.get("Type");
        String name = (String) sensorObject.get("Name");
        JSONArray locationArray = (JSONArray) sensorObject.get("Position");
        Point location = null;
        if (locationArray != null) {
            location = new Point(Integer.parseInt(locationArray.get(0).toString()),
                    Integer.parseInt(locationArray.get(1).toString()));
        }
        switch (type) {
        case "Contact":
            String attached = (String) sensorObject.get("Attached_to");
            sensor = new Contact(name, attached);
            break;
        case "Camera":
            JSONArray range = (JSONArray) sensorObject.get("Range");
            double[] dArray = new double[range.size()];
            for (int i = 0; i < dArray.length; i++) {
                dArray[i] = Double.parseDouble(range.get(i).toString());
            }
            if (!sensorObject.containsKey("Resolution"))
                sensor = new Camera(name, location,
                        Double.parseDouble(sensorObject.get("Direction").toString()),
                        Double.parseDouble(sensorObject.get("FieldOfView").toString()), dArray);
            else
                sensor = new Camera(name, location,
                        Double.parseDouble(sensorObject.get("Direction").toString()),
                        Double.parseDouble(sensorObject.get("FieldOfView").toString()), dArray,
                        Integer.parseInt(sensorObject.get("Resolution").toString()));
            break;
        case "Door":
            JSONArray dimensionArray = (JSONArray) sensorObject.get("Size");
            Dimension dims = new Dimension(Integer.parseInt(dimensionArray.get(0).toString()),
                    Integer.parseInt(dimensionArray.get(1).toString()));
            sensor = new Door(name, location, dims);
            break;
        case "MotionSensor":
            if (sensorObject.containsKey("Radius"))
                sensor = new MotionSensor(name, location,
                        Double.parseDouble(sensorObject.get("Radius").toString()));
            else {
                sensor = new MotionSensor(name, location,
                        Double.parseDouble(sensorObject.get("Direction").toString()),
                        Double.parseDouble(sensorObject.get("Range").toString()),
                        Double.parseDouble(sensorObject.get("FieldOfView").toString()));
            }
        }
        if (sensorObject.containsKey("Exclude")) {
            JSONArray excludeArray = (JSONArray) sensorObject.get("Exclude");
            for (Object exclude : excludeArray) {
                JSONObject exclud = (JSONObject) exclude;
                JSONArray edgeArray = (JSONArray) exclud.get("Edge");
                Point edgeLocation = new Point(Integer.parseInt(edgeArray.get(0).toString()),
                        Integer.parseInt(edgeArray.get(1).toString()));
                String direction = (String) exclud.get("Direction");
                Point edge = null;
                Dimension dim = new Dimension(1000, 1000);
                switch (direction) {
                case ("Northeast"):
                    edge = new Point(edgeLocation.x, edgeLocation.y - 1000);
                    break;
                case ("Northwest"):
                    edge = new Point(edgeLocation.x - 1000, edgeLocation.y - 1000);
                    break;
                case ("Southeast"):
                    edge = new Point(edgeLocation.x, edgeLocation.y);
                    break;
                case ("Southwest"):
                    edge = new Point(edgeLocation.x - 1000, edgeLocation.y);
                }
                Rectangle exluTangle = new Rectangle(edge, dim);
                sensor.removeArea(new Area(exluTangle));
            }
        }
        if (sensorObject.containsKey("Confine")) {
            JSONArray coords = (JSONArray) sensorObject.get("Confine");
            int[] x = new int[coords.size()];
            int[] y = new int[coords.size()];
            for (int i = 0; i < coords.size(); i++) {
                JSONArray coord = (JSONArray) coords.get(i);
                x[i] = Integer.parseInt(coord.get(0).toString());
                y[i] = Integer.parseInt(coord.get(1).toString());
            }
            Polygon p = new Polygon(x, y, coords.size());
            Area excluPoly = new Area(p);
            sensor.confineToArea(excluPoly);
        }
        sensors.add(sensor);
    }

    return sensors;
}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

private void drawArrow(Graphics2D g, double speed) {
    int startX = -(arrowWidth / 2);
    int startY = -(arrowHeight / 2);
    // draw main arrow line
    g.fill(new Rectangle2D.Double(startX, startY, arrowWidth, arrowHeight));

    // drawing speed feathers (representing 25 ms each)
    int featherOffset = 0;
    int featherHeight = arrowHeight / featherPadding;
    int flagFeathers = (int) speed / 25;
    int[] flagFeatherX = { 1, -featherWidth, 1 };
    for (int i = 0; i < flagFeathers; i++) {
        if (i > 0)
            featherOffset += 1; // more space if multiple flags
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        int flagSize = (int) (featherHeight * 1.5f);
        int[] flagFeatherY = { yValue, (yValue - flagSize / 2), yValue - flagSize };
        g.fill(new Polygon(flagFeatherX, flagFeatherY, 3));
        featherOffset += 1;/*from   w  w  w.  j a  v a2 s  .co m*/
    }
    // Add space between flag-feather and next
    if (flagFeathers > 0)
        featherOffset++;
    double remainingSpeed = speed - (flagFeathers * 25);

    // draw full feathers (representing 5 ms each)
    int fullFeathers = (int) remainingSpeed / 5;
    g.setStroke(new BasicStroke(arrowWidth));
    for (int i = 0; i < fullFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, -featherWidth };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }
    remainingSpeed = remainingSpeed - (fullFeathers * 5);

    // draw half feathers (representing 2.5 ms each)
    int halfFeathers = (int) (remainingSpeed / 2.5);
    // never draw half-feathers at the start
    if (featherOffset == 0)
        featherOffset = 1;
    for (int i = 0; i < halfFeathers; i++) {
        int yValue = (arrowHeight / 2) - (featherOffset * featherHeight);
        yValue -= (arrowWidth / 2); // allign with start of arrow
        int[] x = { 1, (-featherWidth / 2) };
        int[] y = { yValue, yValue };
        g.draw(new Polygon(x, y, 2));
        featherOffset += 1;
    }

    g.setStroke(new BasicStroke(1));
}

From source file:org.vast.stt.renderer.JFreeChart.XYPlotBuilder.java

public void visit(PointStyler styler) {
    styler.resetIterators();/*from  w w w.j a va 2  s.c o  m*/
    if (styler.getNumPoints() == 0)
        return;

    // create dataset and renderer            
    XYDataset dataset = new ChartXYDataset(currentItem.getName(), styler);
    XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES);

    // point color and size
    PointGraphic point = styler.getPoint(0);
    renderer.setPaint(new Color(point.r, point.g, point.b, point.a));

    // point shape
    Shape shape = null;
    switch (point.shape) {
    case SQUARE:
        shape = new Rectangle2D.Float(-point.size / 2, -point.size / 2, point.size, point.size);
        break;

    case CIRCLE:
        shape = new Ellipse2D.Float(-point.size / 2, -point.size / 2, point.size, point.size);
        break;

    case TRIANGLE:
        int[] xPoints = new int[] { -(int) point.size / 2, 0, (int) point.size / 2 };
        int[] yPoints = new int[] { (int) point.size / 2, -(int) point.size / 2, (int) point.size / 2 };
        shape = new Polygon(xPoints, yPoints, 3);
        break;
    }
    renderer.setShape(shape);

    // add new dataset and corresponding renderer and range axis
    addDataSet(dataset, renderer);
}

From source file:org.eclipse.birt.chart.device.g2d.G2dRendererBase.java

protected Shape getPolygon(Location loa[]) {
    final int[][] i2a = getCoordinatesAsInts(loa);
    return new Polygon(i2a[0], i2a[1], loa.length);
}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

private Polygon getPolygonHead(int size, int lineHeight) {
    int half = (size / 2);
    int yStart = -(lineHeight / 2);
    int[] x = { -half, half, 0 };
    int[] y = { yStart, yStart, yStart - size };
    return new Polygon(x, y, 3);
}

From source file:oct.analysis.application.OCTSelection.java

private void updateTopLineMarker() {
    int leftEdge = getSelectionLeftEdgeCoordinate();
    int rightEdge = getSelectionRightEdgeCoordinate();
    topLine = new Polygon(new int[] { leftEdge, leftEdge, rightEdge, rightEdge },
            new int[] { this.yPositionOnOct - 2, this.yPositionOnOct + 2, this.yPositionOnOct + 2,
                    this.yPositionOnOct - 2 },
            4);/*www.j  av a 2s.c  om*/
}

From source file:uk.ac.leeds.ccg.andyt.projects.moses.process.RegressionReport.java

public static JFreeChart[] createRegressionCharts(JFreeChart[] t_ScatterPlots,
        Object[] t_RegressionParametersAndXYLineCharts) throws IOException {
    JFreeChart[] result = new JFreeChart[t_ScatterPlots.length];
    Object[] t_RegressionParameters = (Object[]) t_RegressionParametersAndXYLineCharts[0];
    JFreeChart[] t_regressionXYLineCharts = (JFreeChart[]) t_RegressionParametersAndXYLineCharts[1];
    JFreeChart[] t_yequalsxXYLineCharts = (JFreeChart[]) t_RegressionParametersAndXYLineCharts[2];
    for (int i = 0; i < result.length; i++) {
        double[] a_RegressionParameters = (double[]) t_RegressionParameters[i];
        int maxLengthOfIntString = 6;
        String b = String.valueOf(a_RegressionParameters[0]);
        String a = String.valueOf(a_RegressionParameters[1]);
        String rsquare = String.valueOf(a_RegressionParameters[2]);
        String title = "Y=" + a.substring(0, Math.min(a.length(), maxLengthOfIntString)) + "X";
        if (a_RegressionParameters[0] < 0) {
            title += b.substring(0, Math.min(b.length(), maxLengthOfIntString));
        } else {/*  ww w .  j av  a  2  s .  c  o  m*/
            title += "+" + b.substring(0, Math.min(b.length(), maxLengthOfIntString));
        }
        if (a_RegressionParameters.length > 2) {
            title += "    RSquare " + rsquare.substring(0, Math.min(rsquare.length(), maxLengthOfIntString));
        }
        XYLineAndShapeRenderer points_XYLineAndShapeRenderer = new XYLineAndShapeRenderer(false, true);
        XYPlot a_ScatterPlotXYPlot = (XYPlot) t_ScatterPlots[i].getPlot();
        a_ScatterPlotXYPlot.setDataset(0, a_ScatterPlotXYPlot.getDataset());
        points_XYLineAndShapeRenderer.setSeriesPaint(0, Color.blue);

        //Shape[] _Shapes = DefaultDrawingSupplier.createStandardSeriesShapes();
        // System.out.println("There are " + _Shapes.length +
        // " number of Shapes to try...");
        // 0 are square
        // 1 are circles
        // 2 are up-pointing triangle
        // 3 are diamond
        // 4 are horizontal rectangle
        // 5 are down-pointing triangle
        // 6 are horizontal ellipse
        // 7 are right-pointing triangle
        // 8 are vertical rectangle
        // 9 are left-pointing triangle
        int a_SpikeLength = 3;
        int[] xpoints = new int[9];
        int[] ypoints = new int[9];
        xpoints[0] = 0;
        ypoints[0] = 0;
        xpoints[1] = 0;
        ypoints[1] = a_SpikeLength;
        xpoints[2] = 0;
        ypoints[2] = 0;
        xpoints[3] = a_SpikeLength;
        ypoints[3] = 0;
        xpoints[4] = 0;
        ypoints[4] = 0;
        xpoints[5] = 0;
        ypoints[5] = -a_SpikeLength;
        xpoints[6] = 0;
        ypoints[6] = 0;
        xpoints[7] = -a_SpikeLength;
        ypoints[7] = 0;
        xpoints[8] = 0;
        ypoints[8] = 0;
        Shape a_Shape = new Polygon(xpoints, ypoints, xpoints.length);
        // points_XYLineAndShapeRenderer.setSeriesShape( 0, _Shapes[2] );
        points_XYLineAndShapeRenderer.setSeriesShape(0, a_Shape);
        a_ScatterPlotXYPlot.setRenderer(0, points_XYLineAndShapeRenderer);

        XYDataset a_regressionXYLineChartXYDataset = ((XYPlot) t_regressionXYLineCharts[i].getPlot())
                .getDataset();
        XYLineAndShapeRenderer line_XYLineAndShapeRenderer = new XYLineAndShapeRenderer(true, false);
        a_ScatterPlotXYPlot.setDataset(1, a_regressionXYLineChartXYDataset);
        line_XYLineAndShapeRenderer.setPaint(Color.red);
        a_ScatterPlotXYPlot.setRenderer(1, line_XYLineAndShapeRenderer);

        XYDataset a_yequalsxXYLineChartXYDataset = ((XYPlot) t_yequalsxXYLineCharts[i].getPlot()).getDataset();
        XYLineAndShapeRenderer yequalsxline_XYLineAndShapeRenderer = new XYLineAndShapeRenderer(true, false);
        a_ScatterPlotXYPlot.setDataset(2, a_yequalsxXYLineChartXYDataset);
        yequalsxline_XYLineAndShapeRenderer.setPaint(Color.green);
        a_ScatterPlotXYPlot.setRenderer(2, yequalsxline_XYLineAndShapeRenderer);
        result[i] = new JFreeChart(title, a_ScatterPlotXYPlot);
    }
    return result;
}