Last active
August 29, 2015 13:56
-
-
Save gunungloli666/8811867 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.fjr.main; | |
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(ObservableList<Node> list) { | |
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<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent arg0) { | |
if (state) { | |
removeOverlap(root.getChildren()); | |
} | |
state = false; | |
} | |
}); | |
button1.setOnAction(new EventHandler<ActionEvent>() { | |
@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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.fjr.main | |
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 | |
import TestCollision._ | |
import scala.collection.JavaConversions._ | |
object TestCollision { | |
def main(args: Array[String]) { | |
Application.launch(classOf[TestCollision], args: _*) | |
} | |
} | |
class TestCollision extends Application { | |
var r: Double = 14.0 | |
var rand: Random = new Random() | |
var xwidth: Double = 400 | |
var ywidth: Double = 400 | |
var numberParticle: Int = 100 | |
def testCol(p1: Circle, p2: Circle): Boolean = { | |
val dx = p1.getCenterX - p2.getCenterX | |
val dy = p1.getCenterY - p2.getCenterY | |
if (dx * dx + dy * dy < 4 * r * r) { | |
return true | |
} | |
false | |
} | |
def removeOverlap(list: ObservableList[Node]) { | |
for (i <- list.size-1 to 0 by -1 if list.get(i).isInstanceOf[Circle]) { | |
val p1 = list.get(i).asInstanceOf[Circle] | |
var j = list.size - 1 | |
while (j > i) { | |
if (list.get(j).isInstanceOf[Circle]) { | |
val p2 = list.get(j).asInstanceOf[Circle] | |
if (testCol(p1, p2)) { | |
list.remove(j) | |
} | |
} | |
j -= 1 | |
} | |
} | |
} | |
var button: Button = _ | |
var button1: Button = _ | |
var state: Boolean = true | |
override def start(primaryStage: Stage) { | |
val root = new Group() | |
primaryStage.setScene(new Scene(root, xwidth, ywidth)) | |
for (i <- 0 until numberParticle) { | |
root.getChildren.add(circle) | |
} | |
button = new Button { | |
setText("ORGANIZE") | |
setTranslateX(10) | |
setTranslateY(10) | |
} | |
button1 = new Button { | |
setText("RESEST") | |
setTranslateX(100) | |
setTranslateY(10) | |
} | |
root.getChildren.addAll(button, button1) | |
button.setOnAction(new EventHandler[ActionEvent]() { | |
override def handle(arg0: ActionEvent) { | |
if (state) { | |
removeOverlap(root.getChildren) | |
} | |
state = false | |
} | |
}) | |
button1.setOnAction(new EventHandler[ActionEvent]() { | |
override def handle(arg0: ActionEvent) { | |
var i = root.getChildren.size - 1 | |
while (i >= 0) { | |
root.getChildren.remove(i) | |
i -= 1 | |
} | |
for (i <- 0 until numberParticle) { | |
root.getChildren.add(circle) | |
} | |
root.getChildren.addAll(button, button1) | |
state = true | |
} | |
}) | |
primaryStage.show() | |
} | |
def circle: Circle = { | |
val c = new Circle { | |
setCenterX(rand.nextDouble() * xwidth) | |
setCenterY(rand.nextDouble() * ywidth) | |
setRadius(r) | |
} | |
c | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment