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:fr.fg.server.core.TerritoryManager.java

private static Polygon createPolygon(float[][] coords, int offset, int reduction) {
    int[] xpts = new int[coords.length], ypts = new int[coords.length];

    for (int j = 0; j < coords.length; j++) {
        // Calcule l'angle form par le segment suivant le point
        float dx1 = coords[(j + 1) % coords.length][0] - coords[j][0];
        float dy1 = coords[(j + 1) % coords.length][1] - coords[j][1];
        double angle1 = Math.atan2(dy1, dx1);

        // Calcule l'angle form par le segment prcdent le point
        float dx2 = coords[(j - 1 + coords.length) % coords.length][0] - coords[j][0];
        float dy2 = coords[(j - 1 + coords.length) % coords.length][1] - coords[j][1];
        double angle2 = Math.atan2(dy2, dx2);

        // Dtermine l'angle entre les deux segments, en supposant que le
        // polygone est convexe (angles < pi)
        double angle = (-angle1 - angle2) / 2;
        if (Math.abs(angle1 - angle2) > Math.PI)
            angle += Math.PI;//from   ww w . ja  va  2s  .com

        // Dcale le point vers l'intrieur du polygone selon l'angle form
        // par les deux segments
        xpts[j] = (int) (Math.round(coords[j][0]) + Math.cos(angle) * reduction + offset);
        ypts[j] = (int) (Math.round(-coords[j][1]) + Math.sin(angle) * reduction + offset);
    }

    return new Polygon(xpts, ypts, coords.length);
}

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

@Override
public void fillPolygon(PolygonRenderEvent pre) throws ChartException {
    if (iv != null) {
        iv.modifyEvent(pre);/*  www .  ja  va 2  s .  c om*/
    }
    final Fill flBackground = validateMultipleFill(pre.getBackground());

    if (isFullTransparent(flBackground)) {
        return;
    }

    final Location[] loa = pre.getPoints();
    final int[][] i2a = getCoordinatesAsInts(loa);

    if (flBackground instanceof ColorDefinition) {
        final ColorDefinition cd = (ColorDefinition) flBackground;
        _g2d.setColor((Color) _ids.getColor(cd));
        _g2d.fill(getPolygon(loa));
    } else if (flBackground instanceof Gradient) {
        final Gradient g = (Gradient) flBackground;
        final ColorDefinition cdStart = g.getStartColor();
        final ColorDefinition cdEnd = g.getEndColor();
        // final boolean bRadial = g.isCyclic();
        final double dAngleInDegrees = g.getDirection();
        final double dAngleInRadians = ((-dAngleInDegrees * Math.PI) / 180.0);
        // final int iAlpha = g.getTransparency();

        final double dMinX = BaseRenderer.getX(loa, IConstants.MIN);
        final double dMaxX = BaseRenderer.getX(loa, IConstants.MAX);
        final double dMinY = BaseRenderer.getY(loa, IConstants.MIN);
        final double dMaxY = BaseRenderer.getY(loa, IConstants.MAX);

        if (dAngleInDegrees < -90 || dAngleInDegrees > 90) {
            throw new ChartException(ChartDeviceExtensionPlugin.ID, ChartException.RENDERING,
                    "SwingRendererImpl.exception.gradient.angle", //$NON-NLS-1$
                    new Object[] { new Double(dAngleInDegrees) }, Messages.getResourceBundle(getULocale()));
        }

        Point2D.Double p2dStart, p2dEnd;
        if (dAngleInDegrees == 90) {
            p2dStart = new Point2D.Double(dMinX, dMaxY);
            p2dEnd = new Point2D.Double(dMinX, dMinY);
        } else if (dAngleInDegrees == -90) {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMinX, dMaxY);
        } else if (dAngleInDegrees > 0) {
            p2dStart = new Point2D.Double(dMinX, dMaxY);
            p2dEnd = new Point2D.Double(dMaxX, dMaxY - (dMaxX - dMinX) * Math.abs(Math.tan(dAngleInRadians)));
        } else if (dAngleInDegrees < 0) {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMaxX, dMinY + (dMaxX - dMinX) * Math.abs(Math.tan(dAngleInRadians)));
        } else {
            p2dStart = new Point2D.Double(dMinX, dMinY);
            p2dEnd = new Point2D.Double(dMaxX, dMinY);
        }
        _g2d.setPaint(new GradientPaint(p2dStart, (Color) _ids.getColor(cdStart), p2dEnd,
                (Color) _ids.getColor(cdEnd)));
        _g2d.fill(getPolygon(loa));
    } else if (flBackground instanceof org.eclipse.birt.chart.model.attribute.Image) {
        Area ar2 = new Area(new Polygon(i2a[0], i2a[1], loa.length));
        if (flBackground instanceof PatternImage) {
            fillWithPatternImage(ar2, flBackground);
            return;
        }
        java.awt.Image img = createImageFromModel(flBackground);

        if (img != null) {
            final Shape shClip = _g2d.getClip();
            if (shClip != null) {
                Area ar1 = new Area(shClip);
                ar2.intersect(ar1);
            }
            _g2d.setClip(ar2);

            final double dMinX = BaseRenderer.getX(loa, IConstants.MIN);
            final double dMaxX = BaseRenderer.getX(loa, IConstants.MAX);
            final double dMinY = BaseRenderer.getY(loa, IConstants.MIN);
            final double dMaxY = BaseRenderer.getY(loa, IConstants.MAX);
            final Size szImage = _ids.getSize(img);

            final int iXRepeat = (int) (Math.ceil((dMaxX - dMinX) / szImage.getWidth()));
            final int iYRepeat = (int) (Math.ceil((dMaxY - dMinY) / szImage.getHeight()));
            final ImageObserver io = (ImageObserver) _ids.getObserver();
            for (int i = 0; i < iXRepeat; i++) {
                for (int j = 0; j < iYRepeat; j++) {
                    _g2d.drawImage(img, (int) (dMinX + i * szImage.getWidth()),
                            (int) (dMinY + j * szImage.getHeight()), io);
                }
            }

            _g2d.setClip(shClip); // RESTORE
        }
    }
}

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

