📄 basicgeometry.java
字号:
* * @param x X pixel coordinate of the point. * @param y Y pixel coordinate of the point. * @return getShape().contains(x, y), false if the OMGraphic * hasn't been generated yet. */ public boolean contains(int x, int y) { Shape shape = getShape(); boolean ret = false; if (shape != null) { ret = shape.contains((double) x, (double) y); } return ret; } /** * Invoke this to regenerate a "dirty" graphic. This method is a * wrapper around the <code>generate()</code> method. It invokes * <code>generate()</code> only if</code> needToRegenerate() * </code> on the graphic returns true. To force a graphic to be * generated, call <code>generate()</code> directly. * * @param proj the Projection * @return true if generated, false if didn't do it (maybe a * problem). * @see #generate */ public boolean regenerate(Projection proj) { if (proj == null) { return false; } if (getNeedToRegenerate()) { return generate(proj); } return false; } /** * Get the java.awt.Shape object that represents the projected * graphic. The array will one Shape object even if the object * wraps around the earth and needs to show up in more than one * place on the map. In conditions like that, the Shape will have * multiple parts. * <p> * * The java.awt.Shape object gives you the ability to do a little * spatial analysis on the graphics. * * @return java.awt.geom.GeneralPath (a java.awt.Shape object), or * null if the graphic needs to be generated with the * current map projection, or null if the OMGeometry * hasn't been updated to use Shape objects for its * internal representation. */ public GeneralPath getShape() { return shape; } /** * Set the java.awt.Shape object that represents the projected * graphic. This Shape object should be internally generated, but * this method is provided to clear out the object to save memory, * or to allow a little customization if your requirements * dictate. * <p> * * The java.awt.Shape object gives you the ability to do a little * spatial analysis on the graphics. * * @param gp java.awt.geom.GeneralPath, or null if the graphic * needs to be generated with the current map projection or * to clear out the object being held by the OMGeometry. */ public void setShape(GeneralPath gp) { shape = gp; } /** * Create a Shape object given an array of x points and y points. * The x points a y points should be projected. This method is * used by subclasses that get projected coordinates out of the * projection classes, and they need to build a Shape object from * those coordinates. * * @param xpoints projected x coordinates * @param ypoints projected y coordinates * @param isPolygon whether the points make up a polygon, or a * polyline. If it's true, the Shape object returned is a * Polygon. If false, the Shape returned is a GeneralPath * object. * @return The Shape object for the points. */ public static GeneralPath createShape(int xpoints[], int ypoints[], boolean isPolygon) { return createShape(xpoints, ypoints, 0, xpoints.length, isPolygon); } /** * Create a Shape object given an array of x points and y points. * The x points a y points should be projected. This method is * used by subclasses that get projected coordinates out of the * projection classes, and they need to build a Shape object from * those coordinates. * * @param xpoints projected x coordinates * @param ypoints projected y coordinates * @param startIndex the starting coordinate index in the array. * @param length the number of points to use from the array for * the shape. * @param isPolygon whether the points make up a polygon, or a * polyline. If it's true, the Shape object returned is a * Polygon. If false, the Shape returned is a GeneralPath * object. * @return The Shape object for the points. */ public static GeneralPath createShape(int xpoints[], int ypoints[], int startIndex, int length, boolean isPolygon) { // used to return a Shape if (xpoints == null || ypoints == null) { return null; } if (startIndex < 0) { startIndex = 0; } if (length > xpoints.length - startIndex) { // Do as much as you can... length = xpoints.length - startIndex - 1; } GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD, length); if (length > startIndex) { path.moveTo(xpoints[startIndex], ypoints[startIndex]); for (int j = startIndex + 1; j < length; j++) { path.lineTo(xpoints[j], ypoints[j]); } if (isPolygon) { path.closePath(); } } return path; } /** * Utility method that iterates over a Shape object and prints out * the points. */ public static void describeShapeDetail(Shape shape) { describeShapeDetail(shape, .25); } /** * Utility method that iterates over a Shape object and prints out * the points. The flattening is used for a * FlatteningPathIterator, controlling the scope of the path * traversal. */ public static void describeShapeDetail(Shape shape, double flattening) { PathIterator pi2 = shape.getPathIterator(null); FlatteningPathIterator pi = new FlatteningPathIterator(pi2, flattening); double[] coords = new double[6]; int pointCount = 0; Debug.output(" -- start describeShapeDetail with flattening[" + flattening + "]"); while (!pi.isDone()) { int type = pi.currentSegment(coords); Debug.output(" Shape point [" + type + "] (" + (pointCount++) + ") " + coords[0] + ", " + coords[1]); pi.next(); } Debug.output(" -- end (" + pointCount + ")"); } /** * Convenience method to add the coordinates to the given * GeneralPath. You need to close the path yourself if you want it * to be a polygon. * * @param toShape the GeneralPath Shape object to add the * coordinates to. * @param xpoints horizontal pixel coordiantes. * @param ypoints vertical pixel coordiantes. * @return toShape, with coordinates appended. */ public static GeneralPath appendShapeEdge(GeneralPath toShape, int xpoints[], int ypoints[]) { return appendShapeEdge(toShape, xpoints, ypoints, 0, xpoints.length); } /** * Convenience method to add the coordinates to the given * GeneralPath. You need to close the path yourself if you want it * to be a polygon. * * @param toShape the GeneralPath Shape object to add the * coordinates to. * @param xpoints horizontal pixel coordiantes. * @param ypoints vertical pixel coordiantes. * @param startIndex the index into pixel coordinate array to * start reading from. * @param length the number of coordinates to add. * @return toShape, with coordinates appended. */ public static GeneralPath appendShapeEdge(GeneralPath toShape, int xpoints[], int ypoints[], int startIndex, int length) { return appendShapeEdge(toShape, createShape(xpoints, ypoints, startIndex, length, false)); } /** * Convenience method to append the edge of a GeneralPath Shape to * another GeneralPath Shape. A PathIterator is used to figure out * the points to use to add to the toShape. You need to close the * path yourself if you want it to be a polygon. Assumes that the * two paths should be connected. * * @param toShape the GeneralPath Shape object to add the edge to. * @param addShape the GeneralPath Shape to add to the toShape. * @return toShape, with coordinates appended. Returns addShape if * toShape was null. */ public static GeneralPath appendShapeEdge(GeneralPath toShape, GeneralPath addShape) { return appendShapeEdge(toShape, addShape, true); } /** * Convenience method to append the edge of a GeneralPath Shape to * another GeneralPath Shape. A PathIterator is used to figure out * the points to use to add to the toShape. You need to close the * path yourself if you want it to be a polygon. * * @param toShape the GeneralPath Shape object to add the edge to. * @param addShape the GeneralPath Shape to add to the toShape. * @param lineTo specify whether the first point of the appended * path is connected to the original path. True to connect. * @return toShape, with coordinates appended. Returns addShape if * toShape was null. */ public static GeneralPath appendShapeEdge(GeneralPath toShape, GeneralPath addShape, boolean lineTo) { boolean DEBUG = Debug.debugging("arealist"); int pointCount = 0; // If both null, return null. if (addShape == null) { return toShape; } if (toShape == null) { return addShape; } PathIterator pi2 = addShape.getPathIterator(null); FlatteningPathIterator pi = new FlatteningPathIterator(pi2, .25); double[] coords = new double[6]; while (!pi.isDone()) { int type = pi.currentSegment(coords); if (lineTo) { if (DEBUG) { Debug.output(" adding point [" + type + "] (" + (pointCount++) + ") " + (float) coords[0] + ", " + (float) coords[1]); } toShape.lineTo((float) coords[0], (float) coords[1]); } else { if (DEBUG) { Debug.output("Creating new shape, first point " + (float) coords[0] + ", " + (float) coords[1]); } toShape.moveTo((float) coords[0], (float) coords[1]); lineTo = true; } pi.next(); } if (DEBUG) { Debug.output(" -- end point (" + pointCount + ")"); } return toShape; } /** * Create a general path from a point plus a height and width; */ public static GeneralPath createBoxShape(int x, int y, int width, int height) { int[] xs = new int[4]; int[] ys = new int[4]; xs[0] = x; ys[0] = y; xs[1] = x + width; ys[1] = y; xs[2] = x + width; ys[2] = y + height; xs[3] = x; ys[3] = y + height; return createShape(xs, ys, true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -