📄 omgraphiclist.java
字号:
} /** * Projects any graphics needing projection. Use this method to project any * new or changed OMGeometrys before painting. to re-project the whole list, * use <code>generate(Projection, boolean)</code> with * <code>forceProjectAll</code> set to <code>true</code>. This is the * same as calling <code> generate(p, false)</code> * * @param p a <code>Projection</code> * @see #generate(Projection, boolean) */ public void project(Projection p) { generate(p, false); } /** * Projects the OMGeometrys on the list. This is the same as calling * <code>generate(p, forceProjectAll)</code>. * * @param p a <code>Projection</code> * @param forceProjectAll if true, all the graphics on the list are * generated with the new projection. If false they are only * generated if getNeedToRegenerate() returns true * @see #generate(Projection, boolean) */ public void project(Projection p, boolean forceProjectAll) { generate(p, forceProjectAll); } /** * Prepare the graphics for rendering. This is the same as calling * <code>project(p, true)</code>. * * @param p a <code>Projection</code> * @return boolean true * @see #generate(Projection, boolean) */ public boolean generate(Projection p) { generate(p, true); return true; } /** * Prepare the graphics for rendering. This must be done before calling * <code>render()</code>! This recursively calls generate() on the * OMGraphics on the list. * * @param p a <code>Projection</code> * @param forceProjectAll if true, all the graphics on the list are * generated with the new projection. If false they are only * generated if getNeedToRegenerate() returns true * @see OMGraphic#generate * @see OMGraphic#regenerate */ public synchronized void generate(Projection p, boolean forceProjectAll) { Iterator iterator = iterator(); // Check forceProjectAll outside the loop for slight // performance improvement. if (forceProjectAll) { while (iterator.hasNext()) { ((OMGraphic) iterator.next()).generate(p); } } else { while (iterator.hasNext()) { ((OMGraphic) iterator.next()).regenerate(p); } } } /** * Renders all the objects in the list a graphics context. This is the same * as <code>paint()</code> for AWT components. The graphics are rendered * in the order of traverseMode. Any graphics where <code>isVisible()</code> * returns false are not rendered. * * @param gr the AWT Graphics context */ public synchronized void render(Graphics gr) { OMGraphic graphic; ListIterator iterator; if (traverseMode == FIRST_ADDED_ON_TOP) { iterator = graphics.listIterator(graphics.size()); while (iterator.hasPrevious()) { graphic = (OMGraphic) iterator.previous(); if (shouldProcess(graphic)) { graphic.render(gr); } } } else { iterator = graphics.listIterator(); while (iterator.hasNext()) { graphic = (OMGraphic) iterator.next(); if (shouldProcess(graphic)) { graphic.render(gr); } } } } /** * Renders all the objects in the list a graphics context, in their * 'selected' mode. This is the same as <code>paint()</code> for AWT * components. The graphics are rendered in the order of traverseMode. Any * graphics where <code>isVisible()</code> returns false are not rendered. * All of the graphics on the list are returned to their deselected state. * * @param gr the AWT Graphics context */ public synchronized void renderAllAsSelected(Graphics gr) { OMGraphic graphic; ListIterator iterator; if (traverseMode == FIRST_ADDED_ON_TOP) { iterator = graphics.listIterator(graphics.size()); while (iterator.hasPrevious()) { graphic = (OMGraphic) iterator.previous(); if (shouldProcess(graphic)) { graphic.select(); graphic.render(gr); graphic.deselect(); } } } else { iterator = graphics.listIterator(); while (iterator.hasNext()) { graphic = (OMGraphic) iterator.next(); if (shouldProcess(graphic)) { graphic.select(); graphic.render(gr); graphic.deselect(); } } } } /** * Override flag for shouldProcess method. The setting will override the * OMGraphicList from using the OMGraphic's visibility settings in * determining which OMGraphics should be used in different distance, * generate and render methods. */ protected boolean processAllGeometries = false; /** * This method is called internally for those methods where skipping * invisible OMGeometries would save processing time and effort. If you * don't want visiblilty to be considered when processing * OMGeometries/OMGraphics, override this method and return true. */ protected boolean shouldProcess(OMGeometry omg) { return processAllGeometries || omg.isVisible(); } /** * Set the programmatic override for shouldProcess method to always process * geometries. */ public void setProcessAllGeometries(boolean set) { processAllGeometries = set; } /** * Get the settings for the programmatic override for shouldProcess method * to always process geometries. */ public boolean getProcessAllGeometries() { return processAllGeometries; } /** * Finds the distance to the closest OMGeometry. * * @param x x coord * @param y y coord * @return float distance * @see #findClosest(int, int, float) */ public float distance(int x, int y) { return _findClosest(x, y, Float.MAX_VALUE, false).d; } /** * RetVal for closest object/distance calculations. */ protected static class OMDist { public OMGeometry omg = null; public float d = Float.POSITIVE_INFINITY; public int index = NONE; // unknown public String toString() { return "OMDist: omg=" + (omg == null ? "null" : omg.getClass().getName()) + ", d=" + d + ", index=" + index; } } /** * Find the closest Object and its distance. The search is always conducted * from the topmost graphic to the bottommost, depending on the * traverseMode. * * @param x x coord * @param y y coord * @param limit the max distance that a graphic has to be within to be * returned, in pixels. * @param resetSelect deselect any OMGraphic touched. * @return OMDist */ protected synchronized OMDist _findClosest(int x, int y, float limit, boolean resetSelect) { OMDist omd = new OMDist(); OMDist tomd; ListIterator iterator; int i; if (size() != 0) { if (traverseMode == FIRST_ADDED_ON_TOP) { i = 0; iterator = graphics.listIterator(); while (iterator.hasNext()) { tomd = findClosestTest(omd, i++, (OMGeometry) iterator.next(), x, y, limit, resetSelect); if (tomd == null) continue; omd = tomd; // for style if (omd.d == 0) break; } } else { i = graphics.size(); iterator = graphics.listIterator(i); while (iterator.hasPrevious()) { tomd = findClosestTest(omd, i--, (OMGeometry) iterator.previous(), x, y, limit, resetSelect); if (tomd == null) continue; omd = tomd; // for style if (omd.d == 0) break; } } } if (Debug.debugging("omgraphics")) { int size = size(); if (omd.omg != null && isVague()) { omd.omg = this; Debug.output(this.getClass().getName() + "(" + size + ") detecting hit and vagueness, returning " + omd); } else if (omd.omg != null && !isVague()) { Debug.output(this.getClass().getName() + "(" + size + ") detecting hit, no vagueness, returning contained " + omd); } else { Debug.output(this.getClass().getName() + "(" + size + ") omd.omg " + (omd.omg == null ? "== null" : "!= null")); } } return omd; } /** * Test the omgraphic distance away from the x, y point, and compare it to * the current OMDist passed in. If the graphic is the new closest, return * the same OMDist object filled in with the new value. Otherwise, return * null. * * @param current the OMDist that contains the current best result of a * search. * @param index the index in the graphic list of the provied OMGeometry * @param graphic the OMGeometry to test * @param x the window horiontal pixel value. * @param y the window vertical pixel value. * @param resetSelect flag to call deselect on any OMGeometry contacted. * Used here to pass on in case the OMGeometry provided is an * OMGraphicList, and to use to decide if deselect should be called * on the provided graphic. * @return OMDist with an OMGraphic if the graphic passed in is the current * closest. OMDist.graphic could be null, OMDist.d could be * Infinity. */ protected synchronized OMDist findClosestTest(OMDist current, int index, OMGeometry graphic, int x, int y, float limit, boolean resetSelect) { if (current == null) { current = new OMDist(); } OMGraphicList omgl; float currentDistance = Float.MAX_VALUE; // cannot select a graphic which isn't visible if (!shouldProcess(graphic)) { return null; } if (graphic instanceof OMGraphicList) { omgl = (OMGraphicList) graphic; OMDist dist = omgl._findClosest(x, y, limit, resetSelect); if (dist.omg != null) { currentDistance = dist.d; graphic = dist.omg; } } else { if (resetSelect) graphic.deselect(); currentDistance = graphic.distance(x, y); } if (currentDistance < limit && currentDistance < current.d) { if (!isVague()) { current.omg = graphic; } else { current.omg = this; } current.index = index; current.d = currentDistance; } return current; } /** * Finds the object located the closest to the point, if the object distance * away is within the limit. The search is always conducted from the topmost * graphic to the bottommost, depending on the traverseMode. Any graphics * where <code>isVisible()</code> returns false are not considered. * * @param x the x coordinate on the component the graphics are displayed on. * @param y the y coordinate on the component the graphics are displayed on. * @param limit the max distance that a graphic has to be within to be * returned, in pixels. * @return OMGraphic the closest on the list within the limit, or null if * not found. */ public OMGraphic findClosest(int x, int y, float limit) { return (OMGraphic) _findClosest(x, y, limit, false).omg; } /** * Find all of the OMGraphics on this list that are located within the pixel * limit of the x, y pixel location. * * @param x the x coordinate on the component the graphics are displayed on. * @param y the y coordinate on the component the graphics are displayed on. * @param limit the max distance that a graphic has to be within to be * returned, in pixels. * @return OMGraphicList containing all of the OMGraphics within the limit. */ public OMGraphicList findAll(int x, int y, float limit) { return findAll(x, y, limit, false, null); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -