create JavaFX Capsule - Java javafx.scene.shape

Java examples for javafx.scene.shape:TriangleMesh

Description

create JavaFX Capsule

Demo Code


//package com.java2s;
import javafx.scene.Group;

import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;

public class Main {
    private final static int POINT_SIZE = 3;
    private final static int TEXCOORD_SIZE = 2;
    private final static int FACE_SIZE = 6;

    public static Group createCapsule(float radius, float height) {
        Group root = new Group();
        final int sphereDivisions = correctDivisions(64);
        int halfDivisions = sphereDivisions / 2;
        int numPoints = sphereDivisions * (halfDivisions - 1) + 2;
        int numTexCoords = (sphereDivisions + 1) * (halfDivisions - 1)
                + sphereDivisions * 2;/*from ww  w . j  a  v  a 2  s.co m*/
        int numFaces = sphereDivisions * (halfDivisions - 2) * 2
                + sphereDivisions * 2;
        float hdTheta, tdTheta, x, y, z;
        float fDivisions = 1.0F / sphereDivisions;

        float[] points = new float[numPoints * POINT_SIZE], texCoords = new float[numTexCoords
                * TEXCOORD_SIZE];
        int[] faces = new int[numFaces * FACE_SIZE];

        //loop for 1/2 sphere
        for (int i = 0; i < halfDivisions; i++) {
            hdTheta = (float) fDivisions * (i + 1 - halfDivisions / 2)
                    * 2.0F * 3.141593F;
            float hdX = (float) Math.cos(hdTheta), hdY = (float) Math
                    .sin(hdTheta);

            for (int j = 0; j < halfDivisions; j++) {
                tdTheta = fDivisions * j * 2.0F * 3.141593F;
                x = (float) Math.sin(tdTheta) * hdY * radius;
                y = (float) Math.cos(tdTheta) * hdY * radius;
                z = hdX * radius;
                if (j != 0) {
                    //                    root.getChildren().add(new Vertex(x,y,z));
                } else {
                    //                   root.getChildren().add(new Vertex(x,y,z, Color.BLUE));
                }
            }

        }

        TriangleMesh mesh = new TriangleMesh();
        mesh.getPoints().addAll(points);
        mesh.getTexCoords().addAll(texCoords);
        mesh.getFaces().addAll(faces);

        MeshView mv = new MeshView(mesh);

        root.getChildren().add(mv);
        return root;
    }

    private static int correctDivisions(int paramInt) {
        return (paramInt + 3) / 4 * 4;
    }
}

Related Tutorials