java - 动画在两个方向而不是仅向右跟踪 JavaFX 行
问题描述
我编写了一些代码,在 TextArea 的插入符号的右上角生成一行,并在我左键单击时创建以下动画:
- 起初,这条线是一个点(startXY 和 endXY 坐标相同)并且由于圆帽它看起来像一个圆
- “圆”的宽度从 1 增加到 16
- endX 坐标增加 70(这样看起来就像
正在跟踪线)
我不能为此使用实际的圆形或任何其他形状,因为圆形边缘在扩展时也会变形。
这条线也必须是 BorderPane 的子项,项目的其他组件也是如此。我也真的不想为此使用 JTextArea,因为我从未使用过 swing。
我将只包含与这一行相关的代码部分,因为整个项目相当长且混乱,但我可以向您保证 Eclipse 给出的错误或警告为零。宽度增加完美,但线在 X 轴上的两个方向上扩展,而不是仅修改 endX 坐标。
我还应该提到,我已经尝试在一个单独的项目中测试线条扩展动画(如果有帮助,在窗格中的空白画布上)并且它完全按预期工作。
这是不起作用的项目:
//coordinates of top-left corner of textarea:
//X: 30
//Y: 184
BorderPane pane = new BorderPane();
EventHandler<MouseEvent> eventHandler = new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
TextAreaSkin skin = (TextAreaSkin) textarea.getSkin();
Bounds b = skin.getCaretBounds();
//coordinates of the upper tip of the caret, (0,0) being the top-left corner of textarea:
//b.getCenterX();
//b.getMinY();
int adjust = 7;
//type a space char (to which l should be tied, haven't written this part yet)
int spacep = textarea.getCaretPosition();
textarea.setText(textarea.getText().substring(0, spacep) + " " + textarea.getText().substring(spacep, textarea.getText().length()));
//growing animation
int sx = (int)(30 + b.getMaxX() + adjust);
int sy = (int)(184 + b.getMinY() + adjust-1);
int ex = (int)(30 + b.getMaxX() + adjust);
int ey = (int)(184 + b.getMinY() + adjust-1);
Line l = new Line(sx, sy, ex, ey);
l.setStroke(Color.LIGHTGRAY);
l.setStrokeLineCap(StrokeLineCap.ROUND);
l.setStrokeWidth(1);
pane.getChildren().add(l);
ScaleTransition s = new ScaleTransition(Duration.millis(500), l);
s.setByX(16);
s.setByY(16);
s.setCycleCount(1);
s.setAutoReverse(false);
//elongating animation (not working properly!!!)
Timeline t = new Timeline(
new KeyFrame(
Duration.millis(1000),
new KeyValue(l.endXProperty(), ex + 70)
)
);
t.setCycleCount(1);
t.setAutoReverse(false);
SequentialTransition sequentialTransition = new SequentialTransition();
sequentialTransition.getChildren().addAll(
s,
t);
sequentialTransition.setCycleCount(1);
sequentialTransition.setAutoReverse(true);
sequentialTransition.play();
}
};
textarea.addEventFilter(MouseEvent.MOUSE_CLICKED, eventHandler);
pane.setTop(lblbox);
pane.setCenter(textarea);
pane.setStyle(String.format("-fx-background-color: #%s;", s));
BorderPane.setMargin(textarea, new Insets(30));
primaryStage.setScene(new Scene(pane, 500, 500));
primaryStage.show();
}
这是测试项目(有效):
public class FXSample extends Application {
public static void main(String[] args) {
launch(args);
}
final int W = 800;
final int H = 500;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Definition of Color");
Canvas canvas = new Canvas(W, H);
GraphicsContext gc = canvas.getGraphicsContext2D();
Line l = new Line(200, 400, 200, 400);
l.setStroke(Color.LIGHTGRAY);
l.setStrokeLineCap(StrokeLineCap.ROUND);
l.setStrokeWidth(16);
Timeline t = new Timeline(
new KeyFrame(
Duration.millis(1000),
new KeyValue(l.endXProperty(), 500)
)
);
t.setCycleCount(2);
t.play();
Pane root = new Pane();
root.getChildren().add(l);
root.getChildren().add(canvas);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
正如您从第一个片段中看到的那样,我尝试更改很多东西以使其工作,包括将坐标从 double 转换为 int 并为 startXY 和 endXY 创建单独的变量,即使它们具有相同的值开始。
如果您需要更多详细信息,请告诉我!抱歉,顺便说一句令人困惑的措辞,我真的是 java 新手,我的英语不是最好的。
解决方案
推荐阅读
- graphics - 用threejs扭曲网格
- android - 无需在网站上进行 InAppPurchase 外部注册即可解锁内容
- biztalk - WCF-WebHttp 向 Dynamics CRM 发送端口身份验证失败
- java - PGJDBC:无法从客户端接收数据:
- javascript - 防止模态框上的背景滚动
- javascript - 如何在 axios 上做出反应选择参数?
- python - 如何在 jinja 模板中循环遍历字典
- javascript - 具有动态 html 菜单的 iframe 可以在 iframe 之外显示其菜单吗?
- excel - VBA:按类型而不是值过滤列
- python-3.x - 后续:使用NetworkX在Python中添加两个独立图的边连接节点