Draw Spline : Curve « Advanced Graphics « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
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
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net 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. Animated GraphAnimated Graph
2. Epsilon DeltaEpsilon Delta
3. Families Of GraphsFamilies Of Graphs
4. Integral CurvesIntegral Curves
5. Trace curveTrace curve
6. Input the function and draw the curveInput the function and draw the curve
7. Scatter PlotScatter Plot
8. Zoom interaction, Text background color, and the effect of transparencyZoom interaction, Text background color, and the effect of transparency
ww___w___.ja_v___a___2__s_.__co___m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.