Total members 10261 | Gratitudes |It is currently Tue May 22, 2012 4:50 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Curve Control Point in java
PostPosted: Tue Nov 11, 2008 6:38 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2279
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

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 1797 times ]

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 video chat application in java
 navigating to database using java
 java mobile apps
 need help in java
 Read your gmail using Java code
 Java Programing to communicating port parallel
 Java Chat
 Steganography in java
 java code for listing folder contents from remote folder
 java

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 3 guests



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team