Advaced geometry generation : Geometry « 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 » GeometryScreenshots 
Advaced geometry generation

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.*;

import javax.swing.*;

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



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li> Advaced geometry generation
 * <li> Dynamic update
 * <li> Threading
 * </ul>
 
 @author <a href="mailto:jacob.dreyer@geosoft.no">Jacob Dreyer</a>
 */   
public class Demo23 extends JFrame
{
  public Demo23()
  {
    super ("G Graphics Library - Demo 23");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create the graphic canvas
    GWindow window = new GWindow (new Color (210235255));
    getContentPane().add (window.getCanvas());
    
    // Create scene with default viewport and world extent settings
    GScene scene = new GScene (window);
    double w0[] {  0.0,   0.00.0};
    double w1[] {1000.0,   0.00.0};
    double w2[] {  0.01000.00.0};
    scene.setWorldExtent (w0, w1, w2);

    // Create clock
    GClock clock1 = new GClock (500500350);
    scene.add (clock1);

    // GClock clock2 = new GClock (40, 80, 15);
    // scene.add (clock2);

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

    window.startInteraction (new ZoomInteraction (scene));

    Timer timer = new Timer (true);
    timer.schedule (new Ticker (clock1)01000);
  }


  
  /**
   * Defines the geometry and presentation for the sample
   * graphic object.
   */   
  private class GClock extends GObject
  {
    private double      x0_, y0_;
    private double      radius_;
    private GSegment    background_;
    private GSegment    disc_;
    private GSegment[]  ticks_;
    private GSegment[]  labels_;
    private GSegment    hourHandle_;
    private GSegment    minuteHandle_;
    private GSegment    secondHandle_;
    private GSegment    brand_;
    private double[]    hourHandleGeometry_;
    private double[]    minuteHandleGeometry_;
    private double[]    secondHandleGeometry_;
    

        
    public GClock (double x0, double y0, double radius)
    {
      x0_     = x0;
      y0_     = y0;
      radius_ = radius;

      background_ = new GSegment();
      GStyle backgroundStyle = new GStyle();
      backgroundStyle.setBackgroundColor (new Color (122136161));
      backgroundStyle.setForegroundColor (new Color (000));
      background_.setStyle (backgroundStyle);
      addSegment (background_);
      
      disc_ = new GSegment();
      GStyle discStyle = new GStyle();
      discStyle.setBackgroundColor (new Color (255255255));
      discStyle.setForegroundColor (new Color (255255255));
      disc_.setStyle (discStyle);
      addSegment (disc_);

      ticks_ = new GSegment[60];
      GStyle minuteStyle = new GStyle();
      minuteStyle.setForegroundColor (new Color (000));
      minuteStyle.setLineWidth (1);

      GStyle tickStyle = (GStyleminuteStyle.clone();
      tickStyle.setLineWidth (3);
      
      for (int i = 0; i < 60; i++) {
        ticks_[inew GSegment();
        ticks_[i].setStyle (i % == ? tickStyle : minuteStyle);
        addSegment (ticks_[i]);
      }

      labels_ = new GSegment[12];
      GStyle labelStyle = new GStyle();
      labelStyle.setForegroundColor (new Color (000));
      labelStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
      labelStyle.setFont (new Font ("Dialog", Font.BOLD, 24));
      for (int i = 0; i < 12; i++) {
        labels_[inew GSegment();
        labels_[i].setStyle (labelStyle);
        int hour = (14 - i12 1;
        labels_[i].setText (new GText (Integer.toString (hour)));
        addSegment (labels_[i]);
      }

      brand_ = new GSegment();
      GStyle brandStyle = new GStyle();
      brandStyle.setForegroundColor (new Color (000));
      brandStyle.setFont (new Font ("Times", Font.PLAIN, 12));
      brandStyle.setLineStyle (GStyle.LINESTYLE_INVISIBLE);
      brand_.setStyle (brandStyle);
      brand_.setText (new GText ("GeoSoft", GPosition.MIDDLE));
      addSegment (brand_);

      GStyle handleStyle = new GStyle();
      handleStyle.setForegroundColor (new Color (0.0f0.0f0.0f0.5f));
      handleStyle.setBackgroundColor (new Color (0.0f0.0f0.0f0.3f));
      handleStyle.setLineWidth (1);      

      GStyle secondHandleStyle = new GStyle();
      secondHandleStyle.setForegroundColor (new Color (25500));
      secondHandleStyle.setBackgroundColor (new Color (25500));
      secondHandleStyle.setLineWidth (1);      
      
      hourHandle_ = new GSegment();
      hourHandle_.setStyle (handleStyle);
      addSegment (hourHandle_);

      hourHandleGeometry_ = new double[]
                            {0.10*radius_, - 0.04*radius_,
                             0.10*radius_, + 0.04*radius_,
                             0.60*radius_, + 0.04*radius_,
                             0.65*radius_, 0.0,
                             0.60*radius_, - 0.04*radius_,
                             0.10*radius_, - 0.04*radius_};
      
      minuteHandle_ = new GSegment();
      minuteHandle_.setStyle (handleStyle);      
      addSegment (minuteHandle_);

      minuteHandleGeometry_ = new double[]
                            {0.10*radius_, - 0.04*radius_,
                             0.10*radius_, + 0.04*radius_,
                             0.90*radius_, + 0.04*radius_,
                             0.95*radius_, 0.0,
                             0.90*radius_, - 0.04*radius_,
                             0.10*radius_, - 0.04*radius_};

      secondHandle_ = new GSegment();
      secondHandle_.setStyle (secondHandleStyle);
      addSegment (secondHandle_);

      secondHandleGeometry_ = new double[]
                            {0.10*radius_, - 0.02*radius_,
                             0.85*radius_, 0.0,
                             0.10*radius_, + 0.02*radius_,
                             0.10*radius_, - 0.02*radius_};
    }

    

    private void update()
    {
      Calendar time = Calendar.getInstance();

      int hour   = time.get (Calendar.HOUR_OF_DAY);
      int minute = time.get (Calendar.MINUTE);
      int second = time.get (Calendar.SECOND);
      int secondOfDay = second + minute*60 + hour*60*60;

      double hourAngle = Math.PI / 2.0 -
                         (doublesecondOfDay / (24.0 60.0 60.0*
                         Math.PI * 4.0;

      secondOfDay -= hour*60*60;

      double minuteAngle = Math.PI / 2.0 -
                           (doublesecondOfDay / (60.0 60.0*
                           Math.PI * 2.0;

      secondOfDay -= minute*60;

      double secondAngle = Math.PI / 2.0 -
                           (doublesecondOfDay / 60.0 *
                           Math.PI * 2.0;
      
      double[] geometry = new double[hourHandleGeometry_.length];
      System.arraycopy (hourHandleGeometry_, 0, geometry, 0,
                        hourHandleGeometry_.length);
      Matrix4x4 m = new Matrix4x4();
      m.rotateZ (hourAngle);
      m.translate (x0_, y0_, 0.0);
      m.transformXyPoints (geometry);
      hourHandle_.setGeometryXy (geometry);

      geometry = new double[minuteHandleGeometry_.length];
      System.arraycopy (minuteHandleGeometry_, 0, geometry, 0,
                        minuteHandleGeometry_.length);
      m = new Matrix4x4();
      m.rotateZ (minuteAngle);
      m.translate (x0_, y0_, 0.0);      
      m.transformXyPoints (geometry);
      minuteHandle_.setGeometryXy (geometry);

      geometry = new double[secondHandleGeometry_.length];
      System.arraycopy (secondHandleGeometry_, 0, geometry, 0,
                        secondHandleGeometry_.length);
      m = new Matrix4x4();
      m.rotateZ (secondAngle);
      m.translate (x0_, y0_, 0.0);      
      m.transformXyPoints (geometry);
      secondHandle_.setGeometryXy (geometry);
    }
    
    
  
    public void draw()
    {
      background_.setGeometryXy (Geometry.createCircle (x0_, y0_, radius_ * 1.2));
      disc_.setGeometryXy (Geometry.createCircle (x0_, y0_, radius_));

      for (int i = 0; i < 60; i++) {
        double x0 = radius_ * (i % == 0.88 0.92);
        double x1 = radius_ * 0.98;
        
        double[] geometry = new double[] {x0, 0, x1, 0};
        Matrix4x4 m = new Matrix4x4();
        m.rotateZ (2.0 * Math.PI * i / 60.0);
        m.translate (x0_, y0_, 0.0);
        m.transformXyPoints (geometry);
        ticks_[i].setGeometryXy (geometry);
      }

      for (int i = 0; i < 12; i++) {
        double[] geometry = new double[] {radius_ * 0.750};        

        Matrix4x4 m = new Matrix4x4();
        m.rotateZ (2.0 * Math.PI * i / 12.0);
        m.translate (x0_, y0_, 0.0);
        m.transformXyPoints (geometry);

        labels_[i].setGeometryXy (geometry);
      }

      brand_.setGeometry (x0_, y0_ - radius_ * 0.3);
      
      update();
    }
  }
  

  
  private class Ticker extends TimerTask
  {
    private GClock clock_;
    
    public Ticker (GClock clock)
    {
      clock_ = clock;
    }
    
    public void run()
    {
      clock_.update();
      clock_.refresh();
    }
  }

  

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

           
       
G-DynamicUpdate.zip( 234 k)
Related examples in the same category
1. Update World Extent GeometryUpdate World Extent Geometry
2. Gemometry GenerationGemometry Generation
3. Embedded AWT components, Custom selection interaction, Dynamic annotationsEmbedded AWT components, Custom selection interaction, Dynamic annotations
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
ww___w___.__j___a___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.