Drawing a curve with two control points

Use the class CubicCurve.

This example draws a curve on a frame using two end points and two control points. It allows you to grab an end or control point and drag it around. In addition, the curve has been texture-painted.

Main.java:

import java.awt.image.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   private BufferedImage image; 
   Point2D[] curvePoints; 
   Point2D selectedCurvePoint;
 
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      image = toBufferedImage(new javax.swing.ImageIcon("c:\djkrush.jpg").getImage());
 
      curvePoints = new Point2D[4];
      curvePoints[0] = new Point2D.Double( 50, 150);
      curvePoints[1] = new Point2D.Double(100,  50);
      curvePoints[2] = new Point2D.Double(200, 250);
      curvePoints[3] = new Point2D.Double(250, 150);
 
      addMouseListener(new MouseAdapter() {
         public void mousePressed(MouseEvent me) {
            selectedCurvePoint = null;
            for (int i=0; i<curvePoints.length; i++) {
               Rectangle2D rect = getCurvePoint(curvePoints[i]);
               if (rect.contains(me.getPoint())) {
                  selectedCurvePoint = curvePoints[i];
                  break;
               }
            }
         }
      });
 
      addMouseMotionListener(new MouseMotionAdapter() {
         public void mouseDragged(MouseEvent me) {
            if (selectedCurvePoint != null) {
               selectedCurvePoint.setLocation(me.getPoint());
               repaint();
            }
         }
      });
   }
 
 
   public void paint(Graphics g) {
      super.paint(g); 
 
      Graphics2D g2d = (Graphics2D) g;
 
      Rectangle2D tr = new Rectangle2D.Double(0, 0, image.getWidth(), image.getHeight());
      TexturePaint tp = new TexturePaint(image, tr);
 
      // draw texture filled ellipse, but with antialiasing
      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);
      BasicStroke bs = new BasicStroke(5, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                           10, null, 0);
      g2d.setStroke(bs);
      g2d.setPaint(tp);
 
      CubicCurve2D cc = new CubicCurve2D.Float();
      cc.setCurve(curvePoints, 0);
      g2d.draw(cc);
      g2d.setColor(Color.black);
      for (int i=0; i<4; i++) {
         g2d.draw(getCurvePoint(curvePoints[i]));
      } 
   }
 
   public Rectangle2D.Float getCurvePoint(Point2D p2d) {
      return new Rectangle2D.Float((int) p2d.getX()-2, (int) p2d.getY()-2, 4, 4);
   }
 
   public BufferedImage toBufferedImage(Image image) {
      BufferedImage bi = new BufferedImage(image.getWidth(null), image.getHeight(null), 
                                           BufferedImage.TYPE_INT_RGB); 
 
      // copy the original image
      Graphics g = bi.createGraphics();
    
      g.drawImage(image, 0, 0, null);
      g.dispose();
 
      return bi;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}

Image used: