2016-12-29 4 views
0

AnchorPaneに10個の円を持つFXMLアプリケーションがあります。私は1つの円の上にマウスを置いて、他の9と背景を暗くしたい。Javafx darken background

私ができることは、基本的なフェードトランジションだけであり、消えて暗くなっていないのですが、マウスを持っているノード以外のノードのすべての子供を選択する方法がわかりません。 1つを除いてすべての子供を手動で選択することは、もっと多くのオブジェクトに対して効率的ではないようです。

私はそれをgoogleしようとしましたが、私は何も見つけることができません。 同様の問題またはサンプルコードに関連するスレッドへのリンクを投稿してください。どんな助けでも本当に感謝しています。

+0

関連項目:[JavaFXでのノードの選択方法(http://stackoverflow.com/a/40939611/1155209)(ただしこれは少し異なります...) – jewelsea

+0

ありがとうございました! :)) – Patrick

答えて

2

次のサンプルを使用できます。シーングラフのすべてのノードが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); 
    } 
} 
+0

それはちょうど完璧です!どうもありがとうございます! – Patrick