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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package mouseevents;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.effect.InnerShadow;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;
/**
* A sample that demonstrates various mouse and scroll events and their usage.
* Click the circles and drag them across the screen. Scroll the whole screen.
* All events are logged to the console.
*
* @see javafx.scene.Cursor
* @see javafx.scene.input.MouseEvent
* @see javafx.event.EventHandler
*/
public class MouseEvents extends Application {
{
}
//variables for storing initial position before drag of circle
private double initX;
private double initY;
private Point2D dragAnchor;
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setResizable(false);
primaryStage.setScene(new Scene(root, 500,500));
// create circle with method listed below: paramethers: name of the circle, color of the circle, radius
final Circle circleSmall = createCircle("Blue circle", Color.DODGERBLUE, 25);
circleSmall.setTranslateX(200);
circleSmall.setTranslateY(80);
// and a second, bigger circle
// final Circle circleSmall = createCircle("Orange circle", Color.CORAL, 40);
// we can set mouse event to any node, also on the rectangle
circleSmall.setOnMouseMoved(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
}
}
);
// show all the circle , rectangle and console
root.getChildren().addAll(circleSmall);
}
private Circle createCircle(final String name, final Color color, int radius) {
//create a circle with desired name, color and radius
final Circle circle = new Circle(radius, new RadialGradient(0, 0, 0.2, 0.3, 1, true, CycleMethod.NO_CYCLE, new Stop[] {
new Stop(0, Color.rgb(250,250,255)),
new Stop(1, color)
}));
//add a shadow effect
circle.setEffect(new InnerShadow(7, color.darker().darker()));
//change a cursor when it is over circle
circle.setCursor(Cursor.HAND);
//add a mouse listeners
circle.setOnMouseClicked(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
//the event will be passed only to the circle which is on front
me.consume();
}
});
circle.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
double dragX = me.getSceneX() - dragAnchor.getX();
double dragY = me.getSceneY() - dragAnchor.getY();
//calculate new position of the circle
double newXPosition = initX + dragX;
double newYPosition = initY + dragY;
//if new position do not exceeds borders of the rectangle, translate to this position
if ((newXPosition>=circle.getRadius()) && (newXPosition<=500-circle.getRadius())) {
circle.setTranslateX(newXPosition);
}
if ((newYPosition>=circle.getRadius()) && (newYPosition<=300-circle.getRadius())){
circle.setTranslateY(newYPosition);
}
}
});
circle.setOnMouseEntered(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
//change the z-coordinate of the circle
circle.toFront();
}
});
circle.setOnMouseExited(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
}
});
circle.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
//when mouse is pressed, store initial position
initX = circle.getTranslateX();
initY = circle.getTranslateY();
dragAnchor = new Point2D(me.getSceneX(), me.getSceneY());
}
});
circle.setOnMouseReleased(new EventHandler<MouseEvent>() {
public void handle(MouseEvent me) {
}
});
return circle;
}
/*private void showOnConsole(String text) {
//if there is 8 items in list, delete first log message, shift other logs and add a new one to end position
if (consoleObservableList.size()==8) {
consoleObservableList.remove(0);
}
consoleObservableList.add(text);
}*/
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX
* application. main() serves only as fallback in case the
* application can not be launched through deployment artifacts,
* e.g., in IDEs with limited FX support. NetBeans ignores main().
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
} |