Simple animation technique : Animation « 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 » AnimationScreenshots 
Simple animation technique
Simple animation technique

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>Simple animation technique
 * </ul>
 
 @author <a href="mailto:[email protected]">Jacob Dreyer</a>
 */   
public class Demo9 extends JFrame
{

  public Demo9()
  {
    super ("G Graphics Library - Demo 9");
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    // Create graphic canvas
    GWindow window = new GWindow (new Color (200210200));
    getContentPane().add (window.getCanvas());    
    
    // Create a view with default viewport and world extent
    GScene scene = new GScene (window);

    // Create a graphic object and put into view
    TestObject object = new TestObject();
    scene.add (object);

    pack();
    setSize (new Dimension (500500));
    setVisible (true);
    
    // Keep program running. Add sleep() if it rotates too fast.
    while (!false) {
      object.rotate();
      window.refresh();
    }
  }
  
  


  class TestObject extends GObject
  {
    private static final int AXIS1_LENGTH = 500;
    private static final int AXIS2_LENGTH = 220;
    private static final int RADII        = 100;
  
    private double[]    spikePos_;
    private GSegment    weel_;
    private GSegment[]  spikes_;
    private GSegment    axis1_;
    private GSegment    axis2_;
    private double      position_;
  
  
    public TestObject()
    {
      spikePos_ = new double[] {0.0 * Math.PI / 3.0,
                                1.0 * Math.PI / 3.0,                              
                                2.0 * Math.PI / 3.0,
                                3.0 * Math.PI / 3.0,
                                4.0 * Math.PI / 3.0,
                                5.0 * Math.PI / 3.0};
      spikes_ = new GSegment[spikePos_.length];

      GStyle spikeStyle = new GStyle();
      spikeStyle.setLineWidth (6);
      spikeStyle.setCapStyle (BasicStroke.CAP_ROUND);      
      spikeStyle.setForegroundColor (new Color (130140130));      

      weel_ = new GSegment();
      GStyle weelStyle = new GStyle();
      weelStyle.setForegroundColor (new Color (708070));
      weelStyle.setLineWidth (20);
      weel_.setStyle (weelStyle);
      addSegment (weel_);

      for (int i = 0; i < spikePos_.length; i++) {
        spikes_[inew GSegment();
        spikes_[i].setStyle (spikeStyle);
        addSegment (spikes_[i]);
      }

      axis2_ = new GSegment();
      GStyle style = new GStyle();
      style.setForegroundColor (new Color (0.3f0.4f0.3f0.5f));
      style.setLineWidth (30);
      axis2_.setStyle (style);
      addSegment (axis2_);

      GText text = new GText (null, GPosition.RIGHT | GPosition.WEST);
      style = new GStyle();
      style.setForegroundColor (new Color (255255255));
      style.setFont (new Font ("Dialog", Font.BOLD, 18));
      text.setStyle (style);
      axis2_.setText (text);
      
      axis1_ = new GSegment();
      style = new GStyle();
      style.setForegroundColor (new Color (160160160));
      style.setLineWidth (15);
      // style.setCapStyle (BasicStroke.CAP_BUTT);
      axis1_.setStyle (style);
      addSegment (axis1_);

      position_ = 0.0;
    }


  
    public void draw()
    {
      // Center of viewport
      int x0 = (intgetScene().getViewport().getCenterX() 100;
      int y0 = (intgetScene().getViewport().getCenterY();

      // Geometry for the wheel circumference
      int[] xy = Geometry.createEllipse (x0, y0, RADII, RADII);
      weel_.setGeometry (xy);

      // Wheel spikes
      for (int i = 0; i < spikePos_.length; i++) {
        double position = spikePos_[i+ position_;
        if (position_ > Math.PI * 2position -= Math.PI * 2;

        Matrix4x4 m = new Matrix4x4();
        m.rotateZ (position);
        m.translate (x0, y0, 0);
      
        double[] xyz = {RADII, 0.00.0};
        xyz = m.transformPoint (xyz);
        spikes_[i].setGeometry (x0, y0, (intxyz[0](intxyz[1]);

        // On first spike add the axis
        if (i == 0) {
          int axisX0 = (intxyz[0];
          int axisY0 = (intxyz[1];
          int axisY1 = y0;

          int dy2 = (axisY1 - axisY0(axisY1 - axisY0);
          int dx = (intMath.sqrt (AXIS2_LENGTH * AXIS2_LENGTH - dy2);

          int axisX1 = axisX0 - dx;

          axis1_.setGeometry (axisX1, axisY1, axisX1 - AXIS1_LENGTH, axisY1);
          axis2_.setGeometry (axisX0, axisY0, axisX1, axisY1);
        }
      }

      GText text = axis2_.getText();
      text.setText ("position = " + Long.toString (Math.round (position_ * 180.0 / Math.PI)) ".0");
    }


    // Make a 6 degree turn
    public void rotate()
    {
      position_ += Math.PI / 60;
      if (position_ > Math.PI * 2position_ -= Math.PI * 2;
      draw();
    }
  }
  
  
  public static void main (String[] args)
  {
    new Demo9();
  }
}

           
       
G-SimpleAnimation.zip( 221 k)
Related examples in the same category
1. Custom move interaction, Switching interactions, Update world extent geometry and Scroll handlingCustom move interaction, Switching interactions, Update world extent geometry and Scroll handling
2. Scroll HandlingScroll Handling
3. Curve AnimationCurve Animation
4. ClockClock
w_w___w_.j_a__v_a__2__s__.__c__o__m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.