📄 gsegment.java
字号:
/** * Set polyline world coordinate geometry. Ignore Z coordinate * (interpret all as 0.0). * * @param x X coordinates. * @param y Y coordinates. */ public void setGeometry (double[] x, double[] y) { setGeometry (x, y, null); } /** * Set polyline world coordinate geometry. * * @param xyz Polyline geometry [x,y,z,x,y,z,...]. */ public void setGeometry (double[] xyz) { GTransformer transformer = owner_.getScene().getTransformer(); int nPoints = xyz.length / 3; int[] devxy = new int[nPoints * 2]; double[] world = new double[3]; int[] device = new int[2]; int wIndex = 0; int dIndex = 0; for (int i = 0; i < nPoints; i++) { world[0] = xyz[wIndex + 0]; world[1] = xyz[wIndex + 1]; world[2] = xyz[wIndex + 2]; device = transformer.worldToDevice (world); devxy[dIndex + 0] = device[0]; devxy[dIndex + 1] = device[1]; wIndex += 3; dIndex += 2; } setGeometry (devxy); } /** * Set polyline world coordinate geometry. Ignore Z coordinate * (set to 0.0). * * @param xy Polyline geometry [x,y,x,y,...]. */ public void setGeometryXy (double[] xy) { GTransformer transformer = owner_.getScene().getTransformer(); int nPoints = xy.length / 2; int[] devxy = new int[nPoints * 2]; double[] world = new double[3]; int[] device = new int[2]; int wIndex = 0; int dIndex = 0; for (int i = 0; i < nPoints; i++) { world[0] = xy[wIndex + 0]; world[1] = xy[wIndex + 1]; world[2] = 0.0; device = transformer.worldToDevice (world); devxy[dIndex + 0] = device[0]; devxy[dIndex + 1] = device[1]; wIndex += 2; dIndex += 2; } setGeometry (devxy); } /** * Translate this segment in device. * * @param dx Translation in x direction. * @param dy Translation in Y direction. */ public void translate (int dx, int dy) { if (x_ == null) return; int[] newX = new int[x_.length]; int[] newY = new int[x_.length]; for (int i = 0; i < x_.length; i++) { newX[i] = x_[i] + dx; newY[i] = y_[i] + dy; } setGeometry (newX, newY); } /** * Set new style for this segment. Style elements not explicitly * set within this GStyle object are inherited from parent * objects. Default style is null, i.e. all style elements are * inherited from parent. * * @param style Style for this segment (or null if the intent is to * unset the current style). */ public void setStyle (GStyle style) { if (style_ != null) style_.removeListener (this); style_ = style; if (style_ != null) style_.addListener (this); updateStyle(); } /** * Return style for this segment. This is the style set by setStyle() * and not necesserily the style as it appears on screen as unset * style elements are inherited from parents. * * @return Style of this GSegment as specified with setStyle(), (or * null if no style has been provided). */ public GStyle getStyle() { return style_; } /** * These are the actual style used for this GSegment when * inheritance for unset values are resolved. * TODO: Make this public? * * @return Actual style for this segment. */ GStyle getActualStyle() { return actualStyle_; } /** * Resolve unset values in segment style. */ void updateStyle() { // Invalidate all style actualStyle_ = new GStyle(); // Update with owner style if (owner_ != null) actualStyle_.update (owner_.getActualStyle()); // Update (and possibly override) with present style if (style_ != null) actualStyle_.update (style_); // Update children object style if (texts_ != null) { for (Iterator i = texts_.iterator(); i.hasNext(); ) { GText text = (GText) i.next(); text.updateStyle(); } } // TODO: This might not be necessary for all style changes computeRectangle(); updateContext(); } /** * Find region of a set of positionals. * * @param positionals Positionals to find region of. * @return Region of specified positionals. */ private Region findRegion (Collection positionals) { Region region = new Region(); for (Iterator i = positionals.iterator(); i.hasNext(); ) { GPositional positional = (GPositional) i.next(); if (positional.isVisible()) region.union (positional.getRectangle()); } return region; } /** * Add a text element to this segment. * <p> * Text elements without line position hint will be associated with * the n'th segment coordinate according to the number of texts added. * * @param text Text element to add. */ public void addText (GText text) { // Create if first text if (texts_ == null) texts_ = new ArrayList(); // Add to list texts_.add (text); text.setSegment (this); // Flag owner region as invalid and annotation too if (owner_ != null) { owner_.flagRegionValid (false); if (owner_.getScene() != null) owner_.getScene().setAnnotationValid (false); } } /** * Set text element of this segment. Replaces all current * text elements of this segment. * * @param text Text element to set. */ public void setText (GText text) { removeText(); addText (text); } /** * Return all text elements of this segment. * * @return All text elements of this segment (or null if none). */ public List getTexts() { return texts_; } /** * Return the first text element of this segment. Convenient when * caller knows that there are exactly one text element. * * @return First text elements of this segment (or null if none). */ public GText getText() { return texts_ != null ? (GText) texts_.iterator().next() : null; } /** * Remove all text elements set on this segment. */ public void removeText() { // Update damage area if (owner_ != null && owner_.getWindow() != null && texts_ != null) { Region damage = findRegion (texts_); owner_.getWindow().updateDamageArea (damage); } // Nullify texts texts_ = null; } /** * Add an image to this segment. * * @param image Image to add. */ public void addImage (GImage image) { // Create if first time if (images_ == null) images_ = new ArrayList(); // Add to list images_.add (image); image.setSegment (this); // Flag owner region as invalid if (owner_ != null) owner_.flagRegionValid (false); } /** * Set image of this segment. All current images are removed. * * @param image Image to set. */ public void setImage (GImage image) { removeImages(); addImage (image); } /** * Return all images associated with this segment. * * @return All images associated with this segment. */ public Collection getImages() { return images_; } /** * Remove all images from this GSegment. */ public void removeImages() { // Update damage area if (owner_ != null && owner_.getWindow() != null && images_ != null) { Region damage = findRegion (images_); owner_.getWindow().updateDamageArea (damage); } // Nullify images images_ = null; } /** * Set image to associate with every vertex of this GSegment. * * @param image Image to decorate every vertex of this * polyline (or null to turn off this feature). */ public void setVertexImage (GImage image) { vertexImage_ = image; } /** * Return the image that is to be associated with all vertices * of this GSegment. Return null if none is specified. * * @return Image that decorates every vertex of this * GSegment (or null if not specified). */ public GImage getVertexImage() { return vertexImage_; } /** * Add a AWT component to this segment. * * @param component Component to add. */ public void addComponent (GComponent component) { // Create if first time if (components_ == null) components_ = new ArrayList(); component.setSegment (this); // Add to list components_.add (component); } /** * Set component of this segment. All current components are removed. * * @param component Component to set. */ public void setComponent (GComponent component) { removeComponents(); addComponent (component); } /** * Return all AWT components of this segment. * * @return All components of this segment. */ public Collection getComponents() { return components_; } /** * Remove all AWT components from this GSegment. * * @param component */ public void removeComponents() { // Update damage area if (owner_ != null && owner_.getWindow() != null && components_ != null) { Region damage = findRegion (components_); owner_.getWindow().updateDamageArea (damage); } // Nullify images components_ = null; } /** * Check if this segment is visible. This is visibility due to * geometry position relative to viewport, <em>not</em> due to * GObject visibility settings. * * @return True if segment geometry is visible, false otherwise. */ boolean isVisible() { return isVisible_; } /** * Check if this segment is filled. The <em>fill</em> property depends * on the style settings of the segment and it is used to determine * segment intersections. * * @return True of the segment is filled, false otherwise. */ boolean isFilled() { return actualStyle_.isDefiningFill(); } /** * Check if the geometry of this GSegment is inside the specified * rectangle. * * @param x0 X coordinate of upper left corner of rectangle. * @param y0 Y coordinate of upper left corner of rectangle. * @param x1 X coordinate of lower right corner of rectangle. * @param y1 Y coordinate of lower right corner of rectangle. * @return True if the geometry of this GSegment is completely * inside the specified rectangle, false otherwise. If * this GSegment has no geometry, false is returned. */ boolean isInsideRectangle (int x0, int y0, int x1, int y1) { if (rectangle_ == null) return false; Box box = new Box (rectangle_); return box.isInsideOf (new Box (x0, y0, x1, y1)); } /** * Check if the geometry of this GSegment intersects the specified * rectangle. * * @param x0 X coordinate of upper left corner of rectangle. * @param y0 Y coordinate of upper left corner of rectangle. * @param x1 X coordinate of lower right corner of rectangle. * @param y1 Y coordinate of lower right corner of rectangle. * @return True if the geometry of this GSegment intersects * the specified rectangle, false otherwise. If * the GSegment has no geometry, false is returned. */ boolean isIntersectingRectangle (int x0, int y0, int x1, int y1) { if (x_ == null) return false; return (isFilled() && Geometry.isPolygonIntersectingRectangle (x_, y_, x0, y0, x1, y1)) || (!isFilled() && Geometry.isPolylineIntersectingRectangle (x_, y_, x0, y0, x1, y1)); } /** * Check if this GSegment intersects the specified point. * * @param x X coordinate of point. * @param y Y coordinate of point. * @return True if this GSegment intersects the specified point, * false otherwise. If the GSegment has no geometry, * false is returned. */ boolean isIntersectingPoint (int x, int y) { if (x_ == null) return false; return (isFilled() && Geometry.isPointInsidePolygon (x_, y_, x, y)) || (!isFilled() && Geometry.isPolylineIntersectingRectangle (x_, y_, x-1, y-1, x+1, y+1)); } /** * Called when the style of this object is changed. * * @param style Style that has changed. */ public void styleChanged (GStyle style) { updateStyle(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -