我正在尝试使用JavaFX使用按钮作为像素来制作sprite editor。我可以在按下时更改每个按钮的颜色,但是我正在尝试获取它,因此如果单击并拖动,我可以绘制多个像素。
我发现的问题是,单击并按住一个按钮后,当我将鼠标移到新按钮上也要选择该按钮时,我无法选择新按钮。如果单击并拖动鼠标再重新输入该按钮,则可以看到“ Paint Dragged Pixel”调试消息,但是如果我用鼠标按下来输入新像素,这不是我想要的。另外,当鼠标进入任何按钮时,我都可以得到像素按钮以打印“ Entered Pixel”,但单击并拖动到新像素时则不能。
我认为问题在于,当我单击一个像素时,我被锁定了,无法通过将鼠标悬停在一个新像素上来选择一个新像素。有没有办法取消此选择的绑定,或者是另一个问题。
主要应用:
public class Main extends Application {
boolean mousePressed = false;
public boolean isMousePressed() {
return mousePressed;
}
@Override
public void start(Stage primaryStage) throws Exception{
BorderPane borderPane = new BorderPane();
primaryStage.setTitle("SpriteSheet");
Group root = new Group();
Scene scene = new Scene(borderPane, 500,200);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
GridPane gridPane = new GridPane();
borderPane.setCenter(root);
for(int x = 0; x < 10; x++)
{
for(int y = 0; y < 10; y++) {
PixelButton button = new PixelButton();
button.setParentMain(this);
button.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mousePressed = true;
System.out.println("mouseDown");
}
});
button.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
mousePressed = false;
System.out.println("mouseUp");
}
});
gridPane.add(button, x, y);
}
}
root.getChildren().add(gridPane);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
PixelButton的类。
public class PixelButton extends Button {
Main parentMain;
public void setParentMain(Main parent) {
parentMain = parent;
}
public PixelButton() {
this.setMinSize(10, 10);
this.setPrefSize(10, 10);
this.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
}
});
this.setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
System.out.println("Entered Pixel");
if(parentMain.isMousePressed()){
System.out.println("Paint Dragged Pixel");
}
}
});
}
}
谢谢。
答案 0 :(得分:0)
好吧,所以我一直在思考这个问题,最后想出了一个解决方案,我简化了我的解决方案,并使用矩形而不是按钮,但是您也可以将大多数代码转移到按钮上首先,这并不是您要寻找的确切功能,但我尽可能地接近我基本上会在按下鼠标时触发一个事件,以释放鼠标单击,只要该事件不是来自矩形,则不要翻转绘画布尔值绘画,因此您基本上可以单击以进入“绘画模式”,然后再次单击以退出着色瓷砖
public class Main extends Application {
private boolean mousePressed;
@Override
public void start(Stage primaryStage) throws Exception{
BorderPane borderPane = new BorderPane();
primaryStage.setTitle("SpriteSheet");
Group root = new Group();
Scene scene = new Scene(borderPane, 500,200);
// scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
GridPane gridPane = new GridPane();
borderPane.setCenter(root);
for(int x = 0; x < 10; x++) {
for(int y = 0; y < 10; y++) {
Rectangle rectangle = new Rectangle(10, 10);
rectangle.setOnMousePressed(event -> {
mousePressed = true;
System.out.println("mouseDown");
rectangle.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED,
rectangle.getLayoutX(), rectangle.getLayoutY(), rectangle.getLayoutX(), rectangle.getLayoutY(),
MouseButton.PRIMARY, 1,
false, false, false, false,
false, false, false, false,
false, false, null));
});
rectangle.setOnMouseReleased(event -> {
System.out.println(event.getSource());
if(!event.getSource().toString().equals("Rectangle[x=0.0, y=0.0, width=10.0, height=10.0, fill=0x000000ff]")) {
mousePressed = false;
System.out.println("mouseUp");
}
});
rectangle.setOnMouseMoved(event -> {
if(mousePressed) {
rectangle.setFill(Color.BLUE);
}
});
gridPane.add(rectangle, x, y);
}
}
root.getChildren().add(gridPane);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}