Anda tinggal menambahkan sedikit sentuhan untuk memberi efek rotasi dengan trigger mouse...
selamat mencoba..!
package fjr.booksample; import org.omg.CORBA.Environment; import javafx.scene.transform.Rotate; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.geometry.Orientation; import javafx.scene.Group; import javafx.scene.GroupBuilder; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.scene.control.Slider; import javafx.scene.control.SliderBuilder; import javafx.scene.image.Image; import javafx.scene.image.ImageViewBuilder; import javafx.scene.layout.StackPane; /** * cari gambar sebanyak 46 buah (bisa juga sejumlah lain tapi biar mudahnya pake 46).. * kemudian namakan dengan pic1.jpg, pic2.jpg dst.... liat method getArrayImage di bawah.. :D * * @author meta alias trisna utami yang pacarnya suka ama lev landau... :D */ public class MakeABall extends Application { static final int NUM_ARRAY = 46; private void init(Stage primaryStage) { final Group root = new Group(); StackPane mainPane = new StackPane(); mainPane.setMinSize(800, 600); primaryStage.setScene(new Scene(mainPane)); Image[] images = MakeABall.getArrayImage(); final Group mainEnsembel = new Group(); int a = 0; Image[] imageA = new Image[12]; for (int i = 0; i < imageA.length; i++) { imageA[i] = images[a]; a++; } ImageEnsembel ensembelA = new ImageEnsembel(imageA, 0, 153); ensembelA.setTranslateX(200); ensembelA.setTranslateY(200); // ensembelA.ry.setAngle(12); mainEnsembel.getChildren().add(ensembelA); Image[] imageB = new Image[10]; for (int i = 0; i < imageB.length; i++) { imageB[i] = images[a]; a++; } Group ensembelB = new ImageEnsembel(imageB, 30, 140); ensembelB.setTranslateX(200); ensembelB.setTranslateY(290); mainEnsembel.getChildren().add(ensembelB); Image[] imageC = new Image[10]; for (int i = 0; i < imageC.length; i++) { imageC[i] = images[a]; a++; } ImageEnsembel ensembelC = new ImageEnsembel(imageC, 30, 140); ensembelC.setTranslateX(200); ensembelC.setTranslateY(150); ensembelC.rz.setAngle(180); mainEnsembel.getChildren().add(ensembelC); Image[] imageD = new Image[6]; for(int i =0 ;i < imageD.length; i++){ imageD[i] = images[a]; a++; } ImageEnsembel ensembelD = new ImageEnsembel(imageD, 65, 110); ensembelD.setTranslateX(200); ensembelD.setTranslateY(355); mainEnsembel.getChildren().add(ensembelD); Image[] imageE = new Image[6]; for(int i =0 ; i< imageE.length ; i++){ imageE[i] = images[a] ; a++; } // ImageEnsembel ensembelE = new ImageEnsembel(imageE, 65, 110); ensembelE.setTranslateX(200); ensembelE.setTranslateY(85); ensembelE.rz.setAngle(180); mainEnsembel.getChildren().add(ensembelE); Image[] imageF = new Image[2]; for(int i =0 ; i< imageF.length ; i++){ imageF[i] = images[a] ; a++; } // ImageEnsembel ensembelF = new ImageEnsembel(imageA, 0, 180); // ensembelF.setTranslateX(210); // ensembelF.setTranslateY(210); // ensembelF.rz.setAngle(90); // mainEnsembel.getChildren().add(ensembelF); mainEnsembel.setRotationAxis(Rotate.Y_AXIS); // mainEnsembel.setRotate(90); root.getChildren().add(mainEnsembel); final Group xGroup = new Group(); xGroup.getChildren().add(root); final Slider slider = SliderBuilder.create() .orientation(Orientation.VERTICAL).minWidth(50).minHeight(50) .value(50).maxHeight(70).translateX(-230).translateY(-200) .build(); slider.valueProperty().addListener(new ChangeListener() { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { root.setRotationAxis(Rotate.X_AXIS); if (slider.getValue() < 50) { root.setRotate(slider.getValue() - 50); } else { root.setRotate(slider.getValue() - 50); } } }); final Slider slider2 = SliderBuilder.create() .orientation(Orientation.VERTICAL).minWidth(50).minHeight(50) .maxHeight(70).translateX(-200).translateY(-200).build(); slider2.valueProperty().addListener(new ChangeListener () { @Override public void changed(ObservableValue arg0, Number arg1, Number arg2) { mainEnsembel.setRotationAxis(Rotate.Y_AXIS); mainEnsembel.setRotate(slider2.getValue() * 2); } }); root.setRotationAxis(Rotate.X_AXIS); mainPane.getChildren().add(xGroup); mainPane.getChildren().addAll(slider, slider2); primaryStage.setResizable(true); } class ImageEnsembel extends Group implements Cloneable { final Rotate rx = new Rotate(0, Rotate.X_AXIS); final Rotate ry = new Rotate(0, Rotate.Y_AXIS); final Rotate rz = new Rotate(0, Rotate.Z_AXIS); public ImageEnsembel(Image[] images, double angles, double space) { super(); double angle = 0; double jarak = 360 / images.length; getTransforms().addAll(rx,ry,rz); for (int i = 0; i < images.length; i++) { Xform xform = new Xform(); xform.getChildren() .add(ImageViewBuilder.create().image(images[i]) .fitWidth(60).fitHeight(50) .preserveRatio(false) .rotationAxis(Rotate.Y_AXIS).rotate(90).build()); xform.rz.setAngle(angles); xform.setTranslateX(space); Xform xform1 = new Xform(); xform1.getChildren().add(xform); xform1.ry.setAngle(angle); getChildren().add(xform1); angle += jarak; } } public Object clone() throws CloneNotSupportedException{ return super.clone(); } } static String[] getArrayString() { String[] array = new String[NUM_ARRAY]; String a = "pic"; for (int i = 0; i < array.length; i++) { array[i] = a.concat(Integer.toString(i + 1).concat(".jpg")); } return array; } static Image[] getArrayImage() { Image[] array = new Image[NUM_ARRAY]; String[] arrStr = getArrayString(); for (int i = 0; i < array.length; i++) { array[i] = new Image(MakeABall.class.getResource(arrStr[i]) .toExternalForm()); } return array; } class Xform extends Group { final Rotate rx = new Rotate(0, Rotate.X_AXIS); final Rotate ry = new Rotate(0, Rotate.Y_AXIS); final Rotate rz = new Rotate(0, Rotate.Z_AXIS); public Xform() { super(); getTransforms().addAll(rz, ry, rx); } } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
No comments:
Post a Comment