Move the curve control point and redraw the curve : Curve « 2D Graphics GUI « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorial
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » 2D Graphics GUI » CurveScreenshots 
Move the curve control point and redraw the curve
  
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.QuadCurve2D;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;

public class MainClass {
  public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.getContentPane().add(new CurveApplet());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200200);
    frame.setVisible(true);
  }
}

class CurveApplet extends JPanel {
  public CurveApplet() {
    super(new BorderLayout());
    pane = new CurvePane();
    add(pane,"Center");

    MouseHandler handler = new MouseHandler();
    pane.addMouseListener(handler);
    pane.addMouseMotionListener(handler);
  }

  class CurvePane extends JComponent {
    public CurvePane() {
      quadCurve = new QuadCurve2D.Double
          startQ.x, startQ.y, 
          control.x, control.y,
          endQ.x, endQ.y);

      cubicCurve = new CubicCurve2D.Double(
          startC.x, startC.y, 
          controlStart.x, controlStart.y,
          controlEnd.x, controlEnd.y,
          endC.x, endC.y);
    }

    public void paint(Graphics g) {
      Graphics2D g2D = (Graphics2Dg;
      quadCurve.ctrlx = ctrlQuad.getCenter().x;
      quadCurve.ctrly = ctrlQuad.getCenter().y;
      cubicCurve.ctrlx1 = ctrlCubic1.getCenter().x;
      cubicCurve.ctrly1 = ctrlCubic1.getCenter().y;
      cubicCurve.ctrlx2 = ctrlCubic2.getCenter().x;
      cubicCurve.ctrly2 = ctrlCubic2.getCenter().y;

      g2D.setPaint(Color.BLUE);
      g2D.draw(quadCurve);
      g2D.draw(cubicCurve);

      g2D.setPaint(Color.RED);
      ctrlQuad.draw(g2D);
      ctrlCubic1.draw(g2D);
      ctrlCubic2.draw(g2D);

      Line2D.Double tangent = new Line2D.Double(startQ, ctrlQuad.getCenter());
      g2D.draw(tangent);
      tangent = new Line2D.Double(endQ, ctrlQuad.getCenter());
      g2D.draw(tangent);

      tangent = new Line2D.Double(startC, ctrlCubic1.getCenter());
      g2D.draw(tangent);
      tangent = new Line2D.Double(endC, ctrlCubic2.getCenter());
      g2D.draw(tangent);

    }
  }

 
  Point2D.Double startQ = new Point2D.Double(5075);

  Point2D.Double endQ = new Point2D.Double(15075);

  Point2D.Double control = new Point2D.Double(8025)

  Point2D.Double startC = new Point2D.Double(50150)

  Point2D.Double endC = new Point2D.Double(150150)

  Point2D.Double controlStart = new Point2D.Double(80100);

  Point2D.Double controlEnd = new Point2D.Double(160100)

  Marker ctrlQuad = new Marker(control);

  Marker ctrlCubic1 = new Marker(controlStart);

  Marker ctrlCubic2 = new Marker(controlEnd);

  QuadCurve2D.Double quadCurve; 

  CubicCurve2D.Double cubicCurve; 

  CurvePane pane = new CurvePane();

  class Marker {
    public Marker(Point2D.Double control) {
      center = control; 
      circle = new Ellipse2D.Double(control.x - radius, control.y - radius, 2.0 * radius,
          2.0 * radius);
    }
    public void draw(Graphics2D g2D) {
      g2D.draw(circle);
    }

    Point2D.Double getCenter() {
      return center;
    }
    public boolean contains(double x, double y) {
      return circle.contains(x, y);
    }
    public void setLocation(double x, double y) {
      center.x = x; 
      center.y = y; 
      circle.x = x - radius; 
      circle.y = y - radius; 
    }

    Ellipse2D.Double circle; 

    Point2D.Double center; 

    static final double radius = 3;
  }

  class MouseHandler extends MouseInputAdapter {
    public void mousePressed(MouseEvent e) {
      if (ctrlQuad.contains(e.getX(), e.getY()))
        selected = ctrlQuad;
      else if (ctrlCubic1.contains(e.getX(), e.getY()))
        selected = ctrlCubic1;
      else if (ctrlCubic2.contains(e.getX(), e.getY()))
        selected = ctrlCubic2;
    }
    public void mouseReleased(MouseEvent e) {
      selected = null;
    }
    public void mouseDragged(MouseEvent e) {
      if (selected != null) {
        selected.setLocation(e.getX(), e.getY());
        pane.repaint()
      }
    }
    Marker selected = null;
  }
}

           
         
    
  
Related examples in the same category
1. Draw curve with mouseDraw curve with mouse
2. Curve with QuadCurve2DCurve with QuadCurve2D
3. MandelbrotMandelbrot
4. A spline factory instance
5. Spline 2D
6. Interpolates given points by a bezier curve
w_w__w__.ja_v___a___2s._c___o___m_ | |Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.