Simple animation technique : Animation « Advanced Graphics « Java

Home
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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Advanced Graphics » Animation 




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.Animated Graphics
2.Custom move interaction, Switching interactions, Update world extent geometry and Scroll handlingCustom move interaction, Switching interactions, Update world extent geometry and Scroll handling
3.Scroll HandlingScroll Handling
4.Curve AnimationCurve Animation
5.ClockClock
6.Moving Button
7.Moving Button Container
8.Smooth Moves
9.Background Animation
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.