public void createMouseInputHandling() {
    scene.setOnMouseClicked(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
    scene.setOnMouseMoved(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent mouse) {
            stage.setTitle(mouse.getX() + " - " + mouse.getY());
            Rectangle targetData = new Rectangle(100, 100, 100, 100);
            if (targetData.contains(mouse.getX(), mouse.getY())) {
              stage.setTitle(mouse.getX() + " - " + mouse.getY() + " Lol");
            }
          }
        });
    scene.setOnMousePressed(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
    scene.setOnMouseReleased(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
    scene.setOnMouseEntered(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
    scene.setOnMouseDragOver(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
    scene.setOnMouseDragReleased(
        new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent arg0) {}
        });
  }
  private void registerMouseClickHandlers(final Scene scene) {
    scene.setOnMouseClicked(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            Vector clickedPositionSC = Vector.cartesian(event.getSceneX(), event.getSceneY());
            Vector clickedPositionEC = getViewport().toEC(clickedPositionSC);

            if (event.getClickCount() == 1) getTracker().stopTracking();

            if (event.getClickCount() == 2) getTracker().startTrackingThingAt(clickedPositionEC);

            if (event.getClickCount() == 3) copyDNAToClipboard(clickedPositionEC);
          }
        });
  }
Beispiel #3
0
  private void attachMouseEvents(Scene scene, final Stage primaryStage) {

    // Full screen toggle
    scene.setOnMouseClicked(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            if (event.getClickCount() == 2) {
              primaryStage.setFullScreen(!primaryStage.isFullScreen());
            }
          }
        });

    // starting initial anchor point
    scene.setOnMousePressed(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            if (!primaryStage.isFullScreen()) {
              anchorPt = new Point2D(event.getScreenX(), event.getScreenY());
            }
          }
        });

    // dragging the entire stage
    scene.setOnMouseDragged(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            if (anchorPt != null && previousLocation != null && !primaryStage.isFullScreen()) {
              primaryStage.setX(previousLocation.getX() + event.getScreenX() - anchorPt.getX());
              primaryStage.setY(previousLocation.getY() + event.getScreenY() - anchorPt.getY());
            }
          }
        });

    // set the current location
    scene.setOnMouseReleased(
        new EventHandler<MouseEvent>() {
          public void handle(MouseEvent event) {
            if (!primaryStage.isFullScreen()) {
              previousLocation = new Point2D(primaryStage.getX(), primaryStage.getY());
            }
          }
        });
  }
 /** Create the game's scene */
 public Scene init(int width, int height) {
   // Create a scene graph to organize the scene
   Group root = new Group();
   // Create a place to see the shapes
   myScene = new Scene(root, width, height, Color.WHITE);
   // Make some shapes and set their properties
   Image image = new Image(getClass().getClassLoader().getResourceAsStream("duke.gif"));
   myBouncer = new ImageView(image);
   // x and y represent the top left corner, so center it
   myBouncer.setX(width / 2 - myBouncer.getBoundsInLocal().getWidth() / 2);
   myBouncer.setY(height / 2 - myBouncer.getBoundsInLocal().getHeight() / 2);
   myTopBlock = new Rectangle(width / 2 - 25, height / 2 - 100, 50, 50);
   myTopBlock.setFill(Color.RED);
   myBottomBlock = new Rectangle(width / 2 - 25, height / 2 + 50, 50, 50);
   myBottomBlock.setFill(Color.BISQUE);
   // order added to the group is the order in whuch they are drawn
   root.getChildren().add(myBouncer);
   root.getChildren().add(myTopBlock);
   root.getChildren().add(myBottomBlock);
   // Respond to input
   myScene.setOnKeyPressed(e -> handleKeyInput(e.getCode()));
   myScene.setOnMouseClicked(e -> handleMouseInput(e.getX(), e.getY()));
   return myScene;
 }
 // initializes the instruction scene and directs user to it upon clicking
 public void goToInfo(Stage stage, Timeline timeline) {
   instructions = new Instructions();
   Scene info = instructions.init(stage, timeline, width, height);
   mySplash.setOnMouseClicked(e -> stage.setScene(info));
 }
  public static void createStartTownSaloonView() {
    GridPane gridPane = new GridPane();

    gridPane.setHgap(2);
    gridPane.setVgap(2);
    gridPane.setId("saloonGridBackground");

    Button backBtn = new Button("Back");
    backBtn.setId("mainScreenBtn");
    gridPane.add(backBtn, 0, 0);

    URL url =
        StartTownSaloonView.class.getClassLoader().getResource("resources/saloonPianoMusic.mp3");
    assert url != null;
    final Media media = new Media(url.toString());
    final MediaPlayer mediaPlayer = new MediaPlayer(media);
    mediaPlayer.setVolume(25);
    mediaPlayer.setCycleCount(99);
    mediaPlayer.play();

    Scene scene =
        new Scene(
            gridPane,
            Main.getPrimaryStage().getScene().getWidth(),
            Main.getPrimaryStage().getScene().getHeight());

    scene.getStylesheets().add("resources/main.css");

    ArrayList<Tooltip> tooltipArrayList = new ArrayList<>();

    AddKeyListenerToScene.addGeneralKeyListener(scene);
    /**
     * Need to add a bunch of collision detection rectangles for clicking on people and getting a
     * toolip to popup.
     */
    scene.setOnMouseClicked(
        event -> {
          double x = event.getX();
          double y = event.getY();
          for (Tooltip eraseTooltip : tooltipArrayList) {
            eraseTooltip.hide();
          }

          if (x >= 767.0 && x <= 814.0 && y >= 378.0 && y <= 461.0) {
            Tooltip bartender = new Tooltip("What can I get ya?");
            bartender.setId("startTownSaloonTooltip");
            bartender.show(
                gridPane, 776.0 + scene.getWindow().getX(), 407.0 + scene.getWindow().getY());
            tooltipArrayList.add(bartender);
          }
          if (x >= 687.0 && x <= 759.0 && y >= 387.0 && y <= 507.0) {
            Tooltip oldManAtTheBar =
                new Tooltip("Whiskey, Mmm Mmm Mmm down to my belly, yum yum yum...*hiccup*");
            oldManAtTheBar.setId("startTownSaloonTooltip");
            oldManAtTheBar.show(
                gridPane, 745.0 + scene.getWindow().getX(), 432.0 + scene.getWindow().getY());
            tooltipArrayList.add(oldManAtTheBar);
          }
          if (x >= 43.0 && x <= 102.0 && y >= 330.0 && y <= 545.0) {
            Tooltip banjoPlayerInBack = new Tooltip("Yoddle yoddleeeee yoddleeeeheeeehoooo...");
            banjoPlayerInBack.setId("startTownSaloonTooltip");
            banjoPlayerInBack.show(
                gridPane, 83.0 + scene.getWindow().getX(), 356.0 + scene.getWindow().getY());
            tooltipArrayList.add(banjoPlayerInBack);
          }
          if (x >= 606.0 && x <= 662.0 && y >= 383.0 && y <= 688.0) {
            Tooltip secretAlienTechHintDude =
                new Tooltip(
                    "I was out in the fields...staring\n"
                        + "at the sky when suddenly a huge blinding\n"
                        + "light shot up from the ground. Aliens man\n"
                        + "I hope they left something behind for me to\n"
                        + "find!");
            secretAlienTechHintDude.setId("startTownSaloonTooltip");
            secretAlienTechHintDude.show(
                gridPane, 645.0 + scene.getWindow().getX(), 428.0 + scene.getWindow().getY());
            tooltipArrayList.add(secretAlienTechHintDude);
          }
          if (x >= 923.0 && x <= 1029.0 && y >= 385.0 && y <= 752.0) {
            Tooltip guyHittingOnGirlOnRight = new Tooltip("Sup baby gurrllllll");
            guyHittingOnGirlOnRight.setId("startTownSaloonTooltip");
            guyHittingOnGirlOnRight.show(
                gridPane, 952.0 + scene.getWindow().getX(), 427.0 + scene.getWindow().getY());
            tooltipArrayList.add(guyHittingOnGirlOnRight);
          }

          /**
           * Set listeners for window drags. If user drags the window then hide all the tooltips or
           * they will stay where they are until user hides them with the other commands. Aesthetics
           * only really.
           */
          scene
              .getWindow()
              .xProperty()
              .addListener(
                  (observable, oldValue, newValue) -> {
                    for (Tooltip eraseTooltip : tooltipArrayList) {
                      eraseTooltip.hide();
                    }
                  });
          scene
              .getWindow()
              .yProperty()
              .addListener(
                  (observable, oldValue, newValue) -> {
                    for (Tooltip eraseTooltip : tooltipArrayList) {
                      eraseTooltip.hide();
                    }
                  });

          // System.out.println(event.getX());
          // System.out.println(event.getY());
        });

    backBtn.setOnAction(
        event -> {
          Main.getPrimaryStage().setScene(StartingTownView.getStartingTownView());
          mediaPlayer.stop();
          for (Tooltip eraseTooltip : tooltipArrayList) {
            eraseTooltip.hide();
            eraseTooltip = null;
          }
        });

    setStartSaloon(scene);
    Main.getPrimaryStage().setScene(scene);
  }