Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package testmouse;
  7.  
  8. import java.util.ArrayList;
  9. import javafx.application.Application;
  10. import javafx.event.ActionEvent;
  11. import javafx.event.Event;
  12. import javafx.event.EventHandler;
  13. import javafx.scene.Group;
  14. import javafx.scene.Scene;
  15. import javafx.scene.control.Button;
  16. import javafx.scene.input.MouseEvent;
  17. import javafx.scene.layout.StackPane;
  18. import javafx.scene.paint.Color;
  19. import javafx.scene.shape.Circle;
  20. import javafx.scene.shape.Rectangle;
  21. import javafx.stage.Stage;
  22.  
  23. /**
  24.  *
  25.  * @author david
  26.  */
  27. public class TestMouse extends Application {
  28.    
  29.     double coordX = 0, coordY = 0, coordX1 = 0, coordY1 = 0;
  30.     Group root = null;
  31.     Circle circ = null;
  32.     boolean dragging = false;
  33.     Rectangle rect = null;
  34.     Color c = null;
  35.            
  36.     @Override
  37.     public void start(Stage primaryStage) {
  38.         root = new Group();        
  39.         Scene scene = new Scene(root, 600, 600);
  40.          /*      
  41.         scene.addEventHandler(MouseEvent.ANY, new EventHandler<MouseEvent>(){
  42.             @Override
  43.             public void handle(MouseEvent e){
  44.                 coordX = e.getX();
  45.                 coordY = e.getY();
  46.                 circ = new Circle(coordX, coordY, Common.rand.nextDouble()*100);
  47.                 circ.setFill(Color.rgb(Common.rand.nextInt(255), Common.rand.nextInt(255), Common.rand.nextInt(255)));
  48.                 root.getChildren().add(circ);
  49.                 if(root.getChildren().size()>200){
  50.                     root.getChildren().remove(0);
  51.                 }
  52.             }
  53.         });*/
  54.          
  55.         scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>(){
  56.             @Override
  57.             public void handle(MouseEvent e) {
  58.                 if(dragging){
  59.                     dragging = false;
  60.                     coordX1 = e.getX();
  61.                     coordY1 = e.getY();
  62.                     rect = new Rectangle(Math.min(coordX, coordX1), Math.min(coordY, coordY1), Math.abs(coordX1-coordX), Math.abs(coordY1-coordY));
  63.                     rect.setFill(c);
  64.                     root.getChildren().add(rect);
  65.                 } else {
  66.                     dragging = true;
  67.                     c = Color.rgb(Common.rand.nextInt(255), Common.rand.nextInt(255), Common.rand.nextInt(255));
  68.                     coordX = e.getX();
  69.                     coordY = e.getY();
  70.                 }
  71.             }
  72.         });
  73.        
  74.         scene.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>(){
  75.             @Override
  76.             public void handle(MouseEvent e) {
  77.                 if(dragging){
  78.                     if(root.getChildren().size()>0){
  79.                         root.getChildren().remove(root.getChildren().size()-1);
  80.                     }
  81.                     coordX1 = e.getX();
  82.                     coordY1 = e.getY();
  83.                     rect = new Rectangle(Math.min(coordX, coordX1), Math.min(coordY, coordY1), Math.abs(coordX1-coordX), Math.abs(coordY1-coordY));
  84.                     rect.setFill(c);
  85.                     root.getChildren().add(rect);
  86.                 }
  87.             }
  88.            
  89.         });
  90.        
  91.         primaryStage.setTitle("Hello World!");
  92.         primaryStage.setScene(scene);
  93.         primaryStage.show();
  94.     }
  95.  
  96.     /**
  97.      * @param args the command line arguments
  98.      */
  99.     public static void main(String[] args) {
  100.         launch(args);
  101.     }
  102.    
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement