0
我想從路徑創建區域。 如果我將new Rectangle(10, 10, 300, 300)
傳遞給區域構造函數,則一切正常。如果我通過一條路徑,那麼該地區似乎是空的。從路徑創建區域
我該如何解決這個問題?
import java.awt.*;
import java.awt.geom.*;
PGraphicsJava2D pg;
Graphics2D g2d;
Area area;
float ang = 0;
void setup() {
size(400, 400, JAVA2D);
pg = (PGraphicsJava2D)g;
g2d = pg.g2;
background(255);
// left top width height
//area = new Area(new Rectangle(10, 10, 300, 300));
// area.subtract(new Area(new Rectangle(0, 0, 50, 50)));
// area.subtract(new Area(new Rectangle(250, 0, 50, 50)));
float[][] path = new float[][] {{0,0},{200,0},{200,200},{0,200},{0,0}};
Path2D.Float p = arrayToPath(path, true);
println(p == null ? "null" : "not null"); // not null
// why does this not appear?
area = new Area(p);
//area = new Area(new Rectangle(10, 10, 300, 300));
noLoop();
}
void draw() {
background(255);
//translate(width/2, height/2);
//rotate(ang);
ang += 0.01f;
g2d.setColor(Color.cyan);
g2d.fill(area);
g2d.setColor(Color.black);
g2d.draw(area);
PathIterator pi = area.getPathIterator(null);
while (!pi.isDone()) {
describeCurrentSegment(pi);
pi.next();
}
}
public void describeCurrentSegment(PathIterator pi) {
double[] coordinates = new double[6];
int type = pi.currentSegment(coordinates);
switch (type) {
case PathIterator.SEG_MOVETO:
System.out.println("move to " + coordinates[0] + ", " + coordinates[1]);
break;
case PathIterator.SEG_LINETO:
System.out.println("line to " + coordinates[0] + ", " + coordinates[1]);
break;
case PathIterator.SEG_QUADTO:
System.out.println("quadratic to " + coordinates[0] + ", " + coordinates[1] + ", "
+ coordinates[2] + ", " + coordinates[3]);
break;
case PathIterator.SEG_CUBICTO:
System.out.println("cubic to " + coordinates[0] + ", " + coordinates[1] + ", "
+ coordinates[2] + ", " + coordinates[3] + ", " + coordinates[4] + ", " + coordinates[5]);
break;
case PathIterator.SEG_CLOSE:
System.out.println("close");
break;
default:
break;
}
}
Path2D.Float arrayToPath(float[][] input, boolean includeLastOne) {
Path2D.Float path = new Path2D.Float();
path.moveTo(input[0][0], input[0][0]);
int length = includeLastOne ? input.length : input.length-1;
for (int i = 1; i < length; i++) {
path.lineTo(input[i][0], input[i][0]);
}
path.closePath();
return path;
}
交叉點:http://forum.processing.org/two/discussion/9366/path-not-showing – 2015-02-10 16:58:21