Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package testmouse;
- import java.util.ArrayList;
- import javafx.application.Application;
- import javafx.event.ActionEvent;
- import javafx.event.Event;
- import javafx.event.EventHandler;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.layout.StackPane;
- import javafx.scene.paint.Color;
- import javafx.scene.shape.Circle;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- /**
- *
- * @author david
- */
- public class TestMouse extends Application {
- double coordX = 0, coordY = 0, coordX1 = 0, coordY1 = 0;
- Group root = null;
- Circle circ = null;
- boolean dragging = false;
- Rectangle rect = null;
- Color c = null;
- @Override
- public void start(Stage primaryStage) {
- root = new Group();
- Scene scene = new Scene(root, 600, 600);
- /*
- scene.addEventHandler(MouseEvent.ANY, new EventHandler<MouseEvent>(){
- @Override
- public void handle(MouseEvent e){
- coordX = e.getX();
- coordY = e.getY();
- circ = new Circle(coordX, coordY, Common.rand.nextDouble()*100);
- circ.setFill(Color.rgb(Common.rand.nextInt(255), Common.rand.nextInt(255), Common.rand.nextInt(255)));
- root.getChildren().add(circ);
- if(root.getChildren().size()>200){
- root.getChildren().remove(0);
- }
- }
- });*/
- scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
- @Override
- public void handle(MouseEvent e) {
- if(dragging){
- dragging = false;
- coordX1 = e.getX();
- coordY1 = e.getY();
- rect = new Rectangle(Math.min(coordX, coordX1), Math.min(coordY, coordY1), Math.abs(coordX1-coordX), Math.abs(coordY1-coordY));
- rect.setFill(c);
- root.getChildren().add(rect);
- } else {
- dragging = true;
- c = Color.rgb(Common.rand.nextInt(255), Common.rand.nextInt(255), Common.rand.nextInt(255));
- coordX = e.getX();
- coordY = e.getY();
- }
- }
- });
- scene.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>(){
- @Override
- public void handle(MouseEvent e) {
- if(dragging){
- if(root.getChildren().size()>0){
- root.getChildren().remove(root.getChildren().size()-1);
- }
- coordX1 = e.getX();
- coordY1 = e.getY();
- rect = new Rectangle(Math.min(coordX, coordX1), Math.min(coordY, coordY1), Math.abs(coordX1-coordX), Math.abs(coordY1-coordY));
- rect.setFill(c);
- root.getChildren().add(rect);
- }
- }
- });
- primaryStage.setTitle("Hello World!");
- primaryStage.setScene(scene);
- primaryStage.show();
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement