-
Notifications
You must be signed in to change notification settings - Fork 0
/
box_roatation.java
186 lines (160 loc) · 5.66 KB
/
box_roatation.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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package javafx_3d;
import javafx.animation.AnimationTimer;
import javafx.animation.PathTransition;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.Event;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.image.Image;
import javafx.scene.input.KeyEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Transform;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;
public class box_roatation extends Application {
int t=0;
private double anchorX=0;
private double anchorY=0;
private double anchrangleX=0;
private double anchrangleY=0;
private final DoubleProperty angleX=new SimpleDoubleProperty(0);
private final DoubleProperty angley=new SimpleDoubleProperty(0);
private final Box box=prepareBox();
private Box prepareBox() {
Box box=new Box(50,50,50);
PhongMaterial material=new PhongMaterial();
material.setDiffuseMap(new Image("file:///G:/untitled3/.jpg"));
material.setSpecularColor(Color.WHITE);
box.setMaterial(material);
return box;
}
@Override
public void start(Stage stage) throws Exception {
//camera
Camera camera=new PerspectiveCamera();
//box
my_group mg=new my_group();
mg.getChildren().addAll(prepareligh());
mg.getChildren().add(box);
mg.getChildren().add(new AmbientLight());
//translate
mg.translateXProperty().set(250);
mg.translateYProperty().set(250);
mg.translateZProperty().set(-500);
Sphere s=new Sphere(50);
s.translateXProperty().set(120);
s.translateYProperty().set(200);
//mg.getChildren().add(s);
//control
stage.addEventHandler(KeyEvent.KEY_PRESSED,keyEvent -> {
switch (keyEvent.getCode()){
case A:
mg.rotateByX(10);
break;
case B:
mg.rotateByX(-10);
break;
case C:
mg.rotateByY(10);
break;
case D:
mg.rotateByY(-10);
break;
}
});
//duration
Duration strt=Duration.ZERO;
Duration end=Duration.seconds(10);
//keyframe
//transition
Circle c=new Circle(150,150,100);
/*PathTransition transition=new PathTransition();
transition.setNode(box);
transition.setDuration(Duration.seconds(5));
transition.setPath(c);
transition.setCycleCount(PathTransition.INDEFINITE);
transition.play();*/
//scene
Group root=new Group();
root.getChildren().add(0,mg);
// root.getChildren().add(prepareligh());
Scene scene=new Scene(root,500,500);
scene.setFill(Color.AQUAMARINE);
scene.setCamera(camera);
//mouse control
initMouseControl(mg,scene);
//animation
animation();
//stage
stage.setScene(scene);
stage.show();
}
private final PointLight pointLight=new PointLight();
private Node[] prepareligh() {
pointLight.getTransforms().add(new Translate(0,-50,100));
pointLight.setColor(Color.RED);
pointLight.setRotationAxis(Rotate.X_AXIS);
Sphere sphere=new Sphere(10);
sphere.rotateProperty().bind(pointLight.rotateProperty());
sphere.rotationAxisProperty().bind(pointLight.rotationAxisProperty());
sphere.getTransforms().setAll(pointLight.getTransforms());
return new Node[]{pointLight,sphere};
}
private void animation() {
AnimationTimer timer=new AnimationTimer() {
@Override
public void handle(long l) {
//box.setRotationAxis(new Point3D(0,1,0));
//box.rotateProperty().set(box.getRotate()+1);
pointLight.setRotate(pointLight.getRotate()+1);
}
};
timer.start();
}
private void initMouseControl(my_group mg, Scene scene) {
Rotate xrotate;
Rotate yrotate;
mg.getTransforms().addAll(xrotate=new Rotate(0,Rotate.X_AXIS),
yrotate=new Rotate(0,Rotate.Y_AXIS));
xrotate.angleProperty().bind(angleX);
yrotate.angleProperty().bind(angley);
scene.setOnMousePressed(mouseEvent -> {
anchorX=mouseEvent.getSceneX();
anchorY=mouseEvent.getSceneY();
anchrangleX=angleX.get();
anchrangleY=angley.get();
System.out.println(anchrangleX+" "+anchrangleY);
});
scene.setOnMouseDragged(mouseEvent -> {
angleX.set(anchrangleX-(anchorY-mouseEvent.getSceneY()));
angley.set(anchrangleY+(anchorX-mouseEvent.getSceneX()));
});
}
public static void main(String[] args){
launch(args);
}
class my_group extends Group{
Rotate r;
Transform t=new Rotate();
public void rotateByX(int deg){
r=new Rotate(deg,Rotate.X_AXIS);
t=t.createConcatenation(r);
this.getTransforms().clear();
this.getTransforms().addAll(t);
}
public void rotateByY(int deg){
r=new Rotate(deg,Rotate.Y_AXIS);
t=t.createConcatenation(r);
this.getTransforms().clear();
this.getTransforms().addAll(t);
}
}
}