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:Main.java

public static void main(String[] args) {
    int[] x = new int[] { 1, 2, 3 };
    int[] y = new int[] { 4, 5, 6 };
    Polygon polygon = new Polygon(x, y, x.length);
    try {//from  www  .  ja  v a2s  .  c  o  m
        ObjectOutputStream objectOut = new ObjectOutputStream(
                new BufferedOutputStream(new FileOutputStream("Polygons.bin")));
        objectOut.writeObject(polygon);
        objectOut.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
    try {
        ObjectInputStream objectIn = new ObjectInputStream(
                new BufferedInputStream(new FileInputStream("Polygons.bin")));
        Polygon theLine = (Polygon) objectIn.readObject();
        System.out.println(theLine);
        objectIn.close();
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

From source file:ImageClip.java

/**
 * Clips the input image to the specified shape
 * /*www  .  j ava  2s  . c  o m*/
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip(BufferedImage image, float... clipVerts) {
    assert clipVerts.length >= 6;
    assert clipVerts.length % 2 == 0;

    int[] xp = new int[clipVerts.length / 2];
    int[] yp = new int[xp.length];

    int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

    for (int j = 0; j < xp.length; j++) {
        xp[j] = Math.round(clipVerts[2 * j] * image.getWidth());
        yp[j] = Math.round(clipVerts[2 * j + 1] * image.getHeight());

        minX = Math.min(minX, xp[j]);
        minY = Math.min(minY, yp[j]);
        maxX = Math.max(maxX, xp[j]);
        maxY = Math.max(maxY, yp[j]);
    }

    for (int i = 0; i < xp.length; i++) {
        xp[i] -= minX;
        yp[i] -= minY;
    }

    Polygon clip = new Polygon(xp, yp, xp.length);
    BufferedImage out = new BufferedImage(maxX - minX, maxY - minY, image.getType());
    Graphics g = out.getGraphics();
    g.setClip(clip);

    g.drawImage(image, -minX, -minY, null);
    g.dispose();

    return out;
}

From source file:Main.java

public TestPane() {
    triangleShape = new TriangleShape(new Point2D.Double(50, 0), new Point2D.Double(100, 100),
            new Point2D.Double(0, 100));
    poly = new Polygon(new int[] { 50, 100, 0 }, new int[] { 0, 100, 100 }, 3);
}

From source file:orchestration.path.RectShape.java

public RectShape(float width, float height) {
    centerPt = new Point(0, 0);
    poly = new Polygon(new int[] { (int) width / 2, -(int) width / 2, -(int) width / 2, (int) width / 2 },
            new int[] { (int) height / 2, (int) height / 2, -(int) height / 2, -(int) height / 2 }, 4);
}

From source file:be.ugent.maf.cellmissy.gui.view.renderer.jfreechart.AngularHistogramRenderer.java

@Override
public void drawSeries(Graphics2D g2, Rectangle2D dataArea, PlotRenderingInfo info, PolarPlot plot,
        XYDataset dataset, int seriesIndex) {

    // compute the right color for the paint
    int length = GuiUtils.getAvailableColors().length;
    Color color = GuiUtils.getAvailableColors()[index % length];
    // get all the data points
    int numPoints = dataset.getItemCount(seriesIndex);

    for (int i = 0; i < numPoints; i++) {
        double theta = dataset.getXValue(seriesIndex, i); // the angle at the center         
        double radius = dataset.getYValue(seriesIndex, i); // the frequency

        Point p0 = plot.translateToJava2D(0, 0, plot.getAxis(), dataArea);
        Point p1 = plot.translateToJava2D(theta - binSize, radius, plot.getAxis(), dataArea);
        Point p2 = plot.translateToJava2D(theta + binSize, radius, plot.getAxis(), dataArea);

        Polygon poly = new Polygon(new int[] { p0.x, p1.x, p2.x }, new int[] { p0.y, p1.y, p2.y }, 3);

        g2.setPaint(new Color(color.getRed(), color.getGreen(), color.getBlue(), 175));
        g2.fill(poly);/* www  .  j  av  a 2  s.c o m*/
    }
}

From source file:D20140128.ApacheXMLGraphicsTest.TilingPatternExample.java

private void paintShapes(Graphics2D g2d) {
    g2d.setPaint(paint);//w w w.j  av a2s. c o m
    Rectangle rect = new Rectangle(10, 50, 30, 30);
    g2d.fill(rect);
    rect = new Rectangle(10, 90, 40, 20);
    g2d.fill(rect);
    Polygon poly = new Polygon(new int[] { 50, 100, 150 }, new int[] { 100, 20, 100 }, 3);
    g2d.fill(poly);
}

From source file:org.schreibubi.JCombinations.jfreechart.XYLineAndShapeRendererExtended.java

/**
 * Draws a horizontal line across the chart to represent a 'range marker'.
 * /* www  . j a  v  a2 s  . c  o  m*/
 * @param g2
 *            the graphics device.
 * @param plot
 *            the plot.
 * @param rangeAxis
 *            the range axis.
 * @param marker
 *            the marker line.
 * @param dataArea
 *            the axis data area.
 */
@Override
public void drawRangeMarker(Graphics2D g2, XYPlot plot, ValueAxis rangeAxis, Marker marker,
        Rectangle2D dataArea) {

    if (marker instanceof ValueMarker)
        super.drawRangeMarker(g2, plot, rangeAxis, marker, dataArea);
    else if (marker instanceof IntervalMarker)
        super.drawRangeMarker(g2, plot, rangeAxis, marker, dataArea);
    else if (marker instanceof ArbitraryMarker) {

        ArbitraryMarker im = (ArbitraryMarker) marker;
        ArrayList<Double> xvals = im.getDomainVal();
        ArrayList<Double> lows = im.getRangeLowValues();
        ArrayList<Double> highs = im.getRangeHighValues();
        sort(xvals, lows, highs);

        Range range = rangeAxis.getRange();
        ValueAxis domainAxis = plot.getDomainAxis();
        Range domain = domainAxis.getRange();

        int length = xvals.size();
        int[] xpoly = new int[length * 2];
        int[] ypoly = new int[length * 2];

        for (int i = 0; i < xvals.size(); i++) {
            double x = domain.constrain(xvals.get(i));
            double low = range.constrain(lows.get(i));
            double high = range.constrain(highs.get(i));
            int low2d = (int) Math.round(rangeAxis.valueToJava2D(low, dataArea, plot.getRangeAxisEdge()));
            int high2d = (int) Math.round(rangeAxis.valueToJava2D(high, dataArea, plot.getRangeAxisEdge()));
            int x2d = (int) Math.round(domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge()));
            xpoly[i] = x2d;
            xpoly[2 * length - 1 - i] = x2d;
            ypoly[i] = low2d;
            ypoly[2 * length - 1 - i] = high2d;
        }

        PlotOrientation orientation = plot.getOrientation();
        Polygon poly = null;
        if (orientation == PlotOrientation.HORIZONTAL)
            poly = new Polygon(ypoly, xpoly, length * 2);
        else if (orientation == PlotOrientation.VERTICAL)
            poly = new Polygon(xpoly, ypoly, length * 2);

        Paint p = im.getPaint();
        if (p instanceof GradientPaint) {
            GradientPaint gp = (GradientPaint) p;
            GradientPaintTransformer t = im.getGradientPaintTransformer();
            if (t != null)
                gp = t.transform(gp, poly);
            g2.setPaint(gp);
        } else
            g2.setPaint(p);
        g2.fill(poly);
        /*
         * String label = marker.getLabel(); RectangleAnchor anchor = marker.getLabelAnchor(); if ( label != null ) {
         * Font labelFont = marker.getLabelFont(); g2.setFont( labelFont ); g2.setPaint( marker.getLabelPaint() );
         * Point2D coordinates = calculateRangeMarkerTextAnchorPoint( g2, orientation, dataArea, poly,
         * marker.getLabelOffset(), marker.getLabelOffsetType(), anchor ); TextUtilities.drawAlignedString( label,
         * g2, ( float ) coordinates.getX(), ( float ) coordinates.getY(), marker.getLabelTextAnchor() ); }
         */
    }
}

From source file:org.fhcrc.cpl.viewer.mrm.Utils.java

public static Polygon legendThing(int width, int height) {
    int[] xes = new int[100];
    int[] yes = new int[100];
    int leftX = -1 * (width / 2);
    int bottomY = -1 * (height / 2);
    int pointCounter = 0;
    xes[0] = leftX;//from   w  w  w. jav  a  2s  . c o  m
    yes[0] = bottomY;
    int curX = leftX;
    int curY = bottomY;
    while (curY <= height / 2) {
        xes[pointCounter] = curX;
        yes[pointCounter] = curY;
        pointCounter++;
        curX = width / 2;
        xes[pointCounter] = curX;
        yes[pointCounter] = curY;
        pointCounter++;
        curY++;
        xes[pointCounter] = curX;
        yes[pointCounter] = curY;
        pointCounter++;
        curX = leftX;
    }
    return new Polygon(xes, yes, pointCounter - 1);
}

From source file:com.sun.japex.report.ChartGenerator.java

static protected void configureLineChart(JFreeChart chart) {
    CategoryPlot plot = chart.getCategoryPlot();

    final DrawingSupplier supplier = new DefaultDrawingSupplier(DefaultDrawingSupplier.DEFAULT_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
            DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
            // Draw a small diamond 
            new Shape[] { new Polygon(new int[] { 3, 0, -3, 0 }, new int[] { 0, 3, 0, -3 }, 4) });
    plot.setDomainGridlinePaint(Color.black);
    plot.setRangeGridlinePaint(Color.black);
    plot.setDrawingSupplier(supplier);//from  ww  w .j  a  v  a2  s . c  om

    LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
    renderer.setShapesVisible(true);
    renderer.setStroke(new BasicStroke(2.0f));

    CategoryAxis axis = plot.getDomainAxis();
    axis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_90);
}

From source file:com.robertolosanno.cdt_maven.Cdt.java

public Cdt() {
    //Ontologia/* w w  w  .j  av  a 2  s .  c o m*/
    OntModel m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    loadModel(m);
    ExtendedIterator<OntClass> iter = m.listHierarchyRootClasses();

    // create a simple graph
    graph = new DelegateTree<String, String>();

    createTree(iter);

    //Vincoli
    System.out.println("--- CONSTRAINTS ---");
    ExtendedIterator<ObjectProperty> iterProp = m.listObjectProperties();
    while (iterProp.hasNext()) {
        ObjectProperty objProp = iterProp.next();
        plotConstraint(objProp);
    }

    treeLayout = new TreeLayout<String, String>(graph);
    vv = new VisualizationViewer<String, String>(treeLayout, new Dimension(1200, 600));
    vv.setBackground(Color.white);
    vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
    vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));

    //***************** MODIFICA COLORE VERTICE ************************
    Transformer<String, Paint> vertexColor = new Transformer<String, Paint>() {
        public Paint transform(String s) {
            String[] ss = s.split("#");
            String type = ss[1];
            if (type.equals("root"))
                return Color.lightGray;
            if (type.equals("dim"))
                return Color.BLACK;
            if (type.equals("val"))
                return Color.WHITE;
            if (type.equals("par_val"))
                return Color.WHITE;
            if (type.equals("par_dim"))
                return Color.WHITE;

            return Color.GREEN;
        }
    };
    vv.getRenderContext().setVertexFillPaintTransformer(vertexColor);

    //**************** MODIFICA FORMA VERTICE ***************************

    //Dati per creare un triangolo (lo creaiamo attraverso la classe Polygon)
    final int[] xShape = new int[4];
    final int[] yShape = new int[4];
    final int nShape; // count of points
    // Make a shape
    xShape[0] = -10;
    xShape[1] = 0;
    xShape[2] = 10;
    yShape[0] = 0;
    yShape[1] = 20;
    yShape[2] = 0;
    nShape = 3;

    Transformer<String, Shape> vertexShape = new Transformer<String, Shape>() {
        private final Shape[] styles = { new Rectangle(-10, -10, 20, 20),
                new Ellipse2D.Double(-10, -10, 20, 20), new Polygon(xShape, yShape, nShape) //Triangolo

        };

        @Override
        public Shape transform(String i) {
            String[] type = i.split("#");
            if (type[1].equals("par_val")) {
                return styles[0];
            } else if (type[1].equals("par_dim")) {
                return styles[2];
            } else {
                return styles[1];
            }
        }
    };
    vv.getRenderContext().setVertexShapeTransformer(vertexShape);

    //                          vv.getRenderer().setVertexRenderer(new MyRenderer());

    //**************** MODIFICA FONT LABEL ***************************
    vv.getRenderContext().setVertexFontTransformer(new Transformer<String, Font>() {

        @Override
        public Font transform(String arg0) {
            Font font = new Font("Arial Unicode MS", Font.PLAIN, 11);
            return font;
        }
    });

    // ********************** POSIZIONA LA LABEL SOTTO IL VERTICE ****************************
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.CNTR);
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.S);

    //******************** RIMUOVE DAL TESTO DELLA LABEL DEL VERTICE IL TIPO DI VERTICE ************************

    Transformer<String, String> transformer = new Transformer<String, String>() {
        @Override
        public String transform(String arg0) {
            String[] node = arg0.split("#");
            return node[0];
        }
    };

    vv.getRenderContext().setVertexLabelTransformer(transformer);

    Container content = getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);

    final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse();

    vv.setGraphMouse(graphMouse);

    JComboBox modeBox = graphMouse.getModeComboBox();
    modeBox.addItemListener(graphMouse.getModeListener());
    graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JPanel scaleGrid = new JPanel(new GridLayout(1, 0));
    scaleGrid.setBorder(BorderFactory.createTitledBorder("Zoom"));

    JPanel controls = new JPanel();
    scaleGrid.add(plus);
    scaleGrid.add(minus);
    controls.add(scaleGrid);
    controls.add(modeBox);

    content.add(controls, BorderLayout.SOUTH);
}