Draw a Line From any Point A To any Point B Dynamically Using AS3

I am trying to make an anmation where there are several locations(say A,B,C,D,......) made on a  map. If a person clicks on one location A and then another location B, I wanted to draw a dotted line from point A (from  destination of the plane) to point B(to destination of the plane)  using AS3. Please help me........

Here is a quick and dirty timeline code that does it (class is below):
var line:Shape;
var lineGraphics:Graphics;
var clickSwitch:int = 0;
var mousePositions:Array = [];
var conversion:Number = 180 / Math.PI;
init();
function init():void {
     drawBackground();
     line = new Shape();
     addChild(line);
     lineGraphics = line.graphics;
     stage.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
     clickSwitch = 1 - clickSwitch;
     mousePositions[clickSwitch] = new Point(mouseX, mouseY);
     if (clickSwitch == 0) {
          drawLine();
     else {
          removeLine();
function removeLine():void
     lineGraphics.clear();
     line.rotation = 0;
function drawLine():void
     lineGraphics.lineStyle(1, 0xff0000);
     lineGraphics.moveTo(0, 0);
     var dotDistance:Number = 2;
     var nextX:Number = dotDistance;
     while (line.width < Point.distance(mousePositions[0], mousePositions[1])) {
          lineGraphics.lineTo(nextX, 0);
          nextX += dotDistance;
          lineGraphics.moveTo(nextX, 0);
          nextX += dotDistance;
     line.rotation =  conversion * Math.atan2(mousePositions[0].y - mousePositions[1].y, mousePositions[0].x - mousePositions[1].x);
     line.x = mousePositions[1].x;
     line.y = mousePositions[1].y;
function drawBackground():void
     var s:Shape = new Shape();
     var g:Graphics = s.graphics;
     g.beginFill(0xC0C0C0);
     g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
     g.endFill();
     addChild(s);
Class:
package
     import flash.display.Graphics;
     import flash.display.Shape;
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     import flash.geom.Point;
     public class DottedLine extends Sprite
          private var line:Shape;
          private var lineGraphics:Graphics;
          private var clickSwitch:int = 0;
          private var mousePositions:Array = [];
          private var conversion:Number = 180 / Math.PI;
          public function DottedLine()
               init();
          private function init():void {
               drawBackground();
               line = new Shape();
               addChild(line);
               lineGraphics = line.graphics;
               stage.addEventListener(MouseEvent.CLICK, onClick);
          private function onClick(e:MouseEvent):void
               clickSwitch = 1 - clickSwitch;
               mousePositions[clickSwitch] = new Point(mouseX, mouseY);
               if (clickSwitch == 0) drawLine();
               else removeLine();
          private function removeLine():void
               lineGraphics.clear();
               line.rotation = 0;
          private function drawLine():void
               lineGraphics.lineStyle(1, 0xff0000);
               lineGraphics.moveTo(0, 0);
               var dotDistance:Number = 2;
               var nextX:Number = dotDistance;
               while (line.width < Point.distance(mousePositions[0], mousePositions[1])) {
                    lineGraphics.lineTo(nextX, 0);
                    nextX += dotDistance;
                    lineGraphics.moveTo(nextX, 0);
                    nextX += dotDistance;
               line.rotation =  conversion * Math.atan2(mousePositions[0].y - mousePositions[1].y, mousePositions[0].x - mousePositions[1].x);
               line.x = mousePositions[1].x;
               line.y = mousePositions[1].y;
          private function drawBackground():void
               var s:Shape = new Shape();
               var g:Graphics = s.graphics;
               g.beginFill(0xC0C0C0);
               g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
               g.endFill();
               addChild(s);

Similar Messages

  • How to see my code drawing a line from one point to another

    hi, im wondering if you could help me.
    i am working on my project which is to visualise travelling salesman heuristics.
    i have managed to make my first heuristic work, but my problem is that when i clicked the run button on my GUI,
    the output is already a complete tour with all the edges already drawn, but what i want is to see how it solves or draw the lines from one vertex to another just by clickin the run button once.
    would be great if you could advice me of what method or technique i need to use to see my application solving the tour or drawing the edges.
    below is my cofe for drawing the edges from one point to another
      void drawLineNNh(Graphics g){
             Graphics2D g2 = (Graphics2D) g;
             g2.setColor(Color.blue);
             int i = 0;
             if (P == null) return;
             else
                 for(i=0; i<P.getSize(); i++)
                 Line2D.Double drawLine = new Line2D.Double(P.x_coor[nnH.seen]+5, P.y_coor[nnH.seen[i]]+5, P.x_coor[nnH.seen[i+1]]+5,P.y_coor[nnH.seen[i+1]]+5);
    Line2D.Double drawLastEdge = new Line2D.Double(P.x_coor[nnH.seen[P.getSize()-1]]+5, P.y_coor[nnH.seen[P.getSize()-1]]+5, P.x_coor[0]+5,P.y_coor[0]+5);
    g2.drawString( " Total Distance : " + nnH.totalDistance , 10, 300);
    g2.draw(drawLine);
    g2.draw(drawLastEdge);
    public void setNNh(Points p)
    nnH = new NNheuristic(p);
    useNNh = true;
    public void paint(Graphics g)
    frame(g);
    drawpoints(g);
    if(useNNh)
    drawLineNNh(g);
    below is my code for calling the above method to draw edges, actionlistererun.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Run")) {
    if (chooseHeur.getSelectedItem()=="Nearest-Neighbour")
    System.out.println(chooseHeur.getSelectedItem());
    //points = new Points(toInt((String)chooseNum.getSelectedItem()));
    //PlotArea.set(points);
    PlotArea.setNNh(points);
    PlotArea.repaint();

    I AM USING SWING.
    HERE IS MY CODE, HOPEFULLY ENOUGH TO UNDERSTAND THE PROBLEM.
    class Plot extends Panel{
         public static int num;
         NNheuristic nnH;
         Closest_insertion CI;
         Points P;
         public static boolean useNNh= false;
         boolean useCI=false;
         boolean triangleDrawn = false;
         boolean CIupdate;
         void drawpoints (Graphics g)
             Graphics2D g2 = (Graphics2D) g;
             Graphics2D g3 = (Graphics2D) g;
             int i=1;
             g2.setColor(Color.red);
                    if (P==null) return;
                    else
                    while (i<P.getSize())
                         Ellipse2D.Double vertices = new Ellipse2D.Double(P.x_coor,P.y_coor[i],10,10);
    g2.fill(vertices);
    i++;
    g3.setColor(Color.MAGENTA);
    Ellipse2D.Double initial = new Ellipse2D.Double(P.x_coor[0],P.y_coor[0],10,10);
    g3.fill(initial);
    System.out.println("No. of Vertices: " + P.getSize());
    for(int k = 0; k < P.getSize(); k++)
    System.out.println("x coordinate: " + P.x_coor[k] + ", " + "y coordinate :" + P.y_coor[k] );
    // System.out.println("next:"+ P.x_coor[k+1]);
    triangleDrawn = false;
    void drawLineNNh(Graphics g){
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.blue);
    int i = 0;
    if (P == null) return;
    else
    for(i=0; i<P.getSize(); i++)
    Line2D.Double drawLine = new Line2D.Double(P.x_coor[nnH.seen[i]]+5, P.y_coor[nnH.seen[i]]+5, P.x_coor[nnH.seen[i+1]]+5,P.y_coor[nnH.seen[i+1]]+5);
    Line2D.Double drawLastEdge = new Line2D.Double(P.x_coor[nnH.seen[P.getSize()-1]]+5, P.y_coor[nnH.seen[P.getSize()-1]]+5, P.x_coor[0]+5,P.y_coor[0]+5);
    g2.drawString( " Total Distance : " + nnH.totalDistance , 10, 300);
    g2.draw(drawLine);
    g2.draw(drawLastEdge);
    public void set (Points p)
    P=p;
    useNNh = false;
    useCI = false;
    public void setNNh(Points p)
    nnH = new NNheuristic(p);
    useNNh = true;
    void frame (Graphics g)
    g.setColor(Color.white);
              g.fillRect(0,0,size().width,size().height);
              g.setColor(Color.green);
              g.drawRect(0,0,579,280);
    public void paint(Graphics g)
    frame(g);
    drawpoints(g);
    if(useNNh)
    drawLineNNh(g);
    else if(useCI)
    if(!CIupdate)
    drawTriCI(g);
    else
    drawRestCI(g);
    // drawLineNNh(g);
    public void clear ()
         // remove the points and the graph.
    P=null;
    triangleDrawn = false;
    code of my GUIpublic class TSP extends JFrame{
    JButton run;
    ...................codes...........
    TSP() {
    ...............................codes...........
    run = new JButton ("Run");
    run.setPreferredSize(new Dimension(113,30));
    run.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Run")) {
    if (chooseHeur.getSelectedItem()=="Nearest-Neighbour")
    System.out.println(chooseHeur.getSelectedItem());
    //points = new Points(toInt((String)chooseNum.getSelectedItem()));
    //PlotArea.set(points);
    PlotArea.setNNh(points);
    PlotArea.repaint();
    else if(chooseHeur.getSelectedItem()=="Closest-Insertion")
    PlotArea.setC_I(points);
    PlotArea.repaint();
    pane2.add(run);

  • Drawing a line from one nested MC to another

    I'm using the Drawing API to draw a line from one MC nested
    within two levels of parent MCs to another similarly-nested MC. Can
    someone tell me how to get the X,Y coordinates of both points?

    Try this:
    var objPointA:Object = new Object()
    var objPointB:Object = new Object()
    objPointA.x = grandparentA_mc.parentA_mc.childA_mc._x;
    objPointA.y = grandparentA_mc.parentA_mc.childA_mc._y;
    objPointB.x = grandparentB_mc.parentB_mc.childB_mc._x;
    objPointB.y = grandparentB_mc.parentB_mc.childB_mc._y;
    grandparentA_mc.parentA_mc.localToGlobal(objPointA);
    grandparentB_mc.parentB_mc.localToGlobal(objPointB);
    var lineHolder = _root.createEmptyMovieClip("lineHolder", 1);
    lineHolder.lineStyle(1, clr, 100);
    lineHolder.moveTo(objPointA.x, objPointA.y);
    lineHolder.lineTo(objPointB.x, objPointB.y);
    Remove '_spamkiller_' to mail

  • Write a program that draws a line from one mouse click spot to the next.

    WHERE AM I GOING WRONG?
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.geom.*;
    public class Week8Lab extends Applet implements MouseListener
         private int[] x;
         private int[] y;
         private int count;
         public void init()
              x = new int[10];
              y = new int[10];
              count = 0;
              this.addMouseListener(this);
         public void paint(Graphics g)
              for(int i = 0; i < 10; i++)
                   g.drawLine(x[0], y[0], x[i]+1, y[i]+1);               
         public void mouseEntered(MouseEvent e) {}
         public void mouseExited(MouseEvent e) {}
         public void mousePressed(MouseEvent e)
              x[count % 10] = e.getX();
              y[count % 10] = e.getY();     
              count++;
              repaint();
    System.out.println ("Mouse X =" + e.getX() + "Mouse Y =" + e.getY());
         public void mouseReleased(MouseEvent e) {}
         public void mouseClicked(MouseEvent e) {}
    WHERE AM I GOING WRONG?

    Try this:
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class Week8Lab extends JFrame implements MouseListener {
         private int lastX = -1;
         private int lastY = -1;     
         private int newX = 0;
         private int newY = 0;     
         public Week8Lab(){
              this.addMouseListener(this);
              setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         public void paint(Graphics g){
              if(lastX < 1){
                   lastX = newX;
                   lastY = newY;
                   return;
              g.drawLine(lastX, lastY, newX, newY);
              lastX = newX;
              lastY = newY;
         public void mouseEntered(MouseEvent e) {}
         public void mouseExited(MouseEvent e) {}
         public void mousePressed(MouseEvent e){
              newX = e.getX();
              newY = e.getY();
              repaint();
         public void mouseReleased(MouseEvent e) {}
         public void mouseClicked(MouseEvent e) {}
         public static void main(String[] args){
              JFrame fr = new Week8Lab();
              fr.setSize(300, 300);
              fr.setVisible(true);
    }

  • Drawing line from existing one without joining

    How can I draw a line from the endpoint of an existing line (snap to grid is on), without the lines being joined?  Joining causes two problems for me:
    1. The arrowhead at the end of the existing line moves to the end of the joined line, whereas I want the arrowhead in the middle of the combined line.
    2. Even without entering isolation mode the new line gets joined to a line which belong to a group, thus becoming part of that group as well. This defies the use of grouping to create logically connected units.
    TIA
    Steven

    JETalmage wrote:
     ...Worst-of-class Bezier drawing interface. The Pen Tool has absolutely no business affecting pre-existing unselected paths by default. The Pen Tool is not a selection tool, and it violates the entire meaning of something's being selected.
    JET
    while I agree in principle, on practice when clicking on end points with the pen tool most of the time I do want the paths to join but a lot of other times I do not. So, the best practical solution at least for me would be just holding the Spacebar when clicking on an end point to prevent joining without the need to click away and then drag in place. And they should make this an option in the Preferences for those who want to hold Spacebar only when joining.
    The bigger problem is that with overlapping points and edges, Illustrator uses the stacking order and not the selected object when auto joining or adding points which is actually not practical in any case. For example if there are several lines starting at the same point, not the selected but the topmost line will be joined and with overlapping edges adding a point is impossible if the selected edge is not on the top.

  • Custom Control Help: Draw a line and output start and end points

    I'm looking to find or make a custom control (or simple subVI) that will appear as a 100x100 unit grid and allow me to draw a line from one point to another on that grid. It will then output the (x,y) of the starting and end point of that line on the grid.  Any help or ideas?
    Thanks,
    Steve
    LabVIEW 2009 SP1
    Solved!
    Go to Solution.

    What you basically want is a loop with an event structure where you process Mouse Down, Move and Up events for your controls. There are any number of ways of implementing something like this, but this one will probably be the simplest:
    Use a multicolumn listbox or a table for your grid. Hide the scrollbars and headers.
    You can use the ActiveCell property with -2,-2 to select all cells. You can then use the cell size property to set the exact size of the cell.
    Next, you put a picture control on top of the table and color its background transparent so that the table shows through. You use property node to make sure the two are aligned to exactly the same spot and size.
    You use the mouse events on the picture control to detect the clicks and moves.
    You use the table's Point to Row Column method to translate the event's position data to a cell.
    You use the picture control VIs to draw the line on the picture based on that data.
    You can even color the selected cells in the table using the table properties.
    If you want to simplify things somewhat, you can also use the timeout event instead of the Mouse Move event to draw the line, but then you'll need to keep the timeout value in a shift register and reset it to -1 (no timeout) when the Mouse Up event happens.
    I would also suggest processing Mouse Enter and Leave events to change the cursor and cancel if the user leaves in the middle of dragging.
    Try to take over the world!

  • Drawing preloader line with AS?

    Hello
    I have the following AS (2) in a preloader (which works - in
    the sense that it loads the next page):
    The preloader should have a coloured line which runs from
    left to right about 0.25px thick.
    Whenever I have tried to draw the line using a pencil, it
    appears too thick, or uneven, or even goes from right to left.
    I have noticed that if I occasionally delete efforts I do not
    like, even the next page fails to load.
    What I am asking here is if it would be possible to draw the
    line in AS?
    Thanks for any useful advice.
    Steve

    Hello Devendran
    Many thanks for your post.
    So I would use something like:
    lineStyle( 1, 0xBDA0F2, 100 );
    moveTo( x1, y1 );
    lineTo( x2,y2 );
    But how would I incorporate that into my current Preloader AS
    (2)?
    Many thanks.
    Steve

  • Drawing Perpendicular Lines...

    All,
    I have a program that draws simple lines using the Line2D class. As I draw the line, I bisect it to get a point in the middle of the line. Now I have 3 points on my line.
    From here I am trying to draw a short perpendicular line originating from my bisecting point.
    I have been attempting to work from the slope formula m = (x1 - x2) / (y1 - y2), but my trig skills have gotten rusty and I can't figure out how to draw a line from an existing point and a known slope. So I have been trying to just manipulate the rise over run by taking the negative inverse (-run/rise), and adding those values to my known point. But to no avail.
    Now that I'm frustrated, I'm wondering if I'm barking up the wrong tree. Should I be using Arcs and angles to determine my perpendicular point? Or, is there an easier way to draw lines from a known point and slope?
    Thanks in advance.

    Got it.
    I was thinking about the triangle from the wrong perspective. I was making my line the adjacent side of the triangle. Once I looked at it as the hypotenuse, I realized I already had the point I was looking for.
    Was right there all along. Doh.

  • How to draw a line of sin x and its area under the line?

    Hi,
    I know how to draw a line from two points. However, I do not know how to draw a line of function sin(x) and its area under the line. Anyone know where to read or how can I draw it, please help me. Thanks !!
    Calvin

    use Graphics2D:: draw(Shape)
    create a class that implements Shape, and specifically the getPathIterator methods, in these methods you should return a path iterator that follows the sin/cos curve.
    All fairly simple to do.
    rob,

  • Error trying to use multiple lines from a PO in a Goods Receipt PO

    I get the following error when I try to add 2 lines from a PO to a single Goods Receipt PO (code below):
    -5002 One of the base documents has already been closed  [PDN1.BaseEntry][line: 1]
    I can create the Goods Receipt PO if I use 1 line or lines from multiple PO's???
                   SAPbobsCOM.Documents poReceipt2 = (SAPbobsCOM.Documents)_diApi.SboCompany.GetBusinessObject(BoObjectTypes.oPurchaseDeliveryNotes);
                   poReceipt2.CardCode = "ALL";
                   poReceipt2.DocDueDate = DateTime.Now;
                   poReceipt2.Lines.Quantity = 5;
                   poReceipt2.Lines.ItemCode = "HAMSHA";
                   poReceipt2.Lines.BaseEntry = 11;
                   poReceipt2.Lines.BaseLine = 0;
                   poReceipt2.Lines.BaseType = 22;
                   poReceipt2.Lines.Add();
                   poReceipt2.Lines.Quantity = 5;
                   poReceipt2.Lines.ItemCode = "LAMFIL";
                   poReceipt2.Lines.BaseEntry = 11;
                   poReceipt2.Lines.BaseLine = 1;
                   poReceipt2.Lines.BaseType = 22;
                   poReceipt2.Add();
    Any help is appreciated!
    Thanks,
    Daniel

    Hi Louis, thanks for the post...
    However the PO document that I am refercing definately has both lines open, if I use 1 of those lines it works fine, but the error occurs if I use 2 lines from the same PO.  I am also definately using the docentry not the docnum for the GetByKey() method.
    Can anyone run the same basic logic through the DI API?  That is create a PO with 2 lines on it, then run the code as above to make a Goods Receipt PO and reference the 2 lines from the 1 PO document?  (It works if I add multiple lines referncing lines from multiple PO docs??)
    Thanks,
    Dan
    Message was edited by: Daniel Archer

  • How to draw multiple lines on same panel??

    hiya
    i would like to know how can I draw multiple lines on the same panel ?? I have already use repaint(); but it just come out the lastest line (say line 3) i draw .......those previous lines(say line 1 and 2) are disappear ........
    Thanks for your help mate

    http://www.java2s.com/ExampleCode/2D-Graphics/Line.htm

  • How do I place text on a ScatterGraph and have an arrow point to a data point on a plot?

    For example, show the minimum point with the string "MIN" and draw an arrow from the text to the minimum point.
    I am using Measurement Studio 7.0 with C#. This could be accomplished in MS 6.0 with annotations.

    The Measurement Studio 7.0 graphs (WaveformGraph and ScatterGraph) expose events that allow you to custom draw areas of the control. Most of these events appear as pairs of BeforeDraw____ and AfterDraw___. The BeforeDraw events are raised before the drawing begins and AfterDraw events are raised after the drawing has completed.
    In your case, you can attach an event handler to the AfterDrawPlotArea event of the ScatterGraph since you want to draw on top of everything that is drawn in the plot area. In the event handler:
    // Assuming e is the name of the AfterDrawEventArgs parameter.
    // Find the minimum y-value on the first plot in the Plots
    // collection of the graph.
    double[] xData = scatterGraph1.Plots[0].GetXData();
    double[] yData = scatterGraph1.Plots[0].GetYData();
    if (yData.Length > 0)
    double x = xData[0];
    double yMin = yData[0];
    for (int i = 1; i < yData.Length; ++i)
    if (yData[i] < yMin)
    x = xData[i];
    yMin = yData[i];
    // Map the data point to a point in device coordinates.
    PointF minPoint = scatterGraph1.Plots[0].MapPoint(e.Bounds, x, yMin);
    // Calculate the starting point for the line of the
    // arrow.
    PointF startingPoint = new PointF(minPoint.X + 1, minPoint.Y - 1);
    PointF endingPoint = new PointF(minPoint.X + 10, startingPoint.Y - 9);
    // Draw a vertical line representing the stick of the
    // arrow from the point.
    e.Graphics.DrawLine(Pens.White, startingPoint, endingPoint);
    PointF leftArrowEnd = new PointF(startingPoint.X, startingPoint.Y - 3);
    PointF rightArrowEnd = new PointF(startingPoint.X + 3, startingPoint.Y);
    // Draw the arrows.
    e.Graphics.DrawLine(Pens.White, startingPoint, leftArrowEnd);
    e.Graphics.DrawLine(Pens.White, startingPoint, rightArrowEnd);
    // Calculate the location of the text by centering it
    // about the arrow.
    SizeF textSize = e.Graphics.MeasureString("MIN", scatterGraph1.Font);
    PointF textLocation = new PointF(endingPoint.X + 1, endingPoint.Y - (textSize.Height / 2));
    // Draw the "MIN" string
    e.Graphics.DrawString("MIN", scatterGraph1.Font, Brushes.White, textLocation);
    Hope this helps.
    Abhishek Ghuwalewala
    Measurement Studio
    National Instruments
    Abhishek Ghuwalewala | Measurement Studio | National Instruments

  • Is there a way to draw a straight line from one point to another?

    Is there a way of drawing a straight line from one point to another please?

    Yes.  First click on this icon:
    Now select the line drawing icon:
    Now press a shift key and drag your mouse on the image to draw a straight line.
    These instructions are for Windows system so you need to adapt the method for Macs.  I can't afford to by an Apple Mac!!!!!
    Good luck.

  • Suggestion to draw a line between two points...

    I have the XYZ position of 2 points, I would like to draw a line between those points. I have tried with a cylinder and it did not work as I expect...I guess a better method exist in Java3D... Do you have any suggestions??
    thanks
    Pete

    try the LineArray class.

  • Drawing line from thin to thick

    I need to draw a line where some segments of the line is thicker than the rest.
    I can draw the lines, but I need some kind of smooth transition from thin line to thick and the other way around. I'm using generalpath and stroke to draw the lines.
    What is the best (easiest) way to do this?

    Check out following code if it works for you. I've used b-spline approach to generate the shape of the path. You just need to specify control points and width at them.
    import java.awt.*;
    import java.awt.geom.*;
    import javax.swing.*;
    public class FloatWidthPath {
        Point2D[] pnts1;
        Point2D[] pnts2;
        CurvedPath cp1;
        CurvedPath cp2;
        public class CurvedPath {
            Point2D[] cPoints;
            double[] params;
            CubicCurve2D[] curves;
            public CurvedPath(Point2D[] pnts) {
                this.cPoints = pnts;
                double s = 0.75;
                double [] curSeg = new double[2];
                curves = new CubicCurve2D[cPoints.length-1];
                params = new double[cPoints.length];
                for (int i = 0; i < cPoints.length-1; i++){
                    curves[i] = new CubicCurve2D.Double(
                        getCP(i+1).getX(), getCP(i+1).getY(),
                        -s*getCP(i).getX()/3 + getCP(i+1).getX() +
                        s*getCP(i+2).getX()/3,
                        -s*getCP(i).getY()/3 + getCP(i+1).getY() +
                        s*getCP(i+2).getY()/3,
                        s*getCP(i+1).getX()/3 + getCP(i+2).getX() -
                        s*getCP(i+3).getX()/3,
                        s*getCP(i+1).getY()/3 + getCP(i+2).getY() -
                        s*getCP(i+3).getY()/3,
                        getCP(i+2).getX(), getCP(i+2).getY());
            Point2D getCP(int i) {
                if (i == 0) {
                    return new Point2D.Double(
                        -0.5*cPoints[0].getX() + 1.5*cPoints[1].getX(),
                        -0.5*cPoints[0].getY() + 1.5*cPoints[1].getY());
                } else if (i == cPoints.length+1) {
                    int l = cPoints.length;
                    return new Point2D.Double(
                        -0.5*cPoints[l - 1].getX() + 1.5*cPoints[l - 2].getX(),
                        -0.5*cPoints[l - 1].getY() + 1.5*cPoints[l - 2].getY());
                return cPoints[i-1];
            public CubicCurve2D getCurve(int i) {
                return curves;
    public Path2D getPath2D() {
    Path2D.Double result = new Path2D.Double();
    result.moveTo(cPoints[0].getX(), cPoints[0].getY());
    int i = 0;
    while (i < curves.length) {
    result.append(curves[i], true);
    i++;
    return result;
    public FloatWidthPath(Point2D[] pnts, double[] width) {
    pnts1 = new Point2D[pnts.length];
    pnts2 = new Point2D[pnts.length];
    double dx = 0;
    double dy = 0;
    double l = 0;
    for (int i = 0; i < pnts.length - 1; i++) {
    dx = pnts[i + 1].getX() - pnts[i].getX();
    dy = pnts[i + 1].getY() - pnts[i].getY();
    l = Math.sqrt(dx*dx + dy*dy);
    double dx1 = (dx*width[i])/l;
    double dy1 = (dy*width[i])/l;
    pnts1[i] = new Point2D.Double(pnts[i].getX()- dy1, pnts[i].getY() + dx1);
    pnts2[i] = new Point2D.Double(pnts[i].getX()+ dy1, pnts[i].getY() - dx1);
    int i = pnts.length - 1;
    double dx1 = (dx*width[width.length - 1])/l;
    double dy1 = (dy*width[width.length - 1])/l;
    pnts1[i] = new Point2D.Double(pnts[i].getX()- dy1, pnts[i].getY() + dx1);
    pnts2[i] = new Point2D.Double(pnts[i].getX()+ dy1, pnts[i].getY() - dx1);
    for (i = 0; i < (pnts2.length >> 1); i++) {
    Point2D tmp = pnts2[i];
    pnts2[i] = pnts2[pnts2.length - i - 1];
    pnts2[pnts2.length - i - 1] = tmp;
    cp1 = new CurvedPath(pnts1);
    cp2 = new CurvedPath(pnts2);
    public Path2D getPath2D() {
    Path2D.Double result = new Path2D.Double();
    result.append(cp1.getPath2D(), false);
    result.append(cp2.getPath2D(), true);
    result.closePath();
    return result;
    public static void showPath(String frameName, Point2D[] pnts,
    double [] width) {
    JFrame frame = new JFrame(frameName);
    final Point2D[] shownPnts = pnts;
    final double [] showWidth = width;
    frame.getContentPane().add(new JPanel() {
    public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    FloatWidthPath cp = new FloatWidthPath(shownPnts, showWidth);
    g2d.fill(cp.getPath2D());
    frame.setPreferredSize(new Dimension(100,100));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    public static void main(String[] args) {
    showPath("CurvedPath demo", new Point2D[]{
    new Point2D.Double(10,10),
    new Point2D.Double(100,30),
    new Point2D.Double(100,100),
    new Point2D.Double(10,120),
    }, new double [] {3, 4, 20, 8});

Maybe you are looking for