随着向右移动逐渐消失的球体

huangapple go评论118阅读模式
英文:

Ball that fades away as it goes to the right

问题

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.paint.Color;

public class Project3 extends Application
{
   public static void main(String[] args) {
      launch(args);
   }
   
   public void start(Stage primaryStage) {
      Pane root = createRootPane();
      Scene scene1 = new Scene(root);
      primaryStage.setScene(scene1);
      primaryStage.setTitle("Hai Vo");
      primaryStage.show();
   }
   
   public Pane createRootPane()
   {
      Circle ball = new Circle(100, 50, 25);
      Pane root = new Pane(ball);
      root.setMinSize(300, 300); 
      
      root.setOnMouseDragged(event -> {
         double x = event.getX();
         ball.setCenterX(event.getX());
         ball.setCenterY(event.getY());
         
         double opacity = ball.getCenterX() / root.getWidth(); // Convert position to opacity value
         ball.setOpacity(opacity);
      });
      
      return root;   
   }
}
英文:

My task is to write a program that makes a circle ball that gradually fades away as the ball moves to the right. But it's not working, I can make the ball move when the mouse is dragged but the opacity is the same. Can you guys help me? I don't know how to convert the opacity value into double

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.paint.Color;

public class Project3 extends Application
{
   public static void main(String[] args) {
      launch(args);
   }
   public void start(Stage primaryStage) {
      Pane root = createRootPane();
      Scene scene1 = new Scene(root);
      primaryStage.setScene(scene1);
      primaryStage.setTitle(" Hai Vo ");
      primaryStage.show();
   }
   public Pane createRootPane()
   {
      Circle ball = new Circle (100,50,25);
      Pane root = new Pane(ball);
      root.setMinSize(300,300); 
      root.setOnMouseDragged (
       event ->
       {
         double x = event.getX();
         ball.setCenterX(event.getX());
         ball.setCenterY(event.getY());
         ball.opacityProperty().bind(ball.centerXProperty());
         double opacity = ball.opacityProperty();
         ball.setOpacity(opacity);
       } );
       return root;   
   }
}

答案1

得分: 2

首先,将绑定操作移出事件处理程序。绑定操作将确保当centerX属性更新时,不透明度始终会被更新。

其次,不要设置绑定的值;事件处理程序的最后两行只是将不透明度设置为其当前值。

第三,不透明度应该在0和1之间。当centerX为0时,希望不透明度为1,当centerX为300时(或者通常是面板的宽度),希望不透明度为0。

你所需要的是(伪代码):

<!-- language: none -->
opacity = 1 - ball.centerX / root.width
        = (ball.centerX / root.width) * (-1) + 1

你可以使用绑定来表达这个关系:

ball.centerXProperty()
    .divide(root.widthProperty())
    .multiply(-1)
    .add(1)

因此,综合起来,你需要:

public Pane createRootPane() {
    Circle ball = new Circle(100, 50, 25);
    Pane root = new Pane(ball);
    ball.opacityProperty().bind(
        ball.centerXProperty()
            .divide(root.widthProperty())
            .multiply(-1)
            .add(1)
    );
    root.setMinSize(300, 300); 
    root.setOnMouseDragged(event -> {
        ball.setCenterX(event.getX());
        ball.setCenterY(event.getY());
    });
    return root;   
}
英文:

First, move the binding out of the event handler. The binding will ensure the opacity is always updated when the centerX property updates.

Second, don't set bound values; the last two lines of the event handler just set the opacity to its current value anyway.

Third, the opacity should be between 0 and 1. You want it to be 1 when centerX is 0 and 0 when centerX is 300 (or, generally, the width of the pane).

What you need is (in pseudocode)

<!-- language: none -->

opacity = 1 - ball.centerX / root.width
        = (ball.centerX / root.width) * (-1) + 1

which you can express in bindings with

ball.centerXProperty()
    .divide(root.widthProperty())
    .multiply(-1)
    .add(1)

So put together, you need:

public Pane createRootPane() {
   Circle ball = new Circle (100,50,25);
   Pane root = new Pane(ball);
   ball.opacityProperty().bind(
       ball.centerXProperty()
           .divide(root.widthProperty())
           .multiply(-1)
           .add(1)
   );
   root.setMinSize(300,300); 
   root.setOnMouseDragged (
    event -&gt;
    {
      ball.setCenterX(event.getX());
      ball.setCenterY(event.getY());
    } );
    return root;   
 }

huangapple
  • 本文由 发表于 2020年4月11日 03:33:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/61147419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定