What is polygon in java

Java Polygon tutorial with examples

The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space.

Introduction

The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space.

This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon.

Internally, a polygon comprises of a list of (x,y) coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon.

The first and final pairs of (x,y) points are joined by a line segment that closes the polygon.

This Polygon is defined with an even-odd winding rule.

See (java.awt.geom.PathIterator#WIND_EVEN_ODD WIND_EVEN_ODD) for a definition of the even-odd winding rule.

This class’s hit-testing methods, which include the contains, intersects and inside methods, use the insideness definition described in the Shape class comments.

Example

The following code shows how to use Polygon from java.awt.

import java.awt.Graphics; import java.awt.Polygon; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JPanel < public void paint(Graphics g) < super.paintComponent(g); int radius = 40; int centerX = 50; int centerY = 100; Polygon p = new Polygon(); centerX = 150;// w ww .d e m o 2 s . c o m for (int i = 0; i < 5; i++) p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)), (int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5))); g.fillPolygon(p); > public static void main(String[] args) < JFrame frame = new JFrame(); frame.add(new Main()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(20, 20, 500, 500); frame.setVisible(true); > >
import java.awt.Container; import java.awt.Graphics; import java.awt.Polygon; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.JFrame; import javax.swing.JPanel; public class DrawPolyPanel extends JPanel < public void paintComponent(Graphics g) < super.paintComponent(g); Polygon p = new Polygon(); for (int i = 0; i < 5; i++) p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 5)), (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 5))); g.drawPolygon(p);/*w w w . d e m o 2 s . c o m */ Polygon s = new Polygon(); for (int i = 0; i < 360; i++) < double t = i / 360.0; s.addPoint((int) (150 + 50 * t * Math.cos(8 * t * Math.PI)), (int) (150 + 50 * t * Math.sin(8 * t * Math.PI))); > g.drawPolygon(s); > public static void main(String[] args) < JFrame frame = new JFrame(); frame.setTitle("DrawPoly"); frame.setSize(350, 250); frame.addWindowListener(new WindowAdapter() < public void windowClosing(WindowEvent e) < System.exit(0); > >); Container contentPane = frame.getContentPane(); contentPane.add(new DrawPolyPanel()); frame.show(); > >
import java.awt.Polygon; import java.lang.reflect.Method; public class SampleMethod < public static void main(String[] args) < Polygon p = new Polygon(); showMethods(p);// w w w .d em o 2 s . c o m > static void showMethods(Object o) < Class c = o.getClass(); Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) < String methodString = theMethods[i].getName(); System.out.println("Name: " + methodString); String returnString = theMethods[i].getReturnType().getName(); System.out.println(" Return Type: " + returnString); Class[] parameterTypes = theMethods[i].getParameterTypes(); System.out.print(" Parameter Types:"); for (int k = 0; k < parameterTypes.length; k++) < String parameterString = parameterTypes[k].getName(); System.out.print(" " + parameterString); > System.out.println(); > > >

  • Java PointerInfo getLocation()
  • Java PointerInfo getLocation() Returns the Point that represents the coordinates of the pointer on the screen.
  • Java java.awt Polygon
  • Java Polygon tutorial with examples
  • Java Polygon Polygon()
  • Java Polygon addPoint(int x, int y)
  • Java Polygon Polygon(int[] xpoints, int[] ypoints, int npoints)

demo2s.com | Email: | Demo Source and Support. All rights reserved.

Источник

Class Polygon

The Polygon class encapsulates a description of a closed, two-dimensional region within a coordinate space. This region is bounded by an arbitrary number of line segments, each of which is one side of the polygon. Internally, a polygon comprises of a list of (x,y) coordinate pairs, where each pair defines a vertex of the polygon, and two successive pairs are the endpoints of a line that is a side of the polygon. The first and final pairs of (x,y) points are joined by a line segment that closes the polygon. This Polygon is defined with an even-odd winding rule. See WIND_EVEN_ODD for a definition of the even-odd winding rule. This class’s hit-testing methods, which include the contains , intersects and inside methods, use the insideness definition described in the Shape class comments.

Field Summary

Constructor Summary

Method Summary

Tests if the specified coordinates are inside the boundary of the Shape , as described by the definition of insideness.

