How do I make a cube in javafx that have 6 different textures on each side of the cube?
The only way I know how to solve this, is by making 6 imageviews and give them each a specific coordinates that make them look like a cube.
Is there a other way to do this?
Like, I like to give the cube later on lighting, and some rotations. It will be more complex if I use this method.

Btw, I heard about Triangle Mesh, but I don't know how TextureCoords works.

handy links:
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/TriangleMesh.html
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Box.html

Recommended Answers

All 7 Replies

I researched this a bit with "a cube in javafx that have 6 different textures on each side of the cube" using the usual search engine and it appears that there are a lot of prior discussions but you want "6 different textures on each side" which to me reads as one side would have 6 textures and since a cube has 6 sides you have some 36 textures overall. To get to that you would create your image that has those 6 textures for each of the 6 sides then all that image to the side of choice.

Remember that given all the prior work on such an assignment (do tell if this is your homework assignment) I won't get into the actual code.

commented: this is not a homework assignment, and btw, I like to have 6 textures on 6 sides of a cube. +1

I made a mistake in the discussion.
I want each texure on each side of the cube, not 6 textures on each side of the cube.
I am really sorry.

commented: "Good news everyone!" It appears there are many priors to explore with 1 texture per side! (Zapp Brannigan!) +15

this is the code, what I have right now:

This is the first main class.

package com.vinayak.trianglemesh.experiment;

import java.time.Duration;

import org.reactfx.util.FxTimer;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.geometry.Rectangle2D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class CoreCube extends Application {

    public static final String PROGRAM_TITLE = "Mooie Kubus Experiment";

    public static Rectangle2D primaryScreenBounds = Screen.getPrimary().getBounds();
    public static double monitorWidth = (primaryScreenBounds.getWidth());
    public static double monitorHeight = (primaryScreenBounds.getHeight());
    public static double sceneWidth = (monitorWidth / 1.5);
    public static double sceneHeight = (monitorHeight / 1.5);
    public static boolean isFullScreen = false;
    public static boolean isConsoleSpamOn = false;

    public static Group group;
    public static Scene scene;
    public static Stage window;

    public static int fps = 60;
    public static int frame = 0;

    public static Camera camera;

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        group = new Group();
        scene = new Scene(group, sceneWidth, sceneHeight, true);
        window = primaryStage;
        window.setScene(scene);
        window.setTitle(PROGRAM_TITLE);
        window.show();

        Cube.makeCube();

        cameraStuff();
    }

    public static void cameraStuff() {
        camera = new PerspectiveCamera();
        scene.setCamera(camera);

        FxTimer.runPeriodically(Duration.ofMillis((long) (1000 / 60)), () -> {
            for (int i = 0; i < Cube.rect.length; i++) {
                Cube.rect[i].setTranslateX(frame);
                Cube.rect[i].setTranslateY(frame);
                Cube.rect[i].setTranslateZ(frame);
            }
            frame++;
        });
    }
}

And this is the second class:

package com.vinayak.trianglemesh.experiment;

import javafx.geometry.Point3D;
import javafx.scene.paint.Color;
import javafx.scene.shape.Box;
import javafx.scene.shape.Rectangle;

public class Cube extends CoreCube {

    public static Rectangle rect[];

    public static void makeCube() {

        rect = new Rectangle[6];

        rect[0] = new Rectangle(0, 0, 200, 200);
        rect[0].setFill(Color.CORAL);
        rect[0].setTranslateZ(-100);
        rect[0].setRotationAxis(new Point3D(0.0, 0.0, 0.0));
        rect[0].setRotate(0);
        group.getChildren().add(rect[0]);

        rect[1] = new Rectangle(0, 0, 200, 200);
        rect[1].setFill(Color.DARKGREEN);
        rect[1].setTranslateZ(100);
        rect[1].setRotationAxis(new Point3D(0.0, 0.0, 0.0));
        rect[1].setRotate(0);
        group.getChildren().add(rect[1]);

        rect[2] = new Rectangle(100, 0, 200, 200);
        rect[2].setFill(Color.YELLOW);
        rect[2].setTranslateZ(0);
        rect[2].setRotationAxis(new Point3D(0.0, 1.0, 0.0));
        rect[2].setRotate(90);
        group.getChildren().add(rect[2]);

        rect[3] = new Rectangle(-100, 0, 200, 200);
        rect[3].setFill(Color.DARKRED);
        rect[3].setTranslateZ(0);
        rect[3].setRotationAxis(new Point3D(0.0, 1.0, 0.0));
        rect[3].setRotate(90);
        group.getChildren().add(rect[3]);

        rect[4] = new Rectangle(0, 100, 200, 200);
        rect[4].setFill(Color.BLUE);
        rect[4].setTranslateZ(0);
        rect[4].setRotationAxis(new Point3D(1.0, 0.0, 0.0));
        rect[4].setRotate(90);
        group.getChildren().add(rect[4]);

        rect[5] = new Rectangle(0, -100, 200, 200);
        rect[5].setFill(Color.BLUEVIOLET);
        rect[5].setTranslateZ(0);
        rect[5].setRotationAxis(new Point3D(1.0, 0.0, 0.0));
        rect[5].setRotate(90);
        group.getChildren().add(rect[5]);
    }
}

I know, this are not ImageViews, but Rectangles.
But it works the same way, if I replace those rectangles to imageviews.
I just have to make a string for the file path, then the image object, then add the image inside the imageview.

If you are wondering, what "FxTimer" is, it is from ReactFX library, with this I can make a Timeline, that loops 60 times per second.
this part of code is not really important. It was just to test if the cube can move. Like if the 6 sides of the cube can even move.

I don't have the code, but you can check out the download link of that website, there is some source code that can guide you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.