Scroll Handling : 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 




Scroll Handling
Scroll Handling

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

import javax.swing.*;

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



/**
 * G demo program. Demonstrates:
 *
 * <ul>
 * <li>Custom move interaction
 * <li>Switching interactions
 * <li>Update world extent geometry
 * <li>Scroll handling
 * </ul>
 
 @author <a href="mailto:[email protected]">Jacob Dreyer</a>
 */   
public class Demo11 extends JFrame
  implements ActionListener, GInteraction
{
  private JButton   zoomButton_;
  private JButton   moveButton_;
  private GWindow   window_;
  private GSegment  interactionSegment_;
  private int       x0_, y0_;

  
  public Demo11()
  {
    super ("G Graphics Library - Demo 11");    
    setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    getContentPane().setLayout (new BorderLayout());
    
    // Create the GUI
    JScrollBar hScrollBar = new JScrollBar (JScrollBar.HORIZONTAL);
    getContentPane().add (hScrollBar, BorderLayout.SOUTH);

    JScrollBar vScrollBar = new JScrollBar (JScrollBar.VERTICAL);
    getContentPane().add (vScrollBar, BorderLayout.EAST);

    JPanel buttonPanel = new JPanel();
    zoomButton_ = new JButton ("Zoom");
    zoomButton_.addActionListener (this);
    buttonPanel.add (zoomButton_);
    
    moveButton_ = new JButton ("Move");
    moveButton_.addActionListener (this);
    buttonPanel.add (moveButton_);    
    getContentPane().add (buttonPanel, BorderLayout.NORTH);
    
    // Create the graphic canvas
    window_ = new GWindow();
    getContentPane().add (window_.getCanvas(), BorderLayout.CENTER);
    
    // Create scane with default viewport and world extent settings
    GScene scene = new GScene (window_);

    // Use a normalized world extent
    double w0[] {0.00.00.0};
    double w1[] {1.00.00.0};
    double w2[] {0.01.00.0};
    scene.setWorldExtent (w0, w1, w2);
    
    // Create a graphic object
    GObject object = new TestObject();
    scene.add (object);

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

    window_.startInteraction (new ZoomInteraction (scene));

    scene.shouldWorldExtentFitViewport (false);
    scene.shouldZoomOnResize (false);    
    scene.installScrollHandler (hScrollBar, vScrollBar);
  }


  public void actionPerformed (ActionEvent event)
  {
    if (event.getSource() == zoomButton_)
      window_.startInteraction (new ZoomInteraction (window_.getScene()));
    else
      window_.startInteraction (this);
  }


  
  // Move interaction
  public void event (GScene scene, int event, int x, int y)
  {
    switch (event) {
      case GWindow.BUTTON1_DOWN :
        interactionSegment_ = scene.findSegment (x, y);
        x0_ = x;
        y0_ = y;
        break;
        
      case GWindow.BUTTON1_DRAG :
        int dx = x - x0_;
        int dy = y - y0_;
        if (interactionSegment_ != null) {
          TestObject testObject = (TestObjectinteractionSegment_.getOwner();
          testObject.translate (interactionSegment_, dx, dy);
          scene.refresh();
        }
        x0_ = x;
        y0_ = y;
        break;

      case GWindow.BUTTON1_UP :
        interactionSegment_ = null;
        break;
    }
  }
  
  
  
  /**
   * Defines the geometry and presentation for a sample graphic object.
   */   
  private class TestObject extends GObject
  {
    private GSegment[] stars_;
    private double[][] geometry_;
    
    
    TestObject()
    {
      int nStars = 20;

      stars_    = new GSegment[nStars];
      geometry_ = new double[nStars][];
      
      for (int i = 0; i < nStars; i++) {
        stars_[inew GSegment();
        stars_[i].setUserData (new Integer(i));
        addSegment (stars_[i]);
        
        double[] xy = Geometry.createStar (Math.random(), Math.random(),
                                           0.050.120);
        geometry_[i= xy;

        GStyle style = new GStyle();
        style.setForegroundColor (new Color ((floatMath.random(),
                                             (floatMath.random(),
                                             (floatMath.random()));
        
        style.setBackgroundColor (new Color ((floatMath.random(),
                                             (floatMath.random(),
                                             (floatMath.random()));
        style.setLineWidth (2);
        stars_[i].setStyle (style);
      }
    }


    // Convert the world extent geometry of the specified
    // segment according to specified device translation
    public void translate (GSegment segment, int dx, int dy)
    {
      GTransformer transformer = getTransformer();
      double[] dw0 = transformer.deviceToWorld (0,  0);
      double[] dw1 = transformer.deviceToWorld (dx, dy);      

      int index = ((Integersegment.getUserData()).intValue();
      double[] geometry = geometry_[index];
      for (int i = 0; i < geometry.length; i += 2) {
        geometry[i + 0+= dw1[0- dw0[0];
        geometry[i + 1+= dw1[1- dw0[1];        
      }

      segment.setGeometryXy (geometry);              
    }

  
    public void draw()
    {
      for (int i = 0; i < stars_.length; i++)
        stars_[i].setGeometryXy (geometry_[i]);        
    }
  }
  


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

           
       














G-Scroll-Handling.zip( 192 k)
Related examples in the same category
1.Animated Graphics
2.Simple animation techniqueSimple animation technique
3.Custom move interaction, Switching interactions, Update world extent geometry and Scroll handlingCustom move interaction, Switching interactions, Update world extent geometry and Scroll 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.