I am very new JavaFX, started learning it yesterday. Spent the whole day reading through the documentation, but learned nothing...
Here is what I want to do, make a simple JavaFX application that creates a circle. On click its stroke turns orange (some color). On unlicked (click on anything other than that circle), the stroke turns (some color) white.
Here is my pseudo code what I have so far. I want to make a separate class that creates a circle and handle the events (important).
public class Something extends Application {
@Override
public void start(Stage primaryStage) {
MyCircle c1 = new MyCircle();
c1.setCircle(20, 0, 0);
TilePane root = new TilePane();
root.getChildren().add(c1.getCircle());
//Some kind of mouse event listener, not sure which one should be
c1.getCircle().addEventListener(); //pseudo code
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Circle");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This the class that should create a circle and handle all the events, such as, mouse click, mouse location, click and drag and stuff like that.
public class MyCircle implements EventHandler{
Circle circle = new Circle();
public void setCircle(int radius, int x, int y){
circle.setRadius(radius);
position(x,y);
circle.setStrokeWidth(3);
circle.setStroke(Color.valueOf("white"));
}
public Circle getCircle(){
return circle;
}
public void position(int x, int y){
circle.setTranslateX(x);
circle.setTranslateY(y);
}
public void selected(){
circle.setStroke(Color.valueOf("orange"));
}
public void unselected() {
circle.setStroke(Color.valueOf("white"));
}
@Override
public void handle(Event event) {
if (event == MOUSE_CLICKED){ //pseudo code
selected();
}
else if(event == MOUSE_UNCLICKED){ //pseudo code
unselected();
}
}
}
Since I am very new to JavaFX, I'd much appreciate explanation as well. Thanks!
EDIT: This is my pseudo code, and I want to convert it into an actual working code. I am not sure how would I do that. Any help would be appreciated.
ANOTHER EDIT: Everything is a code except 3 marked places. Please look for my comment Psuedo Code
within the code, where I need help to change the pseudo code into an actual code.