Tests if a specified Point2D is inside the boundary of the Shape , as described by the definition of insideness.

Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon .

Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape .

Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon .

Methods declared in class java.lang.Object

Field Details

npoints

The total number of points. The value of npoints represents the number of valid points in this Polygon and might be less than the number of elements in xpoints or ypoints . This value can be 0.

xpoints

The array of X coordinates. The number of elements in this array might be more than the number of X coordinates in this Polygon . The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon .

ypoints

The array of Y coordinates. The number of elements in this array might be more than the number of Y coordinates in this Polygon . The extra elements allow new points to be added to this Polygon without re-creating this array. The value of npoints is equal to the number of valid points in this Polygon .

bounds

Constructor Details

Polygon

Polygon

Method Details

reset

Resets this Polygon object to an empty polygon. The coordinate arrays and the data in them are left untouched but the number of points is reset to zero to mark the old vertex data as invalid and to start accumulating new vertex data at the beginning. All internally-cached data relating to the old vertices are discarded. Note that since the coordinate arrays from before the reset are reused, creating a new empty Polygon might be more memory efficient than resetting the current one if the number of vertices in the new polygon data is significantly smaller than the number of vertices in the data from before the reset.

invalidate

Invalidates or flushes any internally-cached data that depends on the vertex coordinates of this Polygon . This method should be called after any direct manipulation of the coordinates in the xpoints or ypoints arrays to avoid inconsistent results from methods such as getBounds or contains that might cache data from earlier computations relating to the vertex coordinates.

translate

addPoint

Appends the specified coordinates to this Polygon . If an operation that calculates the bounding box of this Polygon has already been performed, such as getBounds or contains , then this method updates the bounding box.

getBounds

Gets the bounding box of this Polygon . The bounding box is the smallest Rectangle whose sides are parallel to the x and y axes of the coordinate space, and can completely contain the Polygon .

getBoundingBox

contains

contains

inside

getBounds2D

Returns a high precision and more accurate bounding box of the Shape than the getBounds method. Note that there is no guarantee that the returned Rectangle2D is the smallest bounding box that encloses the Shape , only that the Shape lies entirely within the indicated Rectangle2D . The bounding box returned by this method is usually tighter than that returned by the getBounds method and never fails due to overflow problems since the return value can be an instance of the Rectangle2D that uses double precision values to store the dimensions. Note that the definition of insideness can lead to situations where points on the defining outline of the shape may not be considered contained in the returned bounds object, but only in cases where those points are also not considered contained in the original shape . If a point is inside the shape according to the contains(point) method, then it must be inside the returned Rectangle2D bounds object according to the contains(point) method of the bounds . Specifically: shape.contains(p) requires bounds.contains(p) If a point is not inside the shape , then it might still be contained in the bounds object: bounds.contains(p) does not imply shape.contains(p)

contains

Tests if the specified coordinates are inside the boundary of the Shape , as described by the definition of insideness.

contains

Tests if a specified Point2D is inside the boundary of the Shape , as described by the definition of insideness.

intersects

  • there is a high probability that the rectangular area and the Shape intersect, but
  • the calculations to accurately determine this intersection are prohibitively expensive.

intersects

  • there is a high probability that the Rectangle2D and the Shape intersect, but
  • the calculations to accurately determine this intersection are prohibitively expensive.

contains

  • the intersect method returns true and
  • the calculations to determine whether or not the Shape entirely contains the rectangular area are prohibitively expensive.

contains

  • the intersect method returns true and
  • the calculations to determine whether or not the Shape entirely contains the Rectangle2D are prohibitively expensive.

getPathIterator

Returns an iterator object that iterates along the boundary of this Polygon and provides access to the geometry of the outline of this Polygon . An optional AffineTransform can be specified so that the coordinates returned in the iteration are transformed accordingly.

getPathIterator

Returns an iterator object that iterates along the boundary of the Shape and provides access to the geometry of the outline of the Shape . Only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE point types are returned by the iterator. Since polygons are already flat, the flatness parameter is ignored. An optional AffineTransform can be specified in which case the coordinates returned in the iteration are transformed accordingly.

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Читайте также:  Убрать квадратные скобки питон
Оцените статью