トップページ > Java3D入門 > Java3Dでプリミティブを描画する(5)
このSphereクラスも、一番最初のJava3Dでグラフィックを扱う準備 のところで既に使っています。一応コンストラクタだけ示しておきます。
Sphere(float radius , Appearance ap)“radius”は球の半径です。Appearanceは前回までと全く同じです。
//******************************************************************************
//Java3D Sphere_test
//Sphereを表示
//******************************************************************************
//==============================================================================
//インポート・ファイル
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
import javax.vecmath.*;
import java.awt.*;
import javax.swing.*;
//==============================================================================
//メイン・クラス
public class Sphere_test
{
//=============================================================================
//メイン・メソッド
public static void main(String[] args)
{
Sphere_test test = new Sphere_test();
}
//=============================================================================
//コンストラクタ
public Sphere_test()
{
//============================================================================
//まずは、基礎フレームの設定。
//============================================================================
JFrame frame = new JFrame();
frame.setSize(250,250);
frame.setTitle("Sphere_test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel cp = new JPanel();
cp.setLayout(null);
frame.add(cp);
//============================================================================
//次にJava3D関係の設定。
//============================================================================
GraphicsConfiguration g_config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas = new Canvas3D(g_config);
canvas.setBounds(0,0,250,250);
cp.add(canvas);
//============================================================================
//空のSimpleUniverseを生成。
//============================================================================
SimpleUniverse universe = new SimpleUniverse(canvas);
frame.setVisible(true);
//============================================================================
//視点の設定
//============================================================================
ViewingPlatform camera = universe.getViewingPlatform();
camera.setNominalViewingTransform();
//============================================================================
//ライトの設定
//============================================================================
Color3f light_color = new Color3f(1.2f,1.2f,1.2f);
Vector3f light_direction = new Vector3f(0.4f,-0.4f,-0.9f);
DirectionalLight light = new DirectionalLight(light_color,light_direction);
BoundingSphere bounds = new BoundingSphere();
light.setInfluencingBounds(bounds);
BranchGroup group2 = new BranchGroup();
group2.addChild(light);
universe.addBranchGraph(group2);
//============================================================================
//プリミティブの設定をして、SimpleUniverseに追加
//============================================================================
Appearance appearance = new Appearance();
Material material = new Material();
//DiffuseColorを設定します。引数は順に赤、緑、青です。今回は青っぽい。
material.setDiffuseColor(0.2f,0.6f,1.0f);
appearance.setMaterial(material);
BranchGroup group1 = new BranchGroup();
//球を生成します。
Sphere sphere = new Sphere(0.3f,appearance);
group1.addChild(sphere);
universe.addBranchGraph(group1);
}
}
//ソースコードここまで。
//******************************************************************************
球です。さすがに飽きるので、今回はライトを当てる角度を少し変えています。