Adding a point to polygon

前端 未结 2 906
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-04 02:27

I\'ve created a class that extends the awt.Polygon class. I\'m trying to write a method that given the PathIterator of the polygon and a Point representing a vertex, adds th

2条回答
  •  一个人的身影
    2020-12-04 03:22

    Try using the "addPoint(x, y)" method, except write your own version (or override it) such that it lets you specify where the point is inserted in the series of points (e.g. first, second, third, etc.).

    So, write a class that inherits from java.awt.Polygon public class InsertablePolygon extends java.awt.Polygon, and define a method on it, something like, public void insertPoint(int index, Point additionalPoint).

    Inside the additionalPoint method, you should have direct access to the int[] xpoints and int[] ypoints arrays, which store the information. Simply modify those arrays (or copy them, insert your point, and then replace them), and you should be good.

提交回复
热议问题