📄 omgraphic.java
字号:
* width of one. * @return the pixel distance to the true display edge of the * graphic. */ public float normalizeDistanceForLineWidth(float distance) { float lineWidth = 1; if (stroke instanceof BasicStroke) { lineWidth = ((BasicStroke) stroke).getLineWidth(); } if (lineWidth > 1) { // extra calculation for lineWidth distance -= lineWidth / 2; if (distance < 0f) { distance = 0f; } } return distance; } // //////////////////////////////////////////////////////////////////////// /** * Prepare the graphic for rendering. This must be done before * calling <code>render()</code>! If a vector graphic has * lat-lon components, then we project these vertices into x-y * space. For raster graphics we prepare in a different fashion. * <p> * If the generate is unsuccessful, it's usually because of some * oversight, (for instance if <code>proj</code> is null), and * if debugging is enabled, a message may be output to the * controlling terminal. * <p> * * @param proj Projection * @return boolean true if successful, false if not. * @see #regenerate */ public abstract boolean generate(Projection proj); /** * Paint the graphic. This paints the graphic into the Graphics * context. This is similar to <code>paint()</code> function of * java.awt.Components. Note that if the graphic has not been * generated, it will not be rendered. * <P> * * This method used to be abstract, but with the conversion of * OMGraphics to internally represent themselves as java.awt.Shape * objects, it's a more generic method. If the OMGraphic hasn't * been updated to use Shape objects, it should have its own * render method. * * @param g Graphics2D context to render into. */ public void render(Graphics g) { if (matted) { if (g instanceof Graphics2D && stroke instanceof BasicStroke) { ((Graphics2D) g).setStroke(new BasicStroke(((BasicStroke) stroke).getLineWidth() + 2f)); setGraphicsColor(g, mattingPaint); draw(g); } } if (shouldRenderFill()) { setGraphicsForFill(g); fill(g); if (textureMask != null && textureMask != fillPaint) { setGraphicsColor(g, textureMask); fill(g); } } if (shouldRenderEdge()) { setGraphicsForEdge(g); draw(g); } renderLabel(g); } /** * Calls super.setShape(), but also checks the attributes for a * label and moves the label accordingly. The label will be placed * in the center of the bounding box around the path. */ public void setShape(GeneralPath gp) { super.setShape(gp); hasLabel = false; // Go ahead and set the label location if the shape exists. if (gp != null) { OMLabeler labeler = (OMLabeler) getAttribute(LABEL); if (labeler != null) { labeler.setLocation(gp); hasLabel = true; } } } protected void setHasLabel(boolean val) { hasLabel = val; } /** * Quick check of the flag to see if a label attribute has been * set. Labels are stored in the attribute table, and that table * should only be checked in a generate() method call, and not in * the render(). The setShape() and initLabelingDuringGenerate() * method calls set this flag which is used to opt-out of labeling * methods for better performance. * * @return true if OMGraphic has label set. */ public boolean getHasLabel() { return hasLabel; } /** * The method only needs to be called in an OMGraphic's generate * method if the setShape() method isn't called there. The * appropriate setLabelLocation method for where the label should * be set should be called if this method is going to be used. */ protected void initLabelingDuringGenerate() { setHasLabel(getAttribute(LABEL) != null); } /** * Sets the label location at the center of the polygon points. If * the hasLabel variable hasn't been set, it no-ops. * * @param xpoints * @param ypoints */ public void setLabelLocation(int[] xpoints, int[] ypoints) { if (hasLabel) { OMLabeler oml = (OMLabeler) getAttribute(LABEL); if (oml != null) { oml.setLocation(xpoints, ypoints); } } } /** * Sets the label location at the given point. If the hasLabel * variable hasn't been set, it no-ops. * * @param p */ public void setLabelLocation(Point p) { if (hasLabel) { OMLabeler oml = (OMLabeler) getAttribute(LABEL); if (oml != null) { oml.setLocation(p); } } } /** * Sets the label location at the center of the bounding box of * the path. If the hasLabel variable hasn't been set, it no-ops. * * @param gp */ public void setLabelLocation(GeneralPath gp) { if (hasLabel) { OMLabeler oml = (OMLabeler) getAttribute(LABEL); if (oml != null) { oml.setLocation(gp); } } } /** * Checks to see if a label should be painted based on what * methods were called in generate(), and renders the label if * necessary. If the label wasn't set up, a quick no-op occurs. * * @param g */ public void renderLabel(Graphics g) { if (hasLabel) { OMLabeler labeler = (OMLabeler) getAttribute(LABEL); if (labeler != null) { labeler.render(g); } } } /** * Return true of the fill color/paint should be rendered (not * clear). */ public boolean shouldRenderFill() { return !isClear(getFillPaint()); } /** * Return true if the edge color/paint should be rendered (not * clear, or doesn't match the fill color). */ public boolean shouldRenderEdge() { // OK, so isClear on the displayPaitn could be inaccurate if // another thread changes the display paint on the graphic // before it actually gets rendered. return !isClear(getDisplayPaint()) || !edgeMatchesFill; } /** * Return the shortest distance from the graphic to an XY-point. * <p> * * This method used to be abstract, but with the conversion of * OMGraphics to internally represent themselves as java.awt.Shape * objects, it's a more generic method. If the OMGraphic hasn't * been updated to use Shape objects, it should have its own * distance method. * * @param x X coordinate of the point. * @param y Y coordinate of the point. * @return float distance, in pixels, from graphic to the point. * Returns Float.POSITIVE_INFINITY if the graphic isn't * ready (ungenerated). */ public float distance(int x, int y) { float distance = Float.POSITIVE_INFINITY; if (shouldRenderFill()) { distance = super.distance(x, y); } else { distance = super.distanceToEdge(x, y); } if (distance != Float.POSITIVE_INFINITY) { distance = normalizeDistanceForLineWidth(distance); } if (hasLabel) { OMLabeler labeler = (OMLabeler) getAttribute(LABEL); if (labeler != null) { float lDistance = labeler.distance(x, y); if (lDistance < distance) { distance = lDistance; } } } return distance; } /** * 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) { boolean ret = false; if (proj != null) { ret = super.regenerate(proj); // handle extra case: OMRasterObject.getNeedToReposition() if (!ret && this instanceof OMRasterObject) { ret = generate(proj); } } return ret; } /** * Used by the GraphicAttributes object to provide a choice on * whether the line type choice can be changed. */ protected boolean hasLineTypeChoice() { return true; } /** * Generic return of SinkGraphic for subclasses that don't * implement clone properly for some reason. */ public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException e) { return SinkGraphic.getSharedInstance(); } } /** * Write this object to a stream. */ private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); // Now write the Stroke. Take into account the stroke member // could be null. boolean writeStroke = (stroke != OMGraphic.BASIC_STROKE) && stroke != null; if (writeStroke) { // First write a flag indicating if a Stroke is on the // stream. oos.writeBoolean(true); if (stroke instanceof BasicStroke) { BasicStroke s = (BasicStroke) stroke; // Then write flag indicating stroke is a BasicStroke oos.writeBoolean(true); // Write the Stroke data if a Stroke is on this // object. if (s != null) { oos.writeFloat(s.getLineWidth()); oos.writeInt(s.getEndCap()); oos.writeInt(s.getLineJoin()); oos.writeFloat(s.getMiterLimit()); oos.writeObject(s.getDashArray()); oos.writeFloat(s.getDashPhase()); } } else if (stroke instanceof Serializable) { oos.writeBoolean(false); oos.writeObject((Serializable) stroke); } } else { oos.writeBoolean(false); } } /** * Read this object from a stream. */ private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); // Read the Stroke // Get the flag indicating a stroke was streamed boolean streamHasStroke = ois.readBoolean(); // Read and create the stroke if (streamHasStroke) { boolean isBasicStroke = ois.readBoolean(); if (isBasicStroke) { float linewidth = ois.readFloat(); int endcap = ois.readInt(); int linejoin = ois.readInt(); float miterlimit = ois.readFloat(); float dasharray[] = (float[]) ois.readObject(); float dashphase = ois.readFloat(); stroke = new BasicStroke(linewidth, endcap, linejoin, miterlimit, dasharray, dashphase); } else { stroke = (Stroke) ois.readObject(); } } else { stroke = OMGraphic.BASIC_STROKE; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -