Geometry Generation : Geometry « 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 » GeometryScreenshots 
Geometry Generation
Geometry Generation

import java.awt.*;
import java.awt.event.*;
import java.text.*;

import javax.swing.*;

import no.geosoft.cc.geometry.Geometry;
import no.geosoft.cc.graphics.*;



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li> A math application
 * <li> Polyline performance
 * <li> Geometry generation
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo20 extends JFrame
  implements ActionListener
{
  private JButton        addLevelButton_;
  private JButton        removeLevelButton_;
  private JLabel         nEdgesLabel_;
  private JLabel         lengthLabel_;
  private JLabel         areaLabel_;
  private KochSnowflake  kochSnowflake_;
  
  
  
  public Demo20()
  {
    super ("G Graphics Library - Demo 20");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

    JPanel controlPanel = new JPanel();
    addLevelButton_ = new JButton("+");
    addLevelButton_.addActionListener (this);
    controlPanel.add (addLevelButton_);

    removeLevelButton_ = new JButton("-");
    removeLevelButton_.addActionListener (this);    
    controlPanel.add (removeLevelButton_);

    nEdgesLabel_ = new JLabel();
    controlPanel.add (nEdgesLabel_);
    
    lengthLabel_ = new JLabel();
    controlPanel.add (lengthLabel_);
    
    areaLabel_ = new JLabel();
    controlPanel.add (areaLabel_);
    getContentPane().add (controlPanel, BorderLayout.NORTH);
    
    // Create the graphic canvas
    GWindow window = new GWindow (new Color (255255255));
    getContentPane().add (window.getCanvas(), BorderLayout.CENTER);
    
    // Create scene with default viewport and world extent settings
    GScene scene = new GScene (window);
    double w0[] {0.0,  0.00.0};
    double w1[] {1.0,  0.00.0};
    double w2[] {0.0,  1.00.0};
    scene.setWorldExtent (w0, w1, w2);

    // Create the graphics object and add to the scene
    kochSnowflake_ = new KochSnowflake();
    scene.add (kochSnowflake_);

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

    window.startInteraction (new ZoomInteraction (scene));
    refresh();
  }



  public void actionPerformed (ActionEvent event)
  {
    if (event.getSource() == addLevelButton_)
      kochSnowflake_.addLevel();
    else
      kochSnowflake_.removeLevel();

    refresh();
  }


  
  public void refresh()
  {
    nEdgesLabel_.setText ("#edges = " + kochSnowflake_.getNEdges());

    DecimalFormat format = new DecimalFormat ("0.00000");
    
    double length = kochSnowflake_.computeLength();
    lengthLabel_.setText ("  Length = " + format.format (length));
    
    double area = kochSnowflake_.computeArea();
    areaLabel_.setText ("  Area = " + format.format (area));
    
    kochSnowflake_.refresh();
  }
  

  
  /**
   * Defines the geometry and presentation for the sample
   * graphic object.
   */   
  private class KochSnowflake extends GObject
  {
    private GSegment  curve_;
    private double[]  geometry_;
    
    
    KochSnowflake()
    {
      GStyle style = new GStyle();
      style.setForegroundColor (new Color (25500));
      style.setBackgroundColor (new Color (2552550));
      setStyle (style);

      curve_ = new GSegment();
      geometry_ = new double[] {0.20.2,
                                0.80.2,
                                0.50.2 0.6 * Math.sin (Math.PI / 3.0),
                                0.20.2};
      addSegment (curve_);
    }


    public int getNEdges()
    {
      return geometry_.length / 1;
    }
    
    
    public double computeLength()
    {
      double length = 0;
      for (int i = 0; i < geometry_.length - 2; i += 2) {
        length += Geometry.length (geometry_[i+0], geometry_[i+1],
                                   geometry_[i+2], geometry_[i+3]);
      }
      return length;
    }

    

    public double computeArea()
    {
      return Geometry.computePolygonArea (geometry_);
    }
    

    
    public void addLevel()
    {
      int nLegs   = geometry_.length / 1;
      int nPoints = nLegs * 2;

      double[] geometry = new double[nPoints];

      int j = 0;
      for (int i = 0; i < nLegs*2; i += 2) {
        geometry[j+0= geometry_[i+0];
        geometry[j+1= geometry_[i+1];

        double[] v0 = Geometry.computePointOnLine (geometry_[i+0],
                                                   geometry_[i+1],
                                                   geometry_[i+2],
                                                   geometry_[i+3],
                                                   1.0 3.0);
        geometry[j+2= v0[0];
        geometry[j+3= v0[1];        

        double[] v1 = Geometry.computePointOnLine (geometry_[i+0],
                                                   geometry_[i+1],
                                                   geometry_[i+2],
                                                   geometry_[i+3],
                                                   2.0 3.0);

        geometry[j+6= v1[0];
        geometry[j+7= v1[1];        

        double length = Geometry.length (v0[0], v0[1], v1[0], v1[1]);

        double dx = geometry_[i+2- geometry_[i+0];
        double dy = geometry_[i+3- geometry_[i+1];

        double l = Math.sqrt (dx * dx + dy * dy);
        double angle = Math.acos (dx / l);
        if (dy < 0angle = 2.0 * Math.PI - angle;
        angle -= Math.PI / 3.0;

        geometry[j+4= geometry[j+2+ length * Math.cos (angle);
        geometry[j+5= geometry[j+3+ length * Math.sin (angle);

        j+= 8;
      }

      geometry[geometry.length-2= geometry[0];
      geometry[geometry.length-1= geometry[1];      
      
      geometry_ = geometry;
      redraw();
    }

    

    public void removeLevel()
    {
      int nLegs   = geometry_.length / 1;
      if (nLegs == 3return;
      
      int nPoints = nLegs / 2;

      double[] geometry = new double[nPoints];

      int j = 0;
      for (int i = 0; i < geometry.length - 2; i += 2) {
        geometry[i+0= geometry_[j+0];
        geometry[i+1= geometry_[j+1];
        j += 8;
      }

      geometry[geometry.length-2= geometry[0];
      geometry[geometry.length-1= geometry[1];      

      geometry_ = geometry;
      redraw();
    }
    
    
    
    public void draw()
    {
      curve_.setGeometryXy (geometry_);
    }
  }
  


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

           
       
G-GemometryGeneration.zip( 229 k)
Related examples in the same category
1. Update World Extent GeometryUpdate World Extent Geometry
2. Embedded AWT components, Custom selection interaction, Dynamic annotationsEmbedded AWT components, Custom selection interaction, Dynamic annotations
3. Advanced geometry generation
4. Draw String and StarDraw String and Star
5. Draw Radiation: Annotation layout mechanism, Visibility settings, Custom linestyleDraw Radiation: Annotation layout mechanism, Visibility settings, Custom linestyle
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.