2015-02-10 129 views
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; 

    } 
+0

交叉點:http://forum.processing.org/two/discussion/9366/path-not-showing – 2015-02-10 16:58:21

回答

0

此代碼的工作對我來說:

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 = new Path2D.Float(); 
p.moveTo(0, 0); 
p.lineTo(100, 0); 
p.lineTo(100, 100); 
p.lineTo(0, 100); 
p.lineTo(0, 0); 


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; 
    } 
} 

所以這是你的arrayToPath()函數的一個問題。仔細看看你要添加到路徑中:你爲什麼開始(輸入[0] [0],輸入[0] [0])?那會是什麼價值?爲什麼你lineTo(輸入[我] [0],輸入[我] [0])?考慮這些索引位於數組中的什麼位置,以及它們是什麼值。

+0

它必須是'[0] [0]'和[[0] [1]'thx :d – clankill3r 2015-02-10 17:49:21