javapiccolo

How to draw a curved line in Piccolo2D?


I am trying to create a curved line in Piccolo2D with Java.

This is my code

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

The graph that I get is :

enter image description here

How do I get rid of the original line segment between the two points so that I see only the curved line?


Solution

  • This is probably too late, but anyway, you can try the following:

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

    Here is a result based on the original code sample:

    enter image description here