I'm trying to set the background of a SubScene as an image, but when I do, this happens:
This is the code:
public void start(Stage stage) throws Exception {
PerspectiveCamera camera = new PerspectiveCamera(true);
Group model = new Group(new Box(200, 10, 10));
Group course = new Group(model);
BorderPane borderPane = new BorderPane();
Pane pane1 = new Pane(course);
SubScene subScene1 = new SubScene(pane1, 1000, 720);
Group homeCameraXform = new Group();
model.getChildren().add(homeCameraXform);
homeCameraXform.getChildren().add(camera);
subScene1.setCamera(camera);
camera.setNearClip(.1);
camera.setFarClip(10000);
camera.setTranslateZ(-1000);
Xform s = new Xform();
s.getChildren().add(camera);
subScene1.setOnMouseDragged(e -> s.rx.setAngle(s.rx.getAngle()+ 10));
borderPane.setLeft(subScene1);
subScene1.setFill(new ImagePattern(new Image("file.jpg")));
Scene scene = new Scene(borderPane, 1280,720, true);
stage.setScene(scene);
stage.show();
}
For a normal Scene with a depthBuffer, I just needed to do scene.setFill(new ImagePattern(new Image("file"))). When I try setFill(Color.RED) instead of setFill(new ImagePattern(new Image(""))), it works as expected. What else do I have to do to setFill with an ImagePattern?
Using a skydome as 3d background
This is a skydome :
A 3d mesh usually made for realtime rendering. Its normal are pointing inside to see its texture from inside .
This is a single javafx app with two files you can test.
( you may notice that is the skydome is rotating not the camera . Camera motion is not implemented in this example)
App.java
public class App extends Application {
#Override
public void start(Stage stage) throws IOException {
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateY(-1.3);
camera.setTranslateZ(-1.5);
Shape3D cube = new Box(0.1, 0.1, 0.1);
cube.setTranslateY(-1.3);
Rotate rotate = new Rotate();
rotate.setAxis(Rotate.Y_AXIS);
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(3 * 10),
new KeyValue(rotate.angleProperty(), 360 * 10)));
MeshView skyDome = getSkyDome();
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(new Image("/space.jpg"));
skyDome.setMaterial(material);
Group group3d = new Group(camera, skyDome, cube);
Scene scene = new Scene(group3d, 640, 480, true, SceneAntialiasing.BALANCED);
scene.setCamera(camera);
stage.setScene(scene);
stage.show();
skyDome.getTransforms().setAll(rotate);
timeline.play();
}
public static void main(String[] args) {
launch();
}
private MeshView getSkyDome() throws IOException {
FXMLLoader loader = new FXMLLoader(App.class.getResource("/skydome.fxml"));
loader.load();
return (MeshView) loader.getRoot();
}
}
skidome.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<MeshView id="default">
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.shape.*?>
<MeshView id="default">
<mesh>
<TriangleMesh>
<points>
-0.094396, -2.432509, 0.474559, 0.094396, -2.432509, 0.474559, 0.0, -2.480165, 0.0, -0.342138, -1.753742, 1.720044, 0.342138, -1.753742, 1.720044, 0.268816, -2.062182, 1.35143, -0.268815, -2.062182, 1.351428, -0.402311, -1.377906, 2.022556, 0.402312, -1.377906, 2.022558, -0.447025, -0.949118, 2.247345, 0.447025, -0.949118, 2.247346, -0.474558, -0.483856, 2.385768, 0.474559, -0.483856, 2.385769, 0.268816, -2.432509, 0.402312, 0.185164, -2.291374, 0.930881, 0.527302, -2.291374, 0.789163, -0.185164, -2.291374, 0.930881, 0.789163, -2.291374, 0.527302, 0.402312, -2.432509, 0.268816, 0.765523, -2.062182, 1.145687, 0.974327, -1.753742, 1.458183, 1.145687, -1.377906, 1.714641, 1.273019, -0.949118, 1.905208, 1.35143, -0.483856, 2.022558, 0.483856, 0.0, 2.432509, 1.377906, 0.0, 2.062182, 0.474559, -2.432509, 0.094396, 0.402312, -2.432509, -0.268816, 0.268816, -2.432509, -0.402312, 1.145687, -2.062182, 0.765523, 1.458183, -1.753742, 0.974327, 1.714641, -1.377906, 1.145687, 1.905207, -0.949118, 1.273019, 2.022558, -0.483856, 1.35143, 2.062182, 0.0, 1.377905, 1.35143, -2.062182, 0.268816, 0.930881, -2.291374, 0.185164, 1.720044, -1.753742, 0.342138, 2.022557, -1.377906, 0.402312, 2.247345, -0.949118, 0.447025, 2.385769, -0.483856, 0.474559, 2.432509, 0.0, 0.483856, 0.789163, -2.291374, -0.527302, 0.527302, -2.291374, -0.789163, 1.35143, -2.062182, -0.268816, 0.930881, -2.291374, -0.185164, 1.720044, -1.753742, -0.342138, 2.022557, -1.377906, -0.402312, 2.247345, -0.949118, -0.447025, 2.385769, -0.483856, -0.474559, 2.432509, 0.0, -0.483856, 0.474559, -2.432509, -0.094396, 1.458183, -1.753742, -0.974326, 1.145687, -2.062182, -0.765523, 1.714641, -1.377906, -1.145687, 1.905207, -0.949118, -1.273019, 2.022557, -0.483856, -1.35143, 2.062181, 0.0, -1.377906, -0.483855, 0.0, 2.432507, 0.974326, -1.753742, -1.458183, 0.765523, -2.062182, -1.145687, 1.145686, -1.377906, -1.714641, 1.273019, -0.949118, -1.905207, 1.351429, -0.483856, -2.022557, 1.377905, 0.0, -2.062181, 0.185164, -2.291374, -0.930881, 0.094396, -2.432509, -0.474559, 0.402311, -1.377906, -2.022557, 0.342138, -1.753742, -1.720044, 0.447025, -0.949118, -2.247345, 0.474559, -0.483856, -2.385769, 0.483855, 0.0, -2.432508, -0.185164, -2.291374, -0.930881, -0.094396, -2.432509, -0.474559, 0.268816, -2.062182, -1.351429, -0.447025, -0.949118, -2.247345, -0.402312, -1.377906, -2.022557, -0.474559, -0.483856, -2.385769, -0.483857, 0.0, -2.432508, -0.527302, -2.291374, -0.789163, -0.268816, -2.432509, -0.402311, -0.268816, -2.062182, -1.351429, -0.342138, -1.753742, -1.720044, -1.273019, -0.949118, -1.905207, -1.145687, -1.377906, -1.714641, -1.35143, -0.483856, -2.022557, -1.377906, 0.0, -2.06218, -0.789163, -2.291374, -0.527301, -0.402311, -2.432509, -0.268816, -0.765523, -2.062182, -1.145686, -0.974326, -1.753742, -1.458183, -2.022557, -0.483856, -1.351429, -1.905207, -0.949118, -1.273019, -2.062181, 0.0, -1.377904, -0.930881, -2.291374, -0.185164, -0.474559, -2.432509, -0.094396, -1.145686, -2.062182, -0.765523, -1.458183, -1.753742, -0.974326, -1.714641, -1.377906, -1.145686, -2.385769, -0.483856, -0.474558, -2.247345, -0.949118, -0.447025, -2.432508, 0.0, -0.483855, -0.930881, -2.291374, 0.185164, -0.474559, -2.432509, 0.094396, -1.351429, -2.062182, -0.268816, -1.720044, -1.753742, -0.342138, -2.022557, -1.377906, -0.402311, -2.432507, 0.0, 0.483857, -2.385768, -0.483856, 0.47456, -0.789162, -2.291374, 0.527302, -0.402311, -2.432509, 0.268816, -1.351429, -2.062182, 0.268816, -1.720044, -1.753742, 0.342138, -2.022557, -1.377906, 0.402312, -2.247345, -0.949118, 0.447025, -0.527301, -2.291374, 0.789163, -0.268816, -2.432509, 0.402311, -1.145686, -2.062182, 0.765523, -1.458182, -1.753742, 0.974326, -1.71464, -1.377906, 1.145687, -1.905207, -0.949118, 1.273019, -2.022556, -0.483856, 1.35143, -2.062179, 0.0, 1.377906, -0.765522, -2.062182, 1.145686, -0.974326, -1.753742, 1.458183, -1.145686, -1.377906, 1.714641, -1.273018, -0.949118, 1.905207, -1.351429, -0.483856, 2.022557, -1.377903, 0.0, 2.06218
</points>
<texCoords>
0.474147, 0.554411, 0.459173, 0.542211, 0.497469, 0.510688, 0.401617, 0.69249904, 0.339297, 0.642836, 0.381025, 0.607478, 0.426818, 0.644186, 0.374268, 0.745052, 0.294258, 0.681365, 0.344075, 0.803576, 0.244831, 0.72359, 0.310262, 0.86816204, 0.188605, 0.77230597, 0.450007, 0.52519, 0.420663, 0.57419896, 0.402144, 0.54010296, 0.450793, 0.598541, 0.398004, 0.501432, 0.448031, 0.505932, 0.352741, 0.55584896, 0.300594, 0.57283497, 0.244277, 0.59156096, 0.182, 0.612661, 0.111153, 0.637178, 0.123258, 0.82906103, 0.027992, 0.666368, 0.453578, 0.48735303, 0.482963, 0.463072, 0.502329, 0.46113402, 0.346159, 0.49715102, 0.291204, 0.49302697, 0.231585, 0.488998, 0.165286, 0.484999, 0.089498, 0.480954, 1.0E-4, 0.47660702, 0.362536, 0.440125, 0.408982, 0.464001, 0.313039, 0.415165, 0.259036, 0.38845903, 0.198627, 0.359159, 0.129173, 0.32611, 0.046856, 0.28750902, 0.468006, 0.41481602, 0.507085, 0.41079903, 0.399699, 0.393515, 0.43354, 0.43354303, 0.363408, 0.351206, 0.323555, 0.30540198, 0.278658, 0.25451702, 0.226602, 0.196352, 0.164136, 0.127671, 0.465839, 0.47228998, 0.434986, 0.311454, 0.452158, 0.364707, 0.415973, 0.253478, 0.394455, 0.188712, 0.369528, 0.11443597, 0.339115, 0.022300005, 0.268737, 0.947176, 0.51661, 0.30255198, 0.511808, 0.35838902, 0.521646, 0.24165797, 0.527191, 0.17353898, 0.533629, 0.09482598, 0.542525, 0.0026819706, 0.544709, 0.422158, 0.520958, 0.466788, 0.623624, 0.271716, 0.595337, 0.32599097, 0.655278, 0.21057498, 0.691454, 0.14150602, 0.733968, 0.058188975, 0.57503, 0.44712198, 0.535978, 0.47915798, 0.569293, 0.375632, 0.758412, 0.295969, 0.705741, 0.33930302, 0.819598, 0.24593401, 0.893654, 0.18697703, 0.593374, 0.481776, 0.545089, 0.49633002, 0.615601, 0.41373003, 0.658739, 0.378128, 0.819903, 0.41351098, 0.754973, 0.432998, 0.895401, 0.392321, 0.984045, 0.368082, 0.596988, 0.52072, 0.546915, 0.51565504, 0.643545, 0.46662802, 0.696888, 0.45051098, 0.90723, 0.554314, 0.831046, 0.544786, 0.9999, 0.565755, 0.585435, 0.557967, 0.541211, 0.534176, 0.648957, 0.525985, 0.704147, 0.531596, 0.764159, 0.537763, 0.85742, 0.706935, 0.791173, 0.669206, 0.934559, 0.751607, 0.560597, 0.58788097, 0.52888, 0.549083, 0.631263, 0.582636, 0.679851, 0.608819, 0.732569, 0.637317, 0.810515, 0.896712, 0.75453, 0.82741296, 0.526317, 0.606017, 0.511816, 0.55813897, 0.59343, 0.628034, 0.628208, 0.670555, 0.665895, 0.716673, 0.707717, 0.76798403, 0.487793, 0.609733, 0.492608, 0.56000197, 0.541351, 0.655504, 0.557295, 0.707849, 0.574561, 0.76454103, 0.593638, 0.827516, 0.614138, 0.899719, 0.637505, 0.984956, 0.482914, 0.66112, 0.47785, 0.715472, 0.47246, 0.774325, 0.466571, 0.839563, 0.45879, 0.913558, 0.450524, 0.9999
</texCoords>
<faces>
0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 3, 3, 5, 5, 6, 6, 7, 7, 8, 8, 4, 4, 7, 7, 4, 4, 3, 3, 9, 9, 10, 10, 8, 8, 9, 9, 8, 8, 7, 7, 11, 11, 12, 12, 10, 10, 11, 11, 10, 10, 9, 9, 1, 1, 13, 13, 2, 2, 14, 14, 15, 15, 13, 13, 14, 14, 13, 13, 1, 1, 6, 6, 5, 5, 14, 14, 6, 6, 14, 14, 16, 16, 15, 15, 17, 17, 18, 18, 15, 15, 18, 18, 13, 13, 13, 13, 18, 18, 2, 2, 5, 5, 19, 19, 15, 15, 5, 5, 15, 15, 14, 14, 4, 4, 20, 20, 19, 19, 4, 4, 19, 19, 5, 5, 8, 8, 21, 21, 20, 20, 8, 8, 20, 20, 4, 4, 10, 10, 22, 22, 21, 21, 10, 10, 21, 21, 8, 8, 12, 12, 23, 23, 22, 22, 12, 12, 22, 22, 10, 10, 24, 24, 25, 25, 23, 23, 24, 24, 23, 23, 12, 12, 18, 18, 26, 26, 2, 2, 27, 27, 28, 28, 2, 2, 19, 19, 29, 29, 17, 17, 19, 19, 17, 17, 15, 15, 20, 20, 30, 30, 29, 29, 20, 20, 29, 29, 19, 19, 21, 21, 31, 31, 30, 30, 21, 21, 30, 30, 20, 20, 22, 22, 32, 32, 31, 31, 22, 22, 31, 31, 21, 21, 23, 23, 33, 33, 32, 32, 23, 23, 32, 32, 22, 22, 25, 25, 34, 34, 33, 33, 25, 25, 33, 33, 23, 23, 29, 29, 35, 35, 36, 36, 29, 29, 36, 36, 17, 17, 30, 30, 37, 37, 35, 35, 30, 30, 35, 35, 29, 29, 31, 31, 38, 38, 37, 37, 31, 31, 37, 37, 30, 30, 32, 32, 39, 39, 38, 38, 32, 32, 38, 38, 31, 31, 33, 33, 40, 40, 39, 39, 33, 33, 39, 39, 32, 32, 34, 34, 41, 41, 40, 40, 34, 34, 40, 40, 33, 33, 42, 42, 43, 43, 28, 28, 42, 42, 28, 28, 27, 27, 17, 17, 36, 36, 26, 26, 17, 17, 26, 26, 18, 18, 35, 35, 44, 44, 45, 45, 35, 35, 45, 45, 36, 36, 37, 37, 46, 46, 44, 44, 37, 37, 44, 44, 35, 35, 38, 38, 47, 47, 46, 46, 38, 38, 46, 46, 37, 37, 39, 39, 48, 48, 47, 47, 39, 39, 47, 47, 38, 38, 40, 40, 49, 49, 48, 48, 40, 40, 48, 48, 39, 39, 41, 41, 50, 50, 49, 49, 41, 41, 49, 49, 40, 40, 26, 26, 51, 51, 2, 2, 45, 45, 42, 42, 27, 27, 45, 45, 27, 27, 51, 51, 46, 46, 52, 52, 53, 53, 46, 46, 53, 53, 44, 44, 47, 47, 54, 54, 52, 52, 47, 47, 52, 52, 46, 46, 48, 48, 55, 55, 54, 54, 48, 48, 54, 54, 47, 47, 49, 49, 56, 56, 55, 55, 49, 49, 55, 55, 48, 48, 50, 50, 57, 57, 56, 56, 50, 50, 56, 56, 49, 49, 51, 51, 27, 27, 2, 2, 58, 58, 24, 24, 12, 12, 58, 58, 12, 12, 11, 11, 44, 44, 53, 53, 42, 42, 44, 44, 42, 42, 45, 45, 52, 52, 59, 59, 60, 60, 52, 52, 60, 60, 53, 53, 54, 54, 61, 61, 59, 59, 54, 54, 59, 59, 52, 52, 55, 55, 62, 62, 61, 61, 55, 55, 61, 61, 54, 54, 56, 56, 63, 63, 62, 62, 56, 56, 62, 62, 55, 55, 57, 57, 64, 64, 63, 63, 57, 57, 63, 63, 56, 56, 43, 43, 65, 65, 66, 66, 43, 43, 66, 66, 28, 28, 28, 28, 66, 66, 2, 2, 53, 53, 60, 60, 43, 43, 53, 53, 43, 43, 42, 42, 61, 61, 67, 67, 68, 68, 61, 61, 68, 68, 59, 59, 62, 62, 69, 69, 67, 67, 62, 62, 67, 67, 61, 61, 63, 63, 70, 70, 69, 69, 63, 63, 69, 69, 62, 62, 64, 64, 71, 71, 70, 70, 64, 64, 70, 70, 63, 63, 65, 65, 72, 72, 73, 73, 65, 65, 73, 73, 66, 66, 66, 66, 73, 73, 2, 2, 60, 60, 74, 74, 65, 65, 60, 60, 65, 65, 43, 43, 59, 59, 68, 68, 74, 74, 59, 59, 74, 74, 60, 60, 69, 69, 75, 75, 76, 76, 69, 69, 76, 76, 67, 67, 70, 70, 77, 77, 75, 75, 70, 70, 75, 75, 69, 69, 71, 71, 78, 78, 77, 77, 71, 71, 77, 77, 70, 70, 72, 72, 79, 79, 80, 80, 72, 72, 80, 80, 73, 73, 73, 73, 80, 80, 2, 2, 74, 74, 81, 81, 72, 72, 74, 74, 72, 72, 65, 65, 68, 68, 82, 82, 81, 81, 68, 68, 81, 81, 74, 74, 67, 67, 76, 76, 82, 82, 67, 67, 82, 82, 68, 68, 75, 75, 83, 83, 84, 84, 75, 75, 84, 84, 76, 76, 77, 77, 85, 85, 83, 83, 77, 77, 83, 83, 75, 75, 78, 78, 86, 86, 85, 85, 78, 78, 85, 85, 77, 77, 79, 79, 87, 87, 88, 88, 79, 79, 88, 88, 80, 80, 80, 80, 88, 88, 2, 2, 81, 81, 89, 89, 79, 79, 81, 81, 79, 79, 72, 72, 82, 82, 90, 90, 89, 89, 82, 82, 89, 89, 81, 81, 76, 76, 84, 84, 90, 90, 76, 76, 90, 90, 82, 82, 85, 85, 91, 91, 92, 92, 85, 85, 92, 92, 83, 83, 86, 86, 93, 93, 91, 91, 86, 86, 91, 91, 85, 85, 87, 87, 94, 94, 95, 95, 87, 87, 95, 95, 88, 88, 88, 88, 95, 95, 2, 2, 89, 89, 96, 96, 87, 87, 89, 89, 87, 87, 79, 79, 90, 90, 97, 97, 96, 96, 90, 90, 96, 96, 89, 89, 84, 84, 98, 98, 97, 97, 84, 84, 97, 97, 90, 90, 83, 83, 92, 92, 98, 98, 83, 83, 98, 98, 84, 84, 91, 91, 99, 99, 100, 100, 91, 91, 100, 100, 92, 92, 93, 93, 101, 101, 99, 99, 93, 93, 99, 99, 91, 91, 94, 94, 102, 102, 103, 103, 94, 94, 103, 103, 95, 95, 95, 95, 103, 103, 2, 2, 96, 96, 104, 104, 94, 94, 96, 96, 94, 94, 87, 87, 97, 97, 105, 105, 104, 104, 97, 97, 104, 104, 96, 96, 98, 98, 106, 106, 105, 105, 98, 98, 105, 105, 97, 97, 92, 92, 100, 100, 106, 106, 92, 92, 106, 106, 98, 98, 101, 101, 107, 107, 108, 108, 101, 101, 108, 108, 99, 99, 102, 102, 109, 109, 110, 110, 102, 102, 110, 110, 103, 103, 103, 103, 110, 110, 2, 2, 104, 104, 111, 111, 102, 102, 104, 104, 102, 102, 94, 94, 105, 105, 112, 112, 111, 111, 105, 105, 111, 111, 104, 104, 106, 106, 113, 113, 112, 112, 106, 106, 112, 112, 105, 105, 100, 100, 114, 114, 113, 113, 100, 100, 113, 113, 106, 106, 99, 99, 108, 108, 114, 114, 99, 99, 114, 114, 100, 100, 109, 109, 115, 115, 116, 116, 109, 109, 116, 116, 110, 110, 110, 110, 116, 116, 2, 2, 111, 111, 117, 117, 109, 109, 111, 111, 109, 109, 102, 102, 112, 112, 118, 118, 117, 117, 112, 112, 117, 117, 111, 111, 113, 113, 119, 119, 118, 118, 113, 113, 118, 118, 112, 112, 114, 114, 120, 120, 119, 119, 114, 114, 119, 119, 113, 113, 108, 108, 121, 121, 120, 120, 108, 108, 120, 120, 114, 114, 107, 107, 122, 122, 121, 121, 107, 107, 121, 121, 108, 108, 115, 115, 16, 16, 0, 0, 115, 115, 0, 0, 116, 116, 116, 116, 0, 0, 2, 2, 117, 117, 123, 123, 115, 115, 117, 117, 115, 115, 109, 109, 118, 118, 124, 124, 123, 123, 118, 118, 123, 123, 117, 117, 119, 119, 125, 125, 124, 124, 119, 119, 124, 124, 118, 118, 120, 120, 126, 126, 125, 125, 120, 120, 125, 125, 119, 119, 121, 121, 127, 127, 126, 126, 121, 121, 126, 126, 120, 120, 122, 122, 128, 128, 127, 127, 122, 122, 127, 127, 121, 121, 123, 123, 6, 6, 16, 16, 123, 123, 16, 16, 115, 115, 124, 124, 3, 3, 6, 6, 124, 124, 6, 6, 123, 123, 125, 125, 7, 7, 3, 3, 125, 125, 3, 3, 124, 124, 126, 126, 9, 9, 7, 7, 126, 126, 7, 7, 125, 125, 127, 127, 11, 11, 9, 9, 127, 127, 9, 9, 126, 126, 128, 128, 58, 58, 11, 11, 128, 128, 11, 11, 127, 127, 16, 16, 14, 14, 1, 1, 16, 16, 1, 1, 0, 0, 36, 36, 45, 45, 51, 51, 36, 36, 51, 51, 26, 26
</faces>
<faceSmoothingGroups>
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
</faceSmoothingGroups>
</TriangleMesh>
</mesh>
</MeshView>
space.jpg
Result
I need to split a deck of cards into two packets: the top half and the bottom half. This new array of cards is suppose to go: first card from from the top packet, first card from bottom packet, second card from top packet, second card from bottom packet, etc. If there are an odd number of cards then the top packet should have one more than the bottom packet. The top of the deck is the front of the array.
How would I go about doing this?
Here is the method I created to generate the deck of cards (I think it works):
private Card[] cards;
int value, suit;
private final int DECK_SIZE = 52;
public Deck()
{
int index = 0;
cards = new Card[DECK_SIZE];
//0 = spades, 1 = hearts, 2 = clovers, 3 =diamonds
int suits[] = {0, 1, 2, 3};
//1 = Ace, 11=jack, 12=queen, 13=king
int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
for (int suit : suits)
for (int value : values)
{
cards[index] = new Card(value, suit);
index++;
}
}
Before you go about doing what you say, note that a perfect shuffle is not a good idea if you are looking to randomize the order of a deck:
A perfect faro shuffle, where the cards are perfectly alternated, is considered one of the most difficult sleights of card manipulation, because it requires the shuffler to cut the deck into two equal stacks and apply just the right pressure when pushing the half decks into each other. If one manages to perform eight perfect faro out-shuffles in a row, then the deck of 52 cards will be restored to its original order. If one can do perfect in-shuffles, then 26 shuffles will reverse the order of the deck and 26 more will restore it to its original order.
If you want a random shuffle, on the other hand, the way to go is a Fisher-Yates shuffle. From the wikipedia page:
To shuffle an array a of n elements (indexes 0..n-1):
for i from n − 1 downto 1 do
j ← random integer with 0 ≤ j ≤ i
exchange a[j] and a[i]
Note, however, that depending on your randomness criteria, the standard Java random number generator may not be sufficient: (also from the Wikipedia page:)
For example, the built-in pseudorandom number generator provided by many programming languages and/or libraries may often have only 32 bits of internal state, which means it can only produce 232 different sequences of numbers. If such a generator is used to shuffle a deck of 52 playing cards, it can only ever produce a very small fraction of the 52! ≈ 2225.6 possible permutations. It's impossible for a generator with less than 226 bits of internal state to produce all the possible permutations of a 52-card deck. It has been suggested[citation needed] that confidence that the shuffle is unbiased can only be attained with a generator with more than about 250 bits of state.
Mersenne Twister is a well-known random number generator that would be adequate.
edit: for a literal answer to your original question, here's how I would probably do it (including a test method):
import java.util.Arrays;
public class Shuffle {
/* assumes input and output arrays are same length (N) */
static public <T> void perfectShuffle(T[] input, T[] output, int N)
{
int itop = 0;
int ibottom = N - (N/2);
/* bottom has (N/2) elements; for odd N this is rounded down,
* and the top part has 1 more element */
int k = 0;
while (ibottom < N)
{
output[k++] = input[itop++];
output[k++] = input[ibottom++];
}
// handle last element for N = odd
if (k < N)
output[k] = input[itop];
}
public static void main(String[] args) {
int N = 19;
String[] in = new String[N];
String[] out = new String[N];
for (int i = 0; i < N; ++i)
in[i] = Integer.toString(i);
perfectShuffle(in, out, N);
System.out.println(Arrays.asList(out));
}
}
output of main():
[0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9]
finally, the reason why you shouldn't use this for shuffling cards:
public static void main(String[] args) {
int N = 52;
String[] in = new String[N];
String[] out = new String[N];
for (int i = 0; i < N; ++i)
in[i] = Integer.toString(i);
for (int k = 0; k < 8; ++k)
{
perfectShuffle(in, out, N);
System.out.println(Arrays.asList(out));
String[] tmp = in;
in = out;
out = tmp;
}
}
output:
[0, 26, 1, 27, 2, 28, 3, 29, 4, 30, 5, 31, 6, 32, 7, 33, 8, 34, 9, 35, 10, 36, 11, 37, 12, 38, 13, 39, 14, 40, 15, 41, 16, 42, 17, 43, 18, 44, 19, 45, 20, 46, 21, 47, 22, 48, 23, 49, 24, 50, 25, 51]
[0, 13, 26, 39, 1, 14, 27, 40, 2, 15, 28, 41, 3, 16, 29, 42, 4, 17, 30, 43, 5, 18, 31, 44, 6, 19, 32, 45, 7, 20, 33, 46, 8, 21, 34, 47, 9, 22, 35, 48, 10, 23, 36, 49, 11, 24, 37, 50, 12, 25, 38, 51]
[0, 32, 13, 45, 26, 7, 39, 20, 1, 33, 14, 46, 27, 8, 40, 21, 2, 34, 15, 47, 28, 9, 41, 22, 3, 35, 16, 48, 29, 10, 42, 23, 4, 36, 17, 49, 30, 11, 43, 24, 5, 37, 18, 50, 31, 12, 44, 25, 6, 38, 19, 51]
[0, 16, 32, 48, 13, 29, 45, 10, 26, 42, 7, 23, 39, 4, 20, 36, 1, 17, 33, 49, 14, 30, 46, 11, 27, 43, 8, 24, 40, 5, 21, 37, 2, 18, 34, 50, 15, 31, 47, 12, 28, 44, 9, 25, 41, 6, 22, 38, 3, 19, 35, 51]
[0, 8, 16, 24, 32, 40, 48, 5, 13, 21, 29, 37, 45, 2, 10, 18, 26, 34, 42, 50, 7, 15, 23, 31, 39, 47, 4, 12, 20, 28, 36, 44, 1, 9, 17, 25, 33, 41, 49, 6, 14, 22, 30, 38, 46, 3, 11, 19, 27, 35, 43, 51]
[0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51]
[0, 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]
If you're able to substitute a non-perfect shuffle, try Collections.shuffle(). Your code would look something like this:
List card_list = Arrays.asList(cards);
Collections.shuffle(card_list);
or as #Mark Peters points out, the more concise:
Collections.shuffle(Arrays.asList(cards));
I was looking for something similar (shuffling a JSONArray) in this question: An efficient way to shuffle a JSON array in java?
I ended up making my own shuffle method implementing this algorithm. For your example, it would be something like:
public Card[] shuffle(Card[] cards) {
// Implementing Fisher–Yates shuffle
Random rnd = new Random();
for (int i = cards.length() - 1; i >= 0; i--)
{
int j = rnd.nextInt(i + 1);
// Simple swap
Card card = cards[j];
cards[j] = cards[i];
cards[i] = card;
}
return cards;
}