Total members 11894 |It is currently Sat Nov 23, 2024 10:49 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Curve Control Point in java
Code:
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;
  }
}





Attachments:
File comment: the output
controlpoint.gif
controlpoint.gif [ 5.01 KiB | Viewed 5973 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Curve Control Point in java
 Point class in C++     -  
 Mid-point Algorithm using j2me     -  
 Curve XY 2D     -  
 text like a curve     -  
 All 4 CCNA semesters in power point pass?     -  
 Drawing Circle using Mid-point implmented using J2me     -  
 Draw Hermite Curve     -  
 Draw Circles using Mid-Point Algorithm Equations- On Mouse     -  
 Draw curves using Hermite Curve equations with mouse clicks     -  
 Draw Bezier Curve and selecting the points with mouse click     -  



Topic Tags

Java Graphics
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
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