月別アーカイブ: 2015年6月

超入門:立体の表示,回転と平行移動,カメラ,光源の基本

超入門:立体の表示,回転と平行移動,カメラ,光源の基本
 

import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.stage.Stage;
import javafx.scene.shape.*;
import javafx.scene.transform.*;
import javafx.geometry.Point3D;

public class FX3Dsample01 extends Application {

	@Override
	public void start(Stage Stage) {
		//--- Top Node and Scene ---
		Group root = new Group();
		Scene scene = new Scene(root, 1024, 768, Color.rgb(0,0,0));

		// Axis for Rotation
		Point3D aX = new Point3D(100,0,0);
		Point3D aY = new Point3D(0,100,0);
		Point3D aZ = new Point3D(0,0,100);

		//--- Solid Model Generation ---
		// (Box)
		Box bx1 = new Box(300d,200d,150d);
		root.getChildren().add(bx1);
		PhongMaterial mt1 = new PhongMaterial(); // 別々に作る!
		mt1.setDiffuseColor(Color.rgb(255,0,0));
		bx1.setMaterial(mt1);
		bx1.getTransforms().addAll(
			new Translate(-300d,0d,0d), // 先に平行移動
			new Rotate(30,aX),
			new Rotate(30,aY),
			new Rotate(20,aZ)
		);
		// (Cylinder)
		Cylinder cl1 = new Cylinder(80d,300d);
		root.getChildren().add(cl1);
		PhongMaterial mt2 = new PhongMaterial(); // 別々に作る!
		mt2.setDiffuseColor(Color.rgb(0,255,0));
		cl1.setMaterial(mt2);
		cl1.getTransforms().addAll(
			new Rotate(30,aX),
			new Rotate(0,aY),
			new Rotate(-20,aZ)
		);
		// (Sphere)
		Sphere sp1 = new Sphere(140d);
		root.getChildren().add(sp1);
		PhongMaterial mt3 = new PhongMaterial(); // 別々に作る!
		mt3.setDiffuseColor(Color.rgb(0,0,255));
		sp1.setMaterial(mt3);
		sp1.getTransforms().addAll(
			new Translate(280d,0d,0d)
		);

		//--- Light Setting ---
		AmbientLight aLight = new AmbientLight(Color.rgb(127, 127, 127));
		root.getChildren().add(aLight);

		PointLight pLight = new PointLight(Color.rgb(255,255,255));
		pLight.setTranslateX(500d);
		pLight.setTranslateY(-300d);
		pLight.setTranslateZ(-200d);
		root.getChildren().add(pLight);

		//--- Camera Setting ---
		ParallelCamera cmr = new ParallelCamera();
		cmr.getTransforms().addAll(
			new Translate(-512d,-384d,0d)
		);
		scene.setCamera(cmr);

		//--- Window Activation ---
		Stage.setTitle("FX3Dsample01");
		Stage.setScene(scene);
		Stage.show();
	}

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

 
FX3Dsample01_exe

FXMLアプリケーション構築例(NetBeans IDE & Java FX Scene Builder)

マニュアルをまとめつつあります.)
 
 ここでは,NetBeansIDEとJava FX Scene Builerを用いた画像ビューワのアプリ構築の例を紹介します.
 
1. 新規プロジェクトの作成(FXMLappl01)
FXMLappl01
 
「Java FX FXMLアプリケーション」を選択します.
FXMLappl02
 
「プロジェクト名」と「FXML名」を設定します.
FXMLappl03
 
空のFXMLを追加します.
FXMLappl04
 
FXMLファイルの名前を指定します.
FXMLappl05
 
ここでは既存のFXMLコントローラを使うことにします.
FXMLappl06
 
FXMLのコード:FXML1.fxml









  

 
FXMLのコード:FXML2.fxml










  

 
ファイル:FXMLappl01.java

package fxmlappl01;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class FXMLappl01 extends Application {
    
    public static Stage stage2;
    
    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXML1.fxml"));
        
        Scene scene = new Scene(root);

        stage.setTitle("Main Window");
        stage.setScene(scene);
        stage.show();
        
        
        /* Another Window */
        stage2 = new Stage();
        stage2.initOwner(stage);
        
        Parent root2 = FXMLLoader.load(getClass().getResource("FXML2.fxml"));
        
        Scene scene2 = new Scene(root2);
        
        stage2.setTitle("Picture Window");
        stage2.setScene(scene2);
        stage2.show();
        
        /* image from "http://www.ashinari.com/" */
        Share.img1 = new Image(getClass().getResourceAsStream("1.jpg"));
        Share.img2 = new Image(getClass().getResourceAsStream("2.jpg"));
    }

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

 
ファイル:Share.java

package fxmlappl01;

import javafx.scene.image.*;

public class Share {
    public static Image img1, img2;
}

 
ファイル:FXML1Controller.java

package fxmlappl01;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.*;

public class FXML1Controller implements Initializable {
    
    @FXML
    private Button btn1, btn21, bnt22, btn23;
    @FXML
    private ImageView iv1;
    
    @FXML
    private void handleButton_btn1(ActionEvent ev) {
        System.out.println("Open Another Window!");
        FXMLappl01.stage2.show();
    }
    
    @FXML
    private void handleButton_btn21(ActionEvent ev) {
        iv1.setImage(Share.img1);
    }
    
    @FXML
    private void handleButton_btn22(ActionEvent ev) {
        iv1.setImage(Share.img2);
    }
    
    @FXML
    private void handleButton_btn23(ActionEvent ev) {
        iv1.setImage(null);
    }
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    
    
}

 
このアプリを実行したところ:
FXMLappl01_exe
 
プロジェクト全体(Zip圧縮):FXMLappl01.zip