javafx边框线_如何在JavaFX中使用其边框更改形状属性?

本文介绍了一种在JavaFX中通过形状边框来改变形状属性的方法,特别是调整圆的半径。作者提供了一个名为NewCircle的自定义类,并实现了鼠标点击和拖动来移动形状的功能。此外,还展示了一个更复杂的解决方案,允许用户通过选择形状并拖动边界框或锚点来调整形状大小和位置。
摘要由CSDN通过智能技术生成

bd96500e110b49cbb3cd949968f18be7.png

How can I use the borders of a shape in JavaFX in order to change one of its properties - height, width, radius, etc.

I've tried doing it using a circle on a circle shape in order to resize the radius, but I was wondering if I can do it using the borders of a shape.

Here it is my custom circle class:

public class NewCircle extends Circle {

public NewCircle (double x, double y , double radius, Color colore){

super(x,y,radius);

this.setFill(colore);

this.setOnMousePressed(circleOnMousePressedEventHandler);

this.setOnMouseDragged(circleOnMouseDraggedEventHandler);

}

double orgSceneX, orgSceneY;

double orgTranslateX, orgTranslateY;

EventHandler circleOnMouseClickedEventHandler = new EventHandler(){

@Override

public void handle(MouseEvent t ){

}

};

EventHandler circleOnMousePressedEventHandler = new EventHandler(){

@Override

public void handle(MouseEvent t){

orgSceneX = t.getSceneX();

orgSceneY = t.getSceneY();

Node source = (Node) t.getSource();

orgTranslateX = ((Circle) (t.getSource())).getTranslateX();

orgTranslateY = ((Circle) (t.getSource())).getTranslateY();

((Circle)t.getSource()).toFront();;

}

};

EventHandler circleOnMouseDraggedEventHandler

= new EventHandler() {

@Override

public void handle(MouseEvent t) {

Node source = (Node) t.getSource();

Bounds sceneBounds = source.getScene().getRoot().getLayoutBounds();

Bounds localBounds = source.getBoundsInLocal();

double offsetX = t.getSceneX() - orgSceneX;

double offsetY = t.getSceneY() - orgSceneY;

double newTranslateX = orgTranslateX + offsetX;

double newTranslateY = orgTranslateY + offsetY;

// restirct x movement to scene bounds

if (offsetX >= 0) {

if (localBounds.getMaxX() + newTranslateX > sceneBounds.getMaxX()) {

newTranslateX = sceneBounds.getMaxX() - localBounds.getMaxX();

}

} else {

if (localBounds.getMinX() + newTranslateX < 0) {

newTranslateX = -localBounds.getMinX();

}

}

// restrict y movement to scene bounds

if (offsetY >= 0) {

if (localBounds.getMaxY() + newTranslateY > sceneBounds.getMaxY()) {

newTranslateY = sceneBounds.getMaxY() - localBounds.getMaxY();

}

} else {

if (localBounds.getMinY() + newTranslateY < 0) {

newTranslateY = -localBounds.getMinY();

}

}

source.setTranslateX(newTranslateX);

source.setTranslateY(newTranslateY);

}

};

}

解决方案

What I've done here is put a couple of shapes on a pane, then allow the user to click on the shapes to select them. Selecting the shapes surrounds the bounds of the shapes (not the borders) with a bounding box. The bounding box has anchors on it's corners and side centers. The user

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值