我在AnchorPane中有10個圈子的FXML應用程序。我想將鼠標懸停在一個圓上,並使其他9和背景變暗。Javafx變暗背景
我能做的最好的是一些基本的FadeTransition,它只會讓它們消失,不會變暗,再加上我不知道如何選擇節點的所有子節點,除了一個有鼠標的節點。手動選擇除一個以外的所有子項對於更多對象似乎並不真正有效
我試圖谷歌它,但我只是找不到任何東西。 請發佈一個鏈接到類似問題的線程或示例代碼。任何幫助將非常感激。
我在AnchorPane中有10個圈子的FXML應用程序。我想將鼠標懸停在一個圓上,並使其他9和背景變暗。Javafx變暗背景
我能做的最好的是一些基本的FadeTransition,它只會讓它們消失,不會變暗,再加上我不知道如何選擇節點的所有子節點,除了一個有鼠標的節點。手動選擇除一個以外的所有子項對於更多對象似乎並不真正有效
我試圖谷歌它,但我只是找不到任何東西。 請發佈一個鏈接到類似問題的線程或示例代碼。任何幫助將非常感激。
您可以使用下面的示例。請注意,有一些假設,例如場景圖中的每個節點都是一個Shape對象,並且每個形狀都有一個與填充關聯的Color對象。示例代碼足以派生出與您的用例特別相關的其他解決方案。
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.stage.Stage;
public class SelectionApp extends Application {
private Pane root = new Pane();
private Parent createContent() {
root.setPrefSize(800, 600);
root.getChildren().add(new Rectangle(800, 600, Color.AQUA));
for (int i = 0; i < 10; i++) {
Circle circle = new Circle(25, 25, 25, Color.GREEN);
// just place them randomly
circle.setTranslateX(Math.random() * 700);
circle.setTranslateY(Math.random() * 500);
circle.setOnMouseEntered(e -> select(circle));
circle.setOnMouseExited(e -> deselect(circle));
root.getChildren().add(circle);
}
return root;
}
private void select(Shape node) {
root.getChildren()
.stream()
.filter(n -> n != node)
.map(n -> (Shape) n)
.forEach(n -> n.setFill(darker(n.getFill())));
}
private void deselect(Shape node) {
root.getChildren()
.stream()
.filter(n -> n != node)
.map(n -> (Shape) n)
.forEach(n -> n.setFill(brighter(n.getFill())));
}
private Color darker(Paint c) {
return ((Color) c).darker().darker();
}
private Color brighter(Paint c) {
return ((Color) c).brighter().brighter();
}
@Override
public void start(Stage primaryStage) throws Exception {
Scene scene = new Scene(createContent());
primaryStage.setTitle("Darken");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
這是完美的!非常感謝! – Patrick
請參閱:[如何在JavaFX中實現節點選擇](http://stackoverflow.com/a/40939611/1155209)(雖然這有點不同...)。 – jewelsea
謝謝! :)) – Patrick