2012-10-03 119 views
1

我正嘗試在Java中使用Piccolo2D創建曲線。如何在Piccolo2D中繪製曲線?

這是我的代碼

public class MyCanvas extends PSwingCanvas{ 

private void drawLine(PPath inSocket, PPath outSocket) { 
    float startX = (float)inSocket.getX(); 
    float startY = (float)inSocket.getY()+4; 
    float endX = (float)outSocket.getX()+8; 
    float endY = (float)outSocket.getY()+4; 

    PPath line = PPath.createLine(startX, startY, endX, endY); 
    line.curveTo(endX+60,endY,startX-60,startY, startX, startY); 
    getLayer().addChild(line); 
} 


public void testGraph(){ 
    PPath in = PPath.createEllipse(100f, 100f, 8, 8); 
    PPath out = PPath.createEllipse(200f, 200f, 8, 8); 
    this.getLayer().addChild(in); 
    this.getLayer().addChild(out); 
    drawLine(out,in); 
} 

public static void main(String[] args){ 
    final JFrame frame = new JFrame("Testing"); 
    MyCanvas canvas = new MyCanvas(); 
    canvas.testGraph(); 
    frame.getContentPane().add(editor); 
    frame.setPreferredSize(new Dimension(600, 400)); 
    frame.pack(); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    SwingUtilities.invokeLater(new Runnable() { 
    @Override 
    public void run() { 
      frame.setVisible(true); 
    }}); 
    } 
} 

,我得到圖爲:

enter image description here

如何拿到兩分之間擺脫了原來的線段,這樣我只看到曲線?

回答

1

這可能是太晚了,但無論如何,你可以嘗試以下方法:

PPath line = new PPath(); 
line.moveTo(endX, endY); 
line.curveTo(endX + 60, endY, startX - 60, startY, startX, startY); 

這是在原有基礎上的代碼示例結果:

enter image description here