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. Email
14. Event
15. File Input Output
16. Game
17. Generics
18. Hibernate
19. I18N
20. J2EE
21. J2ME
22. JDK 6
23. JSP
24. JSTL
25. Language Basics
26. Network Protocol
27. PDF RTF
28. Reflection
29. Regular Expressions
30. Scripting
31. Security
32. Servlets
33. Spring
34. Swing Components
35. Swing JFC
36. SWT JFace Eclipse
37. Threads
38. Tiny Application
39. Velocity
40. Web Services SOA
41. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
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
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
www__.___j_a_v_a___2_s___.c___om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.