package fjr.collision; import java.util.Random; import javafx.application.Application; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.ButtonBuilder; import javafx.scene.shape.Circle; import javafx.scene.shape.CircleBuilder; import javafx.stage.Stage; public class TestCollision extends Application { double r = 14.0; Random rand = new Random(); double xwidth = 400; double ywidth = 400; int numberParticle = 100; // uji tumbukan public boolean testCol(Circle p1, Circle p2) { double dx = p1.getCenterX() - p2.getCenterX(); double dy = p1.getCenterY() - p2.getCenterY(); if (dx * dx + dy * dy < 4 * r * r) { return true; } return false; } public void removeOverlap(ObservableListlist) { for (int i = 0 ; i< list.size() ; i++ ) { if (list.get(i) instanceof Circle) { Circle p1 = (Circle) list.get(i); for (int j = list.size()- 1 ; j > i ; j-- ) { if (list.get(j) instanceof Circle) { Circle p2 = (Circle) list.get(j); if (testCol(p1, p2)) { list.remove(j); } } } } } } public static void main(String[] args) { launch(args); } Button button, button1; boolean state = true; @Override public void start(Stage primaryStage) throws Exception { final Group root = new Group(); primaryStage.setScene(new Scene(root, xwidth, ywidth)); for (int i = 0; i < numberParticle; i++) { root.getChildren().add( CircleBuilder.create().centerX(rand.nextDouble() * xwidth) .centerY(rand.nextDouble() * ywidth).radius(r) .build()); } button = ButtonBuilder.create().text("ORGANIZE").translateX(10) .translateY(10).build(); button1 = ButtonBuilder.create().text("RESET").translateX(100) .translateY(10).build(); root.getChildren().addAll(button, button1); button.setOnAction(new EventHandler () { @Override public void handle(ActionEvent arg0) { if (state) { removeOverlap(root.getChildren()); } state = false; } }); button1.setOnAction(new EventHandler () { @Override public void handle(ActionEvent arg0) { for (int i = root.getChildren().size() - 1; i >= 0; i--) { root.getChildren().remove(i); } for (int i = 0; i < numberParticle; i++) { root.getChildren().add( CircleBuilder.create() .centerX(rand.nextDouble() * xwidth) .centerY(rand.nextDouble() * ywidth) .radius(r).build()); } root.getChildren().addAll(button, button1); state = true; } }); primaryStage.show(); } }
Sesungguhnya shalat itu mencegah dari (perbuatan-perbuatan) keji dan mungkar
Q.S. Al-'Ankabut Ayat 45
Monday, August 19, 2013
Deteksi tumbukan dengan javaFX
Pada tutorial ini saya akan memberikan bagaimana cara mendeteksti tumbukan dengan javaFX yang previewnya dapat dilihat pada gambar berikut:
Adapaun source codenya adalah:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment