Home Java Example Java Swing Graphics2D Move Curve Control Point
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Move Curve Control Point

Advertisement
In this section, you will studied how to move the curve control point and manipulates the shape of curve.

Move Curve Control Point

     

In this section, you will studied how to move the curve control point and manipulates the shape of curve.

The Control points are the points on a curve which are used to change the shape and angle of the curve. On stretching and rotating the control point, the shape of the curve and the angle of a curve manipulates.

To draw a curve, we have defined the class QuadCurve2D.Double  and CubicCurve2D.Double. The class QuadCurve2D.Double defines a  quadratic parametric curve segment. The class Line2D.Double creates a line segment. The Point2D.Double class defines a point. The MouseEvent class calls its methods mousePressed(), mouseReleased(), mouseDragged()  through which you can stretch the control point in any direction and manipulates the curve.

Here is the code of CurveControlPoint.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputAdapter;

public class CurveControlPoint {
  public static void main(String[] args){
    JFrame frame = new JFrame("Curve Control Point");
    frame.getContentPane().add(new Curve());
    frame.setSize(350, 200);
    frame.setVisible(true);
  }
}
class Curve extends JPanel {
  QuadCurve2D.Double curveQ; 
  CubicCurve2D.Double curveC; 

    public Curve() {
    super(new BorderLayout());
    curve1 = new Curve1();
    add(curve1,"Center");
    MouseHandler handler = new MouseHandler();
    curve1.addMouseListener(handler);
    curve1.addMouseMotionListener(handler);
  }
class Curve1 extends JComponent {
  public Curve1() {
    curveQ = new QuadCurve2D.Double( 
    startPoint.x, startPoint.y, 
    point2d.x, point2d.y,
    endPoint.x, endPoint.y);
    curveC = new CubicCurve2D.Double();
      }
public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2D) g;
      curveQ.ctrlx = curve.getCenter().x;
      curveQ.ctrly = curve.getCenter().y;
      g2D.setPaint(Color.magenta);
      g2D.draw(curveQ);
      g2D.draw(curveC);
      g2D.setPaint(Color.blue);
      curve.draw(g2D);
      Line2D.Double line = new Line2D.Double(startPoint, curve.getCenter());
      g2D.draw(line);
      line = new Line2D.Double(endPoint, curve.getCenter());
      g2D.draw(line);
}
  }
  Point2D.Double startPoint = new Point2D.Double(60, 95);
  Point2D.Double endPoint = new Point2D.Double(150, 75);
  Point2D.Double point2d = new Point2D.Double(80, 25); 
  CurveMarker curve = new CurveMarker(point2d);
  Curve1 curve1 = new Curve1();

  class CurveMarker {
    public CurveMarker(Point2D.Double point2d) {
      point = point2d; 
      ellipse = new Ellipse2D.Double(point2d.x - r, point2d.y - r, 2.0 * r,
          2.0 * r);
    }
    public void draw(Graphics2D g2D) {
      g2D.draw(ellipse);
    }
    Point2D.Double getCenter() {
      return point;
    }
    public boolean contains(double x, double y) {
      return ellipse.contains(x, y);
    }
    public void setLocation(double x, double y) {
      point.x = x; 
      point.y = y; 
      ellipse.x = x - r; 
      ellipse.y = y - r; 
    }
    Ellipse2D.Double ellipse; 
    Point2D.Double point; 
    double r = 3;
  }
  class MouseHandler extends MouseInputAdapter {
    public void mousePressed(MouseEvent event) {
        selected = curve;
      }
    public void mouseReleased(MouseEvent event) {
      selected = null;
    }
    public void mouseDragged(MouseEvent event) {
        selected.setLocation(event.getX(), event.getY());
        curve1.repaint(); 
    }
    CurveMarker selected = null;
  }
}

Output will be displayed as

On stretching the control point with mouse, the shape of the curve will be changed:

Download Source Code

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 8, 2008

Ask Questions?    Discuss: Move Curve Control Point  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com