Reputation: 115
Please I want to draw a 3D graph using JavaFX 8 (3D). I already know some basics of 3D like draw sphere, firstly coloring the sphere and add shadow, then some light and initialisation of the sphere. My problem is I want to join the spheres by using a cylinder, but if there are for example 2 cylinders between two spheres it must be an arc or curved cylinder (I don't know if this is possible). I already tried that but nothing appears, even if something appears it's just a cylinder (not like a line just small).
Another problem, I want to know how the rotation can help in such a situation.
And last question, is it possible to make a scrollbar or just using event of Zoom? Thanks.
the picture taken from : Here
This is my code:
import java.util.ArrayList;
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.DrawMode;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
public class Graphe3D extends Application {
Group root;
PhongMaterial material;
ArrayList<Sphere> sphere;
@Override
public void start(Stage primaryStage) {
sphere=new ArrayList<>();
root=new Group();
material= new PhongMaterial();
for(int i=0;i<3;i++){
sphere.add(new Sphere(50));
//Sphere Color
material.setDiffuseColor(Color.RED);
//Shadow Color
material.setSpecularColor(Color.rgb(30, 30, 30));
//Init Sphere
sphere.get(i).setMaterial(material);
sphere.get(i).setTranslateX(new Random().nextInt(600));//set location X,Y and Z
sphere.get(i).setTranslateY(new Random().nextInt(600));
sphere.get(i).setTranslateZ(50); // ?
sphere.get(i).setDrawMode(DrawMode.FILL);
sphere.get(i).setCullFace(CullFace.BACK);// ?
//Create Light
PointLight pointLight = new PointLight(Color.ANTIQUEWHITE);
pointLight.setTranslateX(800);
pointLight.setTranslateY(-100);
pointLight.setTranslateZ(-1000);
root.getChildren().add(pointLight); //ajout de lumiere
root.getChildren().add(sphere.get(i)); //ajout des spheres au scene(root)
}
//Display
Scene scene = new Scene(root);
scene.setFill(Color.rgb(10, 10, 40));
scene.setCamera(new PerspectiveCamera(false));
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Upvotes: 3
Views: 1996
Reputation: 435
You can calculate the cylinder position and rotation using
Shape.getTransforms().add(new Rotate(angle, x, y, z, Rotate.Z_AXIS))
and
Math.toDegrees(Math.atan2(y, x))
Here's a sample working in 2D
Sphere var = null;
for(Sphere sphere: myListSphere {
if(var!=null) {
double x = sphere.getTranslateX()-var.getTranslateX();
double y = sphere.getTranslateY()-var.getTranslateY();
//the distance from each sphere
Cylinder cyl = new Cylinder(5, Math.sqrt((x*x)+(y*y)), 5);
cyl.setMaterial(this.redMaterial);
cyl.setTranslateX(sphere.getTranslateX()-(x/2));
cyl.setTranslateY(sphere.getTranslateY()-(y/2));
//the angle from both dots with Math.atan
cyl.getTransforms().add(new Rotate(90+Math.toDegrees(Math.atan2(y, x)), 0, 0, 0, Rotate.Z_AXIS));
}
var = shape;
}
Upvotes: 0
Reputation: 159486
The molecule sample application from Oracle does much of what you are asking. Read the linked tutorial and study the source code provided for the sample application.
Regarding a couple of your questions.
if there is for example 2 cylinder between two sphere it must an arc or curved cylinder
This is possible. You would have to generate a custom TriangleMesh rather than using the pre-built Cylinder class. Essentially, you need to create an elliptical Torus and only display an arc portion of the Torus between your two nodes. I will not provide detailed instructions on how to do this in the context of a StackOverflow answer.
I want to know how the rotation can help in such situation and last question is it possible to make a scrollbar or just using event of Zoom ?
Study the Molecule sample code linked earlier as that has rotation and zoom capability.
Upvotes: 6