2017-06-14 87 views
1

我試圖建立一個顏色選擇器,用戶可以在其中看到鼠標指針所在的RGB值。JavaFX檢測場景外的鼠標移動

問題是,當鼠標指針離開場景時,它不起作用。但我使用另一個KeyTyped事件,並且工作正常。這意味着,當鼠標位於場景之外並按下任何按鈕時,可以看到鼠標指向的值爲RGB

現在,

  1. 有沒有辦法使用情況的keyTyped當鼠標移動?
  2. 有沒有其他的方法來檢測場景外的鼠標指向位置?

下面是代碼:

import java.awt.Color; 
import java.awt.MouseInfo; 
import java.awt.Point; 
import java.awt.Robot; 
import javafx.application.Application; 
import javafx.event.Event; 
import javafx.event.EventHandler; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.HBox; 
import javafx.scene.layout.VBox; 
import javafx.stage.Stage; 

/* 
* @author sapaythhossain 
*/ 
public class ColorPicker extends Application { 

    private Stage window; 
    private Scene scene; 
    private double width; 
    private double height; 
    private String title; 
    private Label colorLabel; 
    private Label rgbLabel; 
    private Label htmlLabel; 
    private Label rgbValueLabel; 
    private Label htmlValueLabel; 
    private Label bValueLabel; 

    private int RGBr; 
    private int RGBg; 
    private int RGBb; 

    private Color currentColor; 
    private Robot robot; 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     title = "Color Picker"; 
     width = 220; 
     height = 80; 

     robot = new Robot(); 

     window = primaryStage; 
     window.setTitle(title); 

     colorLabel = new Label(); 
     colorLabel.setWrapText(true); 
     colorLabel.setMinSize(50, 50); 
     colorLabel.setStyle(
       "-fx-background-color: rgba(" + RGBr + "," + RGBg + "," + RGBb + ",1);" 
      ); 

     VBox leftRow = new VBox(10); 
     leftRow.getChildren().addAll(colorLabel); 

     rgbLabel = new Label("RGB: "); 
     htmlLabel = new Label("HTML: "); 

     rgbValueLabel = new Label(""); 
     htmlValueLabel = new Label(""); 
     bValueLabel = new Label(); 

     VBox middleRow = new VBox(); 
     middleRow.getChildren().addAll(rgbLabel, htmlLabel); 

     VBox rightRow = new VBox(); 
     rightRow.getChildren().addAll(rgbValueLabel, htmlValueLabel, bValueLabel); 

     HBox layout = new HBox(10); 
     layout.getChildren().addAll(leftRow, middleRow, rightRow); 

     EventHandler handler = (EventHandler) (Event event) -> { 
      changeColor(); 
     }; 

     scene = new Scene(layout, width, height); 
     scene.setOnMouseMoved(handler); 
     scene.setOnKeyTyped(handler); 



     window.setScene(scene); 
     window.show(); 
    } 

    public void changeColor() { 
     Point p = MouseInfo.getPointerInfo().getLocation(); 
     currentColor = robot.getPixelColor(p.x, p.y); 

     RGBr = currentColor.getRed(); 
     RGBg = currentColor.getGreen(); 
     RGBb = currentColor.getBlue(); 

     String colorString = String.valueOf(RGBr) + ", " 
       + String.valueOf(RGBg) + ", " + String.valueOf(RGBb); 

     colorLabel.setStyle(
       "-fx-background-color: rgba(" + RGBr + "," + RGBg + "," + RGBb + ",1);" 
      ); 
     String hex = String.format("#%02x%02x%02x", RGBr, RGBg, RGBb); 
     htmlValueLabel.setText(hex); 
     rgbValueLabel.setText(colorString); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 

} 

在此先感謝。

+0

您可以使用'Jnativehook'庫這個目的,它捕獲所有的事件,甚至是在Java應用程序之外 – MaQuiNa1995

+0

非常感謝您的建議。我是Java新手。你能否給我一些我應該遵循的步驟來使用Jnativehook庫? – sapayth

+0

https://stackoverflow.com/questions/16635514/how-to-get-location-of-mouse-in-javafx – Sedrick

回答

1

歐凱您需要:

implements NativeMouseInputListener在類

,那麼你需要註冊鉤:

private void createHook() { 
    try { 
     // here you starts the hook 
     GlobalScreen.registerNativeHook(); 
    } catch (NativeHookException ex) { 
     //TODO Exception handling 
    } 

    // here you add the listener for the hook 
    GlobalScreen.addNativeMouseListener(this); 
} 

,那麼你需要選擇鼠標事件並投入getMousePointColor ()方法:

@Override 
    public void nativeMouseClicked(NativeMouseEvent nme) { 
    } 

    @Override 
    public void nativeMousePressed(NativeMouseEvent nme) { 
    } 

    @Override 
    public void nativeMouseReleased(NativeMouseEvent nme) { 
    } 

    @Override 
    public void nativeMouseMoved(NativeMouseEvent nme) { 
    } 

    @Override 
    public void nativeMouseDragged(NativeMouseEvent nme) { 
    } 

private void getMousePointColor() { 
    int xLocation = MouseInfo.getPointerInfo().getLocation().x; 
    int yLocation = MouseInfo.getPointerInfo().getLocation().y; 
    System.out.println("------------------------------------------------------"); 

    Robot r; 
    try { 
     r = new Robot(); 
     System.out.println(r.getPixelColor(xLocation, yLocation)); 
    } catch (AWTException ex) { 
     //TODO Exception handling 
    } 
    System.out.println("------------------------------------------------------"); 
} 

例如:

@Override 
    public void nativeMouseClicked(NativeMouseEvent nme) { 
     getMousePointColor(); 
    } 

JNative Hook Latest Version

JNative Hook Documention