我正在努力实现的目标:
一些很酷的图像浏览应用程序在应用程序底部有一个状态栏,可以不断显示您当前悬停的像素的信息。诸如X和Y位置、RGB数据等信息,有时甚至是颜色的十六进制代码。
我所做的:
所以MVC的模型和视图部分对我来说很容易。我创建了一个模型类,它包含一个SimpleStringProperty
,我将在StatusBarView
控制器中为其附加一个监听器。
在SceneBuilder中,我已将ImageView.fxml
的onMouseEnter
和onMouseMoved
事件绑定到ImageViewController
中名为mouseHoverInfo
的方法。这是一个空方法,我不知道该放什么。我做了一个深入的谷歌,似乎JavaFX不允许像素信息鼠标悬停跟踪?
伪码
下面方法的主体是Java风格的伪代码,让您了解我在ImageViewController
中的方法声明中要做什么。
@FXML
private void mouseHoverInfo() {
ImageViewInternalMouseHoverTracker ivimht = new ImageViewInternalMouseHoverTracker(this.imageView);
String xPos = ivimht.getX();
String yPos = ivimht.getY();
String colorRed = ivimht.getR();
String colorBlue = ivimht.getG();
String colorGreen = ivimht.getB();
String hexColor = ivimht.getHex();
String pixelInfo = "X: " + xPos + " Y: " + yPos + " | "
+ "r: " + colorRed + " g: " + colorGreen +
" b: " + colorBlue + " | " + hexColor;
mainApp.getPixelInfo().setInfoString(pixelInfo);
}
解决方案:
谢谢你的指点,@James_D!以下是鼠标悬停像素信息跟踪器的完整事件处理(必须使用一点AWT)。
this.imageView.setOnMouseMoved(event -> { try {
// AWT Robot and Color to trace pixel information
Robot robot = new Robot();
Color color = robot.getPixelColor((int) event.getScreenX(), (int) event.getScreenY());
// Initializing pixel info
String xPos = Integer.toString((int) event.getX());
String yPos = Integer.toString((int) event.getY());
String colorRed = Integer.toString(color.getRed());
String colorBlue = Integer.toString(color.getBlue());
String colorGreen = Integer.toString(color.getGreen());
String hexColor = String.format("#%02X%02X%02X", color.getRed(), color.getGreen(), color.getBlue());
// Unify and format the information
String pixelInfo = "X: " + xPos + " Y: " + yPos + " | "
+ "r: " + colorRed + " g: " + colorGreen +
" b: " + colorBlue + " | " + hexColor;
// Pass it on to the MainApp
this.mainApp.getPixelInfo().setInfoString(pixelInfo);
} catch (Exception ignore){}});