📄 gobject.java
字号:
public void toFront() { if (parent_ != null) parent_.reposition (this, parent_.front()); } /** * Move this object to the behind its siblings in its parent * object. If doesn't have a parent, this method has no effect. * This is a convenience shorthand of * getParent().reposition (this, getParent().back()); */ public void toBack() { if (parent_ != null) parent_.reposition (this, parent_.back()); } /** * Reposition specified child object. * Use as: * * <ul> * <li>reposition (inFronOf (otherObject)); * <li>reposition (behind (otherObject)); * <li>reposition (front()); * <li>reposition (back()); * <li>reposition (4); * </ul> * * @param child Child object to be repositioned. * @param position New position of child. */ public void reposition (GObject child, int position) { if (position == FORWARD) position = getPositionOfChild (child) + 1; else if (position == BACKWARD) position = getPositionOfChild (child) - 1; children_.remove (child); add (child, position); updateDamage(); } /** * Return the position code for the position behind the specified * child node. * @see #reposition(GObject,int) * * @param child Child node. * @return Position for "behind" child. */ public int behind (GObject child) { return getPositionOfChild (child); } /** * Return the position code for the position in front of the specified * child node. * @see #reposition(GObject,int) * * @param child Child node. * @return Position for "in front of" child. */ public int inFrontOf (GObject child) { return getPositionOfChild (child) + 1; } /** * Return the position code for front. * @see #reposition(GObject,int) * * @return Position code for "front". */ public int front() { return getNChildren(); } /** * Return the position code for back. * @see #reposition(GObject,int) * * @return Position code for "back". */ public int back() { return 0; } /** * Return position code for "forward". * @see #reposition(GObject,int) * * @return Position code for "forward". */ public int forward() { return FORWARD; } /** * Return position code for "backward". * @see #reposition(GObject,int) * * @return Position code for "backward". */ public int backward() { return BACKWARD; } /** * Return the position of the specified child among the children * of this GObject. * * @param child Child to find index of. * @return Index of child (or -1 if no such child). * 0 indicates back, and so on. */ private int getPositionOfChild (GObject child) { return children_.indexOf (child); } /** * Return all children objects of this GObject. * * @return All children objects of this GObject. */ public List getChildren() { return children_; } /** * Return number of children of this graphic object. * * @return Number of children of this GObject. */ public int getNChildren() { return children_.size(); } /** * Find a child object with specified name. Search entire sub tree * from this node, depth first. * * @param name Name of object to find. * @return Requested object (or null if not found). */ public GObject find (String name) { if ((name_ == null && name == null) || (name_ != null && name_.equals (name))) return this; for (Iterator i = children_.iterator(); i.hasNext(); ) { GObject child = (GObject) i.next(); GObject foundObject = child.find (name); if (foundObject != null) return foundObject; } return null; } /** * Return all segments that intersects the specified rectangle. * Serach this node, and all sub nodes. * * @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 List of segments intersecting the rectangle. * If none do, an empty list is returned. */ public List findSegments (int x0, int y0, int x1, int y1) { List segments = new ArrayList(); findSegments (x0, y0, x1, y1, segments); return segments; } /** * Return all segments that are completely inside of the specified * rectangle. Serach this node, and all sub nodes. * * @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 List of segments intersecting the rectangle. * If none do, an empty list is returned. */ public List findSegmentsInside (int x0, int y0, int x1, int y1) { List segments = new ArrayList(); findSegmentsInside (x0, y0, x1, y1, segments); return segments; } /** * Return the first segment found that intersects the specified rectangle * Search this node, and all sub nodes. * * @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 A segment intersecting the rectangle (or null * if none do). */ public GSegment findSegment (int x0, int y0, int x1, int y1) { // Tailormake to stop after first found List segments = findSegments (x0, y0, x1, y1); return segments.size() == 0 ? null : (GSegment) segments.get (segments.size() - 1); } /** * Return the first segment found that are completely inside * the specified rectangle. Search this node, and all sub nodes. * * @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 A segment intersecting the rectangle (or null * if none do). */ public GSegment findSegmentInside (int x0, int y0, int x1, int y1) { // TODO: Stop after first found List segments = findSegmentsInside (x0, y0, x1, y1); return segments.size() == 0 ? null : (GSegment) segments.get (segments.size() - 1); } /** * Return the first segment found that intersects the specified point. * Search this node, and all sub nodes. * * @param x X coordinate of point to check. * @param y Y coordinate of point to check. * @return Front-most segment intersecting the point (or null if none do). */ public GSegment findSegment (int x, int y) { List segments = findSegments (x, y); return segments.size() == 0 ? null : (GSegment) segments.get (segments.size() - 1); } /** * Return all segments that intersects with the specified point. * Search this node, and all sub nodes. * * @param x, y Point to check. * @return All segments intersecting the point. If none do, an * empty list is returned. */ public List findSegments (int x, int y) { List segments = new ArrayList(); findSegments (x, y, segments); return segments; } /** * Find all segments of the subtree rooted at this GObject that * 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. * @param segments List to add segments to. */ private void findSegments (int x0, int y0, int x1, int y1, List segments) { // Don't scan any furher if the region doesn't intersect if (!region_.isIntersecting (new Rect (x0, y0, x1-x0+1, y1-y0+1))) return; // The region intersects, but we need to consider each segment if (segments_ != null) { for (Iterator i = segments_.iterator(); i.hasNext(); ) { GSegment segment = (GSegment) i.next(); if (segment.isIntersectingRectangle (x0, y0, x1, y1)) segments.add (segment); } } // Check subobjects for (Iterator i = children_.iterator(); i.hasNext(); ) { GObject child = (GObject) i.next(); child.findSegments (x0, y0, x1, y1, segments); } } /** * Find all segments of the subtree rooted at this GObject that are * intersects the specified point. * * @param x X coordinate of point to check. * @param y Y coordinate of point to check. * @param segments List to add segments to. */ private void findSegments (int x, int y, List segments) { // Don't scan any furher if the region doesn't intersect if (!region_.isInside (x, y)) return; // The region intersects, but we need to consider each segment if (segments_ != null) { for (Iterator i = segments_.iterator(); i.hasNext(); ) { GSegment segment = (GSegment) i.next(); if (segment.isIntersectingPoint (x, y)) segments.add (segment); } } // Check subobjects for (Iterator i = children_.iterator(); i.hasNext(); ) { GObject child = (GObject) i.next(); child.findSegments (x, y, segments); } } /** * Find all segments of the subtree rooted at this GObject that are * 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. * @param segments List to add segments to. */ private void findSegmentsInside (int x0, int y0, int x1, int y1, List segments) { // Don't scan any furher if the region doesn't intersect if (!region_.isIntersecting (new Rect (x0, y0, x1-x0+1, y1-y0+1))) return; // The region intersects, but we need to consider each segment if (segments_ != null) { for (Iterator i = segments_.iterator(); i.hasNext(); ) { GSegment segment = (GSegment) i.next(); if (segment.isInsideRectangle (x0, y0, x1, y1)) segments.add (segment); } } // Check subobjects for (Iterator i = children_.iterator(); i.hasNext(); ) { GObject child = (GObject) i.next(); child.findSegmentsInside (x0, y0, x1, y1, segments); } } /** * Return front-most object intersecting the specfied 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 Front-most object intersecting the specified rectangle * (or null if none do). */ public GObject find (int x0, int y0, int x1, int y1) { // TODO: As it needs the first only, this can be optimized List objects = findAll (x0, y0, x1, y1); return objects.size() == 0 ? null : (GObject) objects.get (objects.size() - 1); } /** * Find front-most object that are 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 Front most child object that are fully inside the * specified rectangle (or null if none are). */ public GObject findInside (int x0, int y0, int x1, int y1) { // TODO: As it needs the first only, this can be optimized List objects = findAllInside (x0, y0, x1, y1); return objects.size() == 0 ? null : (GObject) objects.get (objects.size() - 1); } /** * Return front-most object intersecting the specfied point. * * @param x X coordinate of point to check. * @param y Y coordinate of point to check. * @return Front-most object intersecting the point * (or null if none do). */ public GObject find (int x, int y) { return find (x - 1, y - 1, x + 1, y + 1); } /** * Find all objects intersecting a specified rectangle. Objects are * returned with front most object on screen last in list. Seacrh is * done in the subtree of this object (including this). If no objects * intersects, an empty list is returned. * * @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 All objects intersecting the specified rectangle. */ public List findAll (int x0, int y0, int x1, int y1) { List objects = new ArrayList(); findAll (x0, y0, x1, y1, objects); return objects; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -