Tue Nov 11, 2008 6:38 pm
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;
}
}
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.