static private 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 w ww.  j a  v a 2s .  c o  m

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

From source file:DefaultGraphics2D.java

/**
 * Draws a closed polygon defined by arrays of <i>x</i> and <i>y</i>
 * coordinates. Each pair of (<i>x</i>,&nbsp;<i>y</i>) coordinates defines
 * a point.//from  w  w w . j  a v  a 2s  . c o m
 * <p>
 * This method draws the polygon defined by <code>nPoint</code> line
 * segments, where the first <code>nPoint&nbsp;-&nbsp;1</code> line segments
 * are line segments from
 * <code>(xPoints[i&nbsp;-&nbsp;1],&nbsp;yPoints[i&nbsp;-&nbsp;1])</code> to
 * <code>(xPoints[i],&nbsp;yPoints[i])</code>, for 1&nbsp;&le;&nbsp;<i>i</i>&nbsp;&le;&nbsp;<code>nPoints</code>.
 * The figure is automatically closed by drawing a line connecting the final
 * point to the first point, if those points are different.
 * 
 * @param xPoints
 *          a an array of <code>x</code> coordinates.
 * @param yPoints
 *          a an array of <code>y</code> coordinates.
 * @param nPoints
 *          a the total number of points.
 * @see java.awt.Graphics#fillPolygon(int[],int[],int)
 * @see java.awt.Graphics#drawPolyline
 */
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
    Polygon polygon = new Polygon(xPoints, yPoints, nPoints);
    draw(polygon);
}

From source file:DefaultGraphics2D.java

/**
 * Fills a closed polygon defined by arrays of <i>x</i> and <i>y</i>
 * coordinates.//from  www  .  j  a  v  a  2  s .c  o  m
 * <p>
 * This method draws the polygon defined by <code>nPoint</code> line
 * segments, where the first <code>nPoint&nbsp;-&nbsp;1</code> line segments
 * are line segments from
 * <code>(xPoints[i&nbsp;-&nbsp;1],&nbsp;yPoints[i&nbsp;-&nbsp;1])</code> to
 * <code>(xPoints[i],&nbsp;yPoints[i])</code>, for 1&nbsp;&le;&nbsp;<i>i</i>&nbsp;&le;&nbsp;<code>nPoints</code>.
 * The figure is automatically closed by drawing a line connecting the final
 * point to the first point, if those points are different.
 * <p>
 * The area inside the polygon is defined using an even-odd fill rule, also
 * known as the alternating rule.
 * 
 * @param xPoints
 *          a an array of <code>x</code> coordinates.
 * @param yPoints
 *          a an array of <code>y</code> coordinates.
 * @param nPoints
 *          a the total number of points.
 * @see java.awt.Graphics#drawPolygon(int[], int[], int)
 */
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
    Polygon polygon = new Polygon(xPoints, yPoints, nPoints);
    fill(polygon);
}

From source file:org.n52.v3d.terrainserver.povraywts.WebTerrainServlet.java

private void addAnnotations(BufferedImage pImage, int pHeight, int pWidth, double pPitch, double pYaw,
        boolean pDrawNorthArrow) {
    if (mCopyrightTextContent.length() > 0) {
        Graphics2D g = pImage.createGraphics();
        g.drawImage(pImage, 0, 0, null);
        g.setColor(new java.awt.Color(mCopyrightTextColor.getRed(), mCopyrightTextColor.getGreen(),
                mCopyrightTextColor.getBlue()));

        // 1. Copyright-Vermerk
        // Etwas unschn: Durch JPEG-Komprimierung wird Text (insb. bei kleiner Font-Gre) wird unscharf...
        // TODO: Abhilfe evtl. durch Hintergrund?
        Font font = new Font(mCopyrightTextFont, Font.BOLD /* Style als int, siehe ggf. API-Dok.*/,
                mCopyrightTextSize);/*from  w ww. j a va2 s. c  o  m*/
        g.setFont(font);
        // mehrzeilige Copyright-Texte erlauben:
        StringTokenizer str = new StringTokenizer(mCopyrightTextContent, "\n");
        int spacePerRow = mCopyrightTextSize;
        int rows = str.countTokens();
        int startPos = spacePerRow * rows;
        int currRow = 0;
        while (str.hasMoreTokens()) {
            int yPos = pHeight - (startPos - (currRow * spacePerRow)) + spacePerRow / 2;
            g.drawString(str.nextToken().trim(), 5, yPos);
            currRow++;
        }

        // 2. Nordpfeil
        if (pDrawNorthArrow) {
            // Zeichenparameter:
            double radius = 35.;
            double phi = 15.;
            // Symbolkonstruktion:
            int rx = (int) radius;
            int ry = (int) Math.round(radius * Math.sin(-pPitch * Math.PI / 180.));
            int mx = pWidth - rx - 5;
            int my = pHeight - ry - 5;
            int dx = (int) (radius * Math.sin(pYaw * Math.PI / 180.));
            int dy = (int) (radius * Math.sin(-pPitch * Math.PI / 180.) * Math.cos(pYaw * Math.PI / 180.));
            int px = mx - dx, py = my - dy; // Pfeilspitze
            int qlx = mx + (int) (radius * Math.sin((pYaw + phi) * Math.PI / 180.));
            int qly = my + (int) (radius * Math.sin(-pPitch * Math.PI / 180.)
                    * Math.cos((pYaw + phi) * Math.PI / 180.));
            int qrx = mx + (int) (radius * Math.sin((pYaw - phi) * Math.PI / 180.));
            int qry = my + (int) (radius * Math.sin(-pPitch * Math.PI / 180.)
                    * Math.cos((pYaw - phi) * Math.PI / 180.));
            // Ellipse zeichnen:
            g.setStroke(new BasicStroke(2.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
            g.drawOval(mx - rx, my - ry, 2 * rx, 2 * ry);
            // Striche fr Pfeil zeichnen:

            g.setStroke(new BasicStroke(1.f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));

            boolean fillArrow = true;
            if (fillArrow)
                g.fill(new Polygon(new int[] { px, qlx, qrx }, new int[] { py, qly, qry }, 3));
            else {
                g.drawLine(px, py, qlx, qly);
                g.drawLine(px, py, qrx, qry);
                g.drawLine(qlx, qly, qrx, qry);
            }
        }

        g.dispose();
    }
}

From source file:base.BasePlayer.ClusterTable.java

void createPolygon() {
    if (sorter.index > header.size() - 1) {
        return;/* w  ww .  ja va2  s.  c om*/
    }
    if (sorter.ascending) {
        int[] x = { (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 15,
                (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 10,
                (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 5 };
        int[] y = { 12, 4, 12 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    } else {
        int[] x = { (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 15,
                (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 10,
                (int) header.get(sorter.index)[1] + (int) (header.get(sorter.index)[2]) - 5 };
        int[] y = { 4, 12, 4 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    }
}

From source file:base.BasePlayer.AminoTable.java

void createPolygon() {
    if (sorter.index > headerlengths.length - 1) {
        return;/* ww  w . j av  a2  s .co  m*/
    }
    if (sorter.ascending) {
        int[] x = { (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 15,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 10,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 5 };
        int[] y = { 12, 4, 12 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    } else {
        int[] x = { (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 15,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 10,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 5 };
        int[] y = { 4, 12, 4 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    }
}

From source file:base.BasePlayer.BedTable.java

void createPolygon() {

    if (sorter.ascending) {
        int[] x = { (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 15,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 10,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 5 };
        int[] y = { 12, 4, 12 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    } else {/*  w w w .  jav  a  2 s  . c om*/
        int[] x = { (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 15,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 10,
                (int) ((headerlengths[sorter.index][0] + headerlengths[sorter.index][1])) - 5 };
        int[] y = { 4, 12, 4 };
        int n = 3;
        this.sortTriangle = new Polygon(x, y, n);
    }
}

From source file:smlm.util.SRutil.java

public static Polygon[] getCellContours(ImagePlus rebuilt, int rebuiltScale, String rootDirName,
        boolean getNewIfNone) {

    Polygon[] cells = new Polygon[20];
    int cell = 0;

    if (new File(rootDirName + File.separator + "0_Cell1-Contour.txt").isFile()
            || new File(rootDirName.substring(0, rootDirName.lastIndexOf(File.separator)).substring(0,
                    rootDirName.substring(0, rootDirName.lastIndexOf(File.separator))
                            .substring(0, rootDirName.lastIndexOf(File.separator)).lastIndexOf(File.separator))
                    + File.separator + "0_Cell1-Contour.txt").isFile()) {
        String rootDirNameTemp;//  w  ww .j a v a  2  s  . c  om
        if (new File(rootDirName + File.separator + "0_Cell1-Contour.txt").isFile())
            rootDirNameTemp = rootDirName;
        else
            rootDirNameTemp = rootDirName.substring(0, rootDirName.lastIndexOf(File.separator)).substring(0,
                    rootDirName.substring(0, rootDirName.lastIndexOf(File.separator))
                            .substring(0, rootDirName.lastIndexOf(File.separator)).lastIndexOf(File.separator));
        while (new File(rootDirNameTemp + File.separator + "0_Cell" + (cell + 1) + "-Contour.txt").isFile()) {
            ResultsTableMt saveCell = ResultsTableMt
                    .open2(rootDirNameTemp + File.separator + "0_Cell" + (cell + 1) + "-Contour.txt");

            if (getSum(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Xi")))
                    + getSum(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Yi"))) % 1 == 0) {
                cells[cell] = new Polygon(
                        doubleToInt(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Xi"))),
                        doubleToInt(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Yi"))),
                        saveCell.getCounter());
            } else {
                int[] xPol = new int[saveCell.getCounter()];
                int[] yPol = new int[xPol.length];
                for (int pol = 0; pol < xPol.length; pol++) {
                    xPol[pol] = (int) Math.floor(saveCell.getValue("Xi", pol) + 0.5);
                    yPol[pol] = (int) Math.floor(saveCell.getValue("Yi", pol) + 0.5);
                }
                cells[cell] = new Polygon(xPol, yPol, xPol.length);
            }

            cell++;
        }
    } else if (getNewIfNone) {

        ij.gui.WaitForUserDialog waitingGUI = new ij.gui.WaitForUserDialog("Roi selector",
                "Please select a roi in the new window and press 'OK'.");
        waitingGUI.show();
        int cellNumber = 0;
        while (!waitingGUI.escPressed() && cellNumber < cells.length) {
            cells[cellNumber] = rebuilt.getRoi().getPolygon();
            cellNumber++;

            // ImageProcessor cell = Rebuilt.getRoi().getMask();
            // cell.add(Rebuilt.getStatistics().mean);
            Overlay ol = rebuilt.getOverlay();
            if (ol == null)
                ol = new Overlay();

            Roi roiTemp = rebuilt.getRoi();
            roiTemp.setFillColor(new Color(cellNumber * 10, cellNumber * 10, cellNumber * 10));
            roiTemp.setStrokeColor(new Color(cellNumber * 10, cellNumber * 10, cellNumber * 10));
            ol.add(roiTemp);

            TextRoi label1 = new TextRoi(
                    rebuilt.getRoi().getBounds().x + (int) ((double) rebuilt.getRoi().getBounds().width / 2.0),
                    rebuilt.getRoi().getBounds().y + (int) ((double) rebuilt.getRoi().getBounds().height / 2.0),
                    "" + cellNumber, new Font(Font.SANS_SERIF, Font.PLAIN, 8));
            label1.setStrokeColor(Color.BLACK);
            label1.setNonScalable(false);
            ol.add(label1);

            rebuilt.setOverlay(ol);
            rebuilt.setHideOverlay(false);
            rebuilt.updateAndDraw();
            rebuilt.show();

            // rebuilt.setOverlay(Rebuilt.getRoi(), Color.WHITE, 0,
            // Color.GRAY);
            // rebuilt.updateImage();
            rebuilt.deleteRoi();

            waitingGUI = new ij.gui.WaitForUserDialog("Roi selector",
                    "Please select a roi in the new window and press 'OK'.");
            waitingGUI.show();
        }

        for (cell = 0; cell < cells.length && cells[cell] != null; cell++) {
            Polygon polygon = cells[cell];

            ResultsTableMt saveCell = new ResultsTableMt();
            for (int i = 0; i < polygon.npoints; i++) {
                saveCell.incrementCounter();
                saveCell.addValue("Xi", (int) (((double) polygon.xpoints[i]) / ((double) rebuiltScale) + 0.5));
                saveCell.addValue("Yi", (int) (((double) polygon.ypoints[i]) / ((double) rebuiltScale) + 0.5));
            }

            try {
                saveCell.saveAs(rootDirName + File.separator + "0_Cell" + (cell + 1) + "-Contour.txt");
            } catch (IOException e) {
                e.printStackTrace();
            }

            cells[cell] = new Polygon(doubleToInt(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Xi"))),
                    doubleToInt(saveCell.getColumnAsDoubles(saveCell.getColumnIndex("Yi"))),
                    saveCell.getCounter());
        }
    }

    Polygon[] retour = new Polygon[cell];
    for (int i = 0; i < cell; i++)
        retour[i] = cells[i];

    return retour;
}