You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/10/05 14:25:06 UTC
svn commit: r821767 - in /incubator/pivot/trunk/wtk:
src/org/apache/pivot/wtk/media/drawing/Path.java
test/org/apache/pivot/wtk/test/drawing_test.wtkx
Author: tvolkert
Date: Mon Oct 5 12:25:06 2009
New Revision: 821767
URL: http://svn.apache.org/viewvc?rev=821767&view=rev
Log:
PIVOT-295 :: Add hit detection to Path
Modified:
incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/media/drawing/Path.java
incubator/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/drawing_test.wtkx
Modified: incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/media/drawing/Path.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/media/drawing/Path.java?rev=821767&r1=821766&r2=821767&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/media/drawing/Path.java (original)
+++ incubator/pivot/trunk/wtk/src/org/apache/pivot/wtk/media/drawing/Path.java Mon Oct 5 12:25:06 2009
@@ -16,23 +16,17 @@
*/
package org.apache.pivot.wtk.media.drawing;
-import java.awt.BasicStroke;
-import java.awt.Graphics2D;
-import java.awt.Paint;
-import java.awt.geom.GeneralPath;
-import java.awt.geom.Rectangle2D;
+import java.awt.geom.Path2D;
import org.apache.pivot.collections.ArrayList;
import org.apache.pivot.collections.Sequence;
import org.apache.pivot.util.ListenerList;
-
/**
* A shape representing a geometric path constructed from straight lines,
* quadratic curves, and and cubic (Bézier) curves.
*/
-public class Path extends Shape
- implements Sequence<Path.Operation> {
+public class Path extends Shape2D implements Sequence<Path.Operation> {
/**
* Abstract base class for path operations. See the specific subclasses for
* details.
@@ -71,10 +65,10 @@
/**
* Performs this operation on the specified general path.
*
- * @param generalPath
+ * @param path2D
* The object upon which this operation is performed.
*/
- abstract void operate(GeneralPath generalPath);
+ abstract void operate(Path2D path2D);
}
/**
@@ -110,8 +104,8 @@
* {@inheritDoc}
*/
@Override
- void operate(GeneralPath generalPath) {
- generalPath.moveTo(x, y);
+ void operate(Path2D path2D) {
+ path2D.moveTo(x, y);
}
/**
@@ -125,15 +119,7 @@
* Sets the x-coordinate to which this will operation will move.
*/
public void setX(int x) {
- if (this.x != x) {
- this.x = x;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setLocation(x, y);
}
/**
@@ -147,7 +133,16 @@
* Sets the y-coordinate to which this will operation will move.
*/
public void setY(int y) {
- if (this.y != y) {
+ setLocation(x, y);
+ }
+
+ /**
+ * Sets the location to which this will operation will move.
+ */
+ public void setLocation(int x, int y) {
+ if (x != this.x
+ || y != this.y) {
+ this.x = x;
this.y = y;
Path path = getPath();
@@ -180,32 +175,45 @@
* {@inheritDoc}
*/
@Override
- void operate(GeneralPath generalPath) {
- generalPath.lineTo(x, y);
+ void operate(Path2D path2D) {
+ path2D.lineTo(x, y);
}
+ /**
+ * Gets the x-coordinate to which this will operation will draw a line.
+ */
public int getX() {
return x;
}
+ /**
+ * Sets the x-coordinate to which this will operation will draw a line.
+ */
public void setX(int x) {
- if (this.x != x) {
- this.x = x;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setLocation(x, y);
}
+ /**
+ * Gets the y-coordinate to which this will operation will draw a line.
+ */
public int getY() {
return y;
}
+ /**
+ * Sets the y-coordinate to which this will operation will draw a line.
+ */
public void setY(int y) {
- if (this.y != y) {
+ setLocation(x, y);
+ }
+
+ /**
+ * Sets the location to which this will operation will draw a line.
+ */
+ public void setLocation(int x, int y) {
+ if (x != this.x
+ || y != this.y) {
+ this.x = x;
this.y = y;
Path path = getPath();
@@ -249,8 +257,8 @@
* {@inheritDoc}
*/
@Override
- void operate(GeneralPath generalPath) {
- generalPath.curveTo(x1, y1, x2, y2, x3, y3);
+ void operate(Path2D path2D) {
+ path2D.curveTo(x1, y1, x2, y2, x3, y3);
}
public int getX1() {
@@ -258,15 +266,7 @@
}
public void setX1(int x1) {
- if (this.x1 != x1) {
- this.x1 = x1;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2, x3, y3);
}
public int getY1() {
@@ -274,15 +274,7 @@
}
public void setY1(int y1) {
- if (this.y1 != y1) {
- this.y1 = y1;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2, x3, y3);
}
public int getX2() {
@@ -290,15 +282,7 @@
}
public void setX2(int x2) {
- if (this.x2 != x2) {
- this.x2 = x2;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2, x3, y3);
}
public int getY2() {
@@ -306,15 +290,7 @@
}
public void setY2(int y2) {
- if (this.y2 != y2) {
- this.y2 = y2;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2, x3, y3);
}
public int getX3() {
@@ -322,15 +298,7 @@
}
public void setX3(int x3) {
- if (this.x3 != x3) {
- this.x3 = x3;
-
- Path path = getPath();
- if (path != null) {
- path.pathListeners.operationUpdated(this);
- path.invalidate();
- }
- }
+ setPoints(x1, y1, x2, y2, x3, y3);
}
public int getY3() {
@@ -338,7 +306,26 @@
}
public void setY3(int y3) {
- if (this.y3 != y3) {
+ setPoints(x1, y1, x2, y2, x3, y3);
+ }
+
+ /**
+ * Sets the points by which the curve will be drawn. The curve will be
+ * drawn to {@code (x3,y3)}, using {@code (x1,y1)} and {@code (x2,y2)}
+ * as Bézier control points.
+ */
+ public void setPoints(int x1, int y1, int x2, int y2, int x3, int y3) {
+ if (x1 != this.x1
+ || y1 != this.y1
+ || x2 != this.x2
+ || y2 != this.y2
+ || x3 != this.x3
+ || y3 != this.y3) {
+ this.x1 = x1;
+ this.y1 = y1;
+ this.x2 = x2;
+ this.y2 = y2;
+ this.x3 = x3;
this.y3 = y3;
Path path = getPath();
@@ -378,8 +365,8 @@
* {@inheritDoc}
*/
@Override
- void operate(GeneralPath generalPath) {
- generalPath.quadTo(x1, y1, x2, y2);
+ void operate(Path2D path2D) {
+ path2D.quadTo(x1, y1, x2, y2);
}
public int getX1() {
@@ -387,15 +374,7 @@
}
public void setX1(int x1) {
- if (this.x1 != x1) {
- this.x1 = x1;
-
- Path path = getPath();
- if (path != null) {
- path.pathListeners.operationUpdated(this);
- path.invalidate();
- }
- }
+ setPoints(x1, y1, x2, y2);
}
public int getY1() {
@@ -403,15 +382,7 @@
}
public void setY1(int y1) {
- if (this.y1 != y1) {
- this.y1 = y1;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2);
}
public int getX2() {
@@ -419,15 +390,7 @@
}
public void setX2(int x2) {
- if (this.x2 != x2) {
- this.x2 = x2;
-
- Path path = getPath();
- if (path != null) {
- path.invalidate();
- path.pathListeners.operationUpdated(this);
- }
- }
+ setPoints(x1, y1, x2, y2);
}
public int getY2() {
@@ -435,7 +398,22 @@
}
public void setY2(int y2) {
- if (this.y2 != y2) {
+ setPoints(x1, y1, x2, y2);
+ }
+
+ /**
+ * Sets the points by which the curve will be drawn. The curve will be
+ * drawn to {@code (x2,y2)}, using {@code (x1,y1)} as a quadratic
+ * parametric control point.
+ */
+ public void setPoints(int x1, int y1, int x2, int y2) {
+ if (x1 != this.x1
+ || y1 != this.y1
+ || x2 != this.x2
+ || y2 != this.y2) {
+ this.x1 = x1;
+ this.y1 = y1;
+ this.x2 = x2;
this.y2 = y2;
Path path = getPath();
@@ -459,14 +437,14 @@
* ray from left to right does not equal the number of times that the
* path crosses the ray from right to left.
*/
- NON_ZERO(GeneralPath.WIND_NON_ZERO),
+ NON_ZERO(Path2D.WIND_NON_ZERO),
/**
* An <tt>EVEN_ODD</tt> winding rule means that enclosed regions of the
* path alternate between interior and exterior areas as traversed from
* the outside of the path towards a point inside the region.
*/
- EVEN_ODD(GeneralPath.WIND_EVEN_ODD);
+ EVEN_ODD(Path2D.WIND_EVEN_ODD);
private int constantValue;
@@ -477,8 +455,8 @@
/**
* Gets the Java2D constant value that indicates this winding rule.
*
- * @see GeneralPath#WIND_EVEN_ODD
- * @see GeneralPath#WIND_NON_ZERO
+ * @see Path2D#WIND_EVEN_ODD
+ * @see Path2D#WIND_NON_ZERO
*/
private int getConstantValue() {
return constantValue;
@@ -518,7 +496,7 @@
private WindingRule windingRule = WindingRule.NON_ZERO;
private ArrayList<Operation> operations = new ArrayList<Operation>();
- private GeneralPath generalPath = new GeneralPath();
+ private Path2D path2D = new Path2D.Double();
private PathListenerList pathListeners = new PathListenerList();
@@ -544,7 +522,7 @@
if (previousWindingRule != windingRule) {
this.windingRule = windingRule;
- generalPath.setWindingRule(windingRule.getConstantValue());
+ path2D.setWindingRule(windingRule.getConstantValue());
update();
pathListeners.windingRuleChanged(this, previousWindingRule);
@@ -558,7 +536,7 @@
/**
* {@inheritDoc}
*/
- public int add(Operation operation) {
+ public final int add(Operation operation) {
int i = getLength();
insert(operation, i);
@@ -599,7 +577,7 @@
/**
* {@inheritDoc}
*/
- public int remove(Operation operation) {
+ public final int remove(Operation operation) {
int index = indexOf(operation);
if (index != -1) {
remove(index, 1);
@@ -648,33 +626,9 @@
return operations.getLength();
}
- /**
- * {@inheritDoc}
- */
@Override
- public boolean contains(int x, int y) {
- // TODO
- return false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void draw(Graphics2D graphics) {
- Paint fill = getFill();
- if (fill != null) {
- graphics.setPaint(fill);
- graphics.fill(generalPath);
- }
-
- Paint stroke = getStroke();
- if (stroke != null) {
- int strokeThickness = getStrokeThickness();
- graphics.setPaint(stroke);
- graphics.setStroke(new BasicStroke(strokeThickness));
- graphics.draw(generalPath);
- }
+ protected java.awt.Shape getShape2D() {
+ return path2D;
}
/**
@@ -683,26 +637,18 @@
@Override
protected void validate() {
if (!isValid()) {
- // Re-create our GeneralPath
+ // Re-create our Path2D
int n = operations.getLength();
- generalPath = new GeneralPath(windingRule.getConstantValue(), n);
+ path2D = new Path2D.Double(windingRule.getConstantValue(), n);
for (int i = 0; i < n; i++) {
Operation operation = operations.get(i);
- operation.operate(generalPath);
+ operation.operate(path2D);
}
- generalPath.closePath();
+ path2D.closePath();
- // Over-estimate the bounds to keep the logic simple
- int strokeThickness = getStrokeThickness();
- double radius = (strokeThickness / Math.cos(Math.PI / 4)) / 2;
-
- Rectangle2D bounds = generalPath.getBounds2D();
- setBounds((int)Math.floor(bounds.getX() - radius),
- (int)Math.floor(bounds.getY() - radius),
- (int)Math.ceil(bounds.getWidth() + 2 * radius + 1),
- (int)Math.ceil(bounds.getHeight() + 2 * radius + 1));
+ super.validate();
}
}
Modified: incubator/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/drawing_test.wtkx
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/drawing_test.wtkx?rev=821767&r1=821766&r2=821767&view=diff
==============================================================================
Binary files - no diff available.