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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="UTF-8"?>
 
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
 
<AnchorPane id="AnchorPane" prefHeight="92.0" prefWidth="243.0"
 xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
 fx:controller="fxmlappl01.FXML1Controller">
<children>
  <Button fx:id="btn1" layoutX="36.0" layoutY="33.0" mnemonicParsing="false"
   onAction="#handleButton_btn1" prefHeight="26.0" prefWidth="172.0"
   text="Open Another Window" />
</children>
</AnchorPane>

 
FXMLのコード:FXML2.fxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
 
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
 
<AnchorPane id="AnchorPane" prefHeight="482.0" prefWidth="600.0"
 xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"
 fx:controller="fxmlappl01.FXML1Controller">
<children>
  <Button fx:id="btn21" layoutX="14.176322937011719" layoutY="1.0"
   mnemonicParsing="false" onAction="#handleButton_btn21" text="image 1" />
  <Button fx:id="btn22" layoutX="88.17632293701172" layoutY="1.0"
   mnemonicParsing="false" onAction="#handleButton_btn22" text="image 2" />
  <Button fx:id="btn23" layoutX="163.17632293701172" layoutY="1.0"
   mnemonicParsing="false" onAction="#handleButton_btn23" text="erase" />
  <ImageView fx:id="iv1" fitHeight="437.0" fitWidth="574.0" layoutX="14.0"
   layoutY="35.0" pickOnBounds="true" preserveRatio="true" />
</children></AnchorPane>

 
ファイル:FXMLappl01.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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

1
2
3
4
5
6
7
package fxmlappl01;
 
import javafx.scene.image.*;
 
public class Share {
    public static Image img1, img2;
}

 
ファイル:FXML1Controller.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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