ScaleInterpolatorクラス

広告

拡大縮小を行うScaleInterpolatorクラスについて見ていきます。クラス定義は下記のようになっています。

  • java.lang.Object
  • javax.media.j3d.SceneGraphObject
  • javax.media.j3d.Node
  • javax.media.j3d.Leaf
  • javax.media.j3d.Behavior
  • javax.media.j3d.Interpolator
  • javax.media.j3d.TransformInterpolator
  • javax.media.j3d.ScaleInterpolator
  • public class ScaleInterpolator extends TransformInterpolator

コンストラクタは2つ用意されています。

コンストラクタ
ScaleInterpolator(Alpha alpha, TransformGroup target)
Constructs a trivial scale interpolator that varies its target TransformGroup node between the two specified alpha values using the specified alpha, an identity matrix, a minimum scale = 0.1f, and a maximum scale = 1.0f.
ScaleInterpolator(Alpha alpha, TransformGroup target, Transform3D axisOfTransform, float minimumScale, float maximumScale)
Constructs a new scaleInterpolator object that varies its target TransformGroup node's scale component between two scale values (minimumScale and maximumScale).

2番目のコンストラクタを見てみます。

ScaleInterpolator
public ScaleInterpolator(Alpha alpha,
                         TransformGroup target,
                         Transform3D axisOfTransform,
                         float minimumScale,
                         float maximumScale)
Constructs a new scaleInterpolator object that varies its target 
TransformGroup node's scale component between two scale values 
(minimumScale and maximumScale). 

Parameters:
  alpha - the alpha object for this interpolator
  target - the TransformGroup node affected by this interpolator
  axisOfTransform - the transform that defines the local coordinate
    system in which this interpolator operates; the scale is done 
    about the origin of this local coordinate system.
  minimumScale - the starting scale
  maximumScale - the ending scale

引数としては、アルファ値、対象となるTransformGroupオブジェクト、拡大縮小の中心点を変換するためのTransform3Dオブジェクト、開始倍率、終了倍率、となっています。

拡大縮小の場合はデフォルトで中心点が原点(0, 0, 0)です。中心を変更したい場合には、引数で指定するTransform3Dクラスのオブジェクトを使って中心点を移動して下さい。

開始倍率と終了倍率、アルファ値が0.0と1.0のときの拡大縮小率を指定します。

サンプルプログラム

実際にサンプルを試してみましょう。

InterpolatorTest5

import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.geometry.Cone;
import javax.vecmath.*;

public class InterpolatorTest5 extends JFrame {
  public BranchGroup createSceneGraph() {
    BranchGroup objRoot = new BranchGroup();

    /* 光源を設置する */
    objRoot.addChild(createLight());

    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    Alpha alpha = new Alpha(
           -1,  
           Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE,
           0, 
           0, 
           3000, 
           400, 
           0, 
           3000, 
           400, 
           0
   );

    Transform3D axis = new Transform3D();

    ScaleInterpolator scale = 
      new ScaleInterpolator(alpha, objTrans, axis, 0.5f, 1.5f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
    scale.setSchedulingBounds(bounds);
    objTrans.addChild(scale);

    Appearance ap = new Appearance();
    Material ma = new Material();
    ma.setDiffuseColor(0.0f, 0.0f, 1.0f);
    ap.setMaterial(ma);

    Cone cone = new Cone( 0.3f, 0.7f, Cone.GENERATE_NORMALS, 50, 1, ap);

    objTrans.addChild(cone);
    objRoot.compile();

    return objRoot;
  }

  private Light createLight(){
    DirectionalLight light = new DirectionalLight( true,
                             new Color3f(0.0f, 0.0f, 1.0f),
                             new Vector3f(0.0f, 0.0f, -1.0f));

    light.setInfluencingBounds(new BoundingSphere(new Point3d(), 100.0));
  
    return light;
  }

  public InterpolatorTest5() {
    getContentPane().setLayout(new BorderLayout());

    GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();

    Canvas3D canvas = new Canvas3D(config);
    getContentPane().add(canvas, BorderLayout.CENTER);

    BranchGroup scene = createSceneGraph();
    SimpleUniverse universe = new SimpleUniverse(canvas);

    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(scene);
  }

  public static void main(String[] args) {
    InterpolatorTest5 sample = new InterpolatorTest5();

    sample.setBounds( 10, 10, 240, 240);
    sample.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    sample.setVisible(true);
  }
}

実際に実行した結果は下記のようになります。

ScaleInterpolatorのテスト

ScaleInterpolatorのテスト

ScaleInterpolatorのテスト

上記の場合は、中心点を原点のままにしていますので、同じ位置で拡大縮小を繰り返します。

( Written by Tatsuo Ikura+ )

Facebook Page