Draw Spline : Curve « Advanced Graphics « 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 Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
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 » Advanced Graphics » CurveScreenshots 
Draw Spline
Draw Spline

import java.awt.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.geometry.Matrix4x4;
import no.geosoft.cc.geometry.spline.SplineFactory;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li> A rudimentary sheet music library
 * <li> GObject extension
 * <li> Advanced geometry generation
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo21 extends JFrame
{
  public Demo21()
  {
    super ("G Graphics Library - Demo 21");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the graphic canvas
    GWindow window = new GWindow (new Color (255255255));
    getContentPane().add (window.getCanvas());
    
    // Create scene with default viewport and world extent settings
    GScene scene = new GScene (window);

    // Create a stave
    GStave stave1 = new GStave (25);
    stave1.setLocation (3050440);

    GNote note;
    note = new GNote ("f"1.010);
    stave1.addNote (note);
    
    note = new GNote ("g"0.510);
    stave1.addNote (note);

    note = new GNote ("d"0.510);        
    stave1.addNote (note);    
    
    note = new GNote ("a"1.010);        
    stave1.addNote (note);    

    note = new GNote ("a"1.010);        
    stave1.addNote (note);    

    
    // Another stave
    GStave stave2 = new GStave (25);
    stave2.setLocation (30250440);

    note = new GNote ("a"1.010);
    stave2.addNote (note);
    
    note = new GNote ("a"1.010);
    stave2.addNote (note);

    note = new GNote ("h"0.510);        
    stave2.addNote (note);    
    
    note = new GNote ("e"1.010);        
    stave2.addNote (note);    

    note = new GNote ("g"0.510);        
    stave2.addNote (note);    

    scene.add (stave1);
    scene.add (stave2);    

    pack();
    setSize (new Dimension (500500));
    setVisible (true);
  }

  

  private class GStave extends GObject
  {
    private int         x_, y_;
    private int         length_;
    private int         lineGap_;
    private GSegment[]  lines_;
    private GSegment    startLine_;
    private GSegment    endLine_;
    private GSegment    endBar_;
    private int         current_;
    

    public GStave (int lineGap)
    {
      lineGap_ = lineGap;
      
      GStyle style = new GStyle();
      style.setBackgroundColor (new Color (000));
      style.setForegroundColor (new Color (000));
      style.setLineWidth ((intMath.max ((intlineGap / 20.01));
      setStyle (style);

      lines_ = new GSegment[5];
      for (int i = 0; i < 5; i++) {
        lines_[inew GSegment();
        addSegment (lines_[i]);
      }

      startLine_ = new GSegment();
      addSegment (startLine_);
      
      endLine_ = new GSegment();
      addSegment (endLine_);

      endBar_ = new GSegment();
      addSegment (endBar_);
    }
    

    public void setLocation (int x, int y, int length)
    {
      x_ = x;
      y_ = y;
      length_ = length;
      current_ = x_ + * lineGap_;
    }


    public void addNote (GNote note)
    {
      String value = note.getValue();

      int y0 = 0;
      if      (value.equals ("c")) y0 = 10;
      else if (value.equals ("d")) y0 =  9;
      else if (value.equals ("e")) y0 =  8;
      else if (value.equals ("f")) y0 =  7;
      else if (value.equals ("g")) y0 =  6;
      else if (value.equals ("a")) y0 =  5;
      else if (value.equals ("h")) y0 =  4;                              
      
      note.setLocation (current_, y_ + (intMath.round (y0 * lineGap_ * 0.5));
      add (note);

      current_ += lineGap_ * 3;
    }
    

    public void draw()
    {
      for (int i = 0; i < 5; i++) {
        lines_[i].setGeometry (x_, y_ + i * lineGap_,
                               x_ + length_, y_ + i * lineGap_);
      }

      startLine_.setGeometry (x_, y_, x_, y_ + * lineGap_);
      
      endBar_.setGeometry (new int[] {x_ + length_,                          y_,
                                      x_ + length_ - (int) (lineGap_ * 0.5), y_,
                                      x_ + length_ - (int) (lineGap_ * 0.5), y_ + * lineGap_,
                                      x_ + length_,                          y_ + * lineGap_,
                                      x_ + length_,                          y_});

      endLine_.setGeometry (x_ + length_ - (int) (lineGap_ * 0.9), y_,
                            x_ + length_ - (int) (lineGap_ * 0.9),y_ + * lineGap_);
    }
  }
  
  

  private class GNote extends GObject
  {
    private String     value_;
    private double     duration_;
    private GSegment   head_;
    private GSegment   stem_;
    private GSegment   flag_;
    private int        x_, y_;
    private int        headSize_;
    private int        stemLength_;
    private Matrix4x4  headRotation_;
    
    
    
    public GNote (String value, double duration, int size)
    {
      value_    = value;
      duration_ = duration;
      headSize_ = size;
      
      GStyle style = new GStyle();
      style.setBackgroundColor (new Color (000));
      style.setForegroundColor (new Color (000));      
      setStyle (style);
      
      head_ = new GSegment();
      addSegment (head_);

      stem_ = new GSegment();
      addSegment (stem_);

      flag_ = new GSegment();
      addSegment (flag_);
    }


    public String getValue()
    {
      return value_;
    }
    
    
    public double getDuration()
    {
      return duration_;
    }
    

    
    public void setLocation (int x, int y)
    {
      x_ = x;
      y_ = y;
    }
    

    
    public void draw()
    {
      //
      // Head
      //
      int[] head = Geometry.createEllipse (00,
                                           headSize_,
                                           (int) ((doubleheadSize_ * 1.5));

      Matrix4x4 m = new Matrix4x4();
      m.rotateZ (Math.PI / 3.0);
      m.translate (x_, y_, 0.0);
      m.transformXyPoints (head);

      head_.setGeometry (head);

      //
      // Stem
      //
      int stemWidth = (intMath.round ((doubleheadSize_ / 5.0);
      if (stemWidth < 1stemWidth = 1;

      int stemLength = headSize_ * 8;
      int stemX0 = x_ + (int) ((doubleheadSize_ * 1.40);
      int stemY0 = y_ - (int) (headSize_ * 0.2);
      
      int[] stem = new int[] {stemX0,             stemY0,
                              stemX0 - stemWidth, stemY0,
                              stemX0 - stemWidth, stemY0 - stemLength,
                              stemX0,             stemY0 - stemLength,
                              stemX0,             stemY0};
      
      stem_.setGeometry (stem);

      //
      // Flag
      //
      if (duration_ < 1.0) {
        int flagX0 = stemX0;
        int flagY0 = stemY0 - stemLength;

        double[] cp = new double[]
                      {flagX0, flagY0, 0.0,
                       flagX0 + (int) (headSize_ * 0.5), flagY0 + (int) (stemLength * 0.2)0.0,
                       flagX0 + (int) (headSize_ * 2.0), flagY0 + (int) (stemLength * 0.5)0.0,
                       flagX0 + (int) (headSize_ * 1.8), flagY0 + (int) (stemLength * 0.9)0.0};
        double[] spline1 = SplineFactory.createCatmullRom (cp, 20);
        
        cp = new double[]
             {flagX0 + (int) (headSize_ * 1.8), flagY0 + (int) (stemLength * 0.9)0.0,
              flagX0 + (int) (headSize_ * 1.7), flagY0 + (int) (stemLength * 0.7)0.0,
              flagX0 + (int) (headSize_ * 0.5), flagY0 + (int) (stemLength * 0.45)0.0,                       
              flagX0,                           flagY0 + (int) (stemLength * 0.35)0.0};
        double[] spline2 = SplineFactory.createCatmullRom (cp, 20);
        
        int[] flag = new int[(spline1.length + spline2.length2];
        
        int j = 0;
        for (int i = 0; i < spline1.length; i+=3) {
          flag[j++(intMath.round (spline1[i+0]);
          flag[j++(intMath.round (spline1[i+1]);
        }
        for (int i = 0; i < spline2.length; i+=3) {
          flag[j++(intMath.round (spline2[i+0]);
          flag[j++(intMath.round (spline2[i+1]);
        }
        
        flag_.setGeometry (flag);
      }
    }
  }

    


  public static void main (String[] args)
  {
    new Demo21();
  }
}

           
       
G-DrawSpline.zip( 249 k)
Related examples in the same category
1. Spline EditorSpline Editor
2. Animated GraphAnimated Graph
3. Epsilon DeltaEpsilon Delta
4. Families Of GraphsFamilies Of Graphs
5. Integral CurvesIntegral Curves
6. Trace curveTrace curve
7. Input the function and draw the curveInput the function and draw the curve
8. Scatter PlotScatter Plot
9. Zoom interaction, Text background color, and the effect of transparencyZoom interaction, Text background color, and the effect of transparency
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.