- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何在 JavaFX 中使用形状的边框来更改其属性之一 - 高度、宽度、半径等。
我试过在圆形上使用一个圆来调整半径,但我想知道我是否可以使用形状的边界来做到这一点。
这是我的自定义圈类:
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<MouseEvent> circleOnMouseClickedEventHandler = new EventHandler<MouseEvent>(){
@Override
public void handle(MouseEvent t ){
}
};
EventHandler<MouseEvent> circleOnMousePressedEventHandler = new EventHandler<MouseEvent>(){
@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<MouseEvent> circleOnMouseDraggedEventHandler
= new EventHandler<MouseEvent>() {
@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);
}
};
}
最佳答案
我在这里所做的是在 Pane 中放置几个形状,然后允许用户单击这些形状来选择它们。选择形状用边界框包围形状的边界(不是边框)。边界框的角和侧边中心有 anchor 。用户可以拖动边界框来移动选定的形状。用户可以拖动 anchor 来调整所选形状的大小。
这是一些代码,因为要完成它还需要做一些工作。代码可以稍微清理一下,但相对来说还可以。
调整应用大小
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.stage.Stage;
public class ResizingSample extends Application {
private Pane root;
private Node selectedNode;
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(final Stage stage) throws Exception {
Ellipse ellipse = new Ellipse(100, 100, 50, 50);
ellipse.setFill(Color.AQUAMARINE);
Rectangle rectangle = new Rectangle(200, 250, 100, 100);
rectangle.setFill(Color.PALEGREEN);
root = new Pane(
ellipse,
rectangle
);
stage.setScene(
new Scene(
root,
400, 400, Color.ALICEBLUE
)
);
stage.show();
root.setOnMouseClicked(event -> {
final Parent parentNode = ((Node) event.getTarget()).getParent();
if (selectedNode != null && !(parentNode instanceof ResizingControl)) {
root.getChildren().removeIf(candidate -> candidate instanceof ResizingControl);
selectedNode = null;
}
});
makeSelectable(ellipse, rectangle);
}
private void makeSelectable(Node... nodes) {
for (Node node: nodes) {
node.setOnMouseClicked(event -> {
if (selectedNode != node) {
root.getChildren().removeIf(candidate -> candidate instanceof ResizingControl);
selectedNode = node;
node.toFront();
ResizingControl resizingControl = new ResizingControl(node);
root.getChildren().add(resizingControl);
}
event.consume();
});
}
}
}
class ResizingControl extends Group {
private Node targetNode = null;
private final Rectangle boundary = new Rectangle();
private Anchor topLeft = new Anchor(Color.GOLD, true, true, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() - (newX - oldX);
if (newWidth > 0) {
boundary.setX(newX);
boundary.setWidth(newWidth);
}
double newHeight = boundary.getHeight() - (newY - oldY);
if (newHeight > 0) {
boundary.setY(newY);
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor topCenter = new Anchor(Color.GOLD, false, true, (oldX, oldY, newX, newY) -> {
double newHeight = boundary.getHeight() - (newY - oldY);
if (newHeight > 0) {
boundary.setY(newY);
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor topRight = new Anchor(Color.GOLD, true, true, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() + (newX - oldX);
if (newWidth > 0) {
boundary.setWidth(newWidth);
}
double newHeight = boundary.getHeight() - (newY - oldY);
if (newHeight > 0) {
boundary.setY(newY);
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor rightCenter = new Anchor(Color.GOLD, true, false, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() + (newX - oldX);
if (newWidth > 0) {
boundary.setWidth(newWidth);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor bottomRight = new Anchor(Color.GOLD, true, true, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() + (newX - oldX);
if (newWidth > 0) {
boundary.setWidth(newWidth);
}
double newHeight = boundary.getHeight() + (newY - oldY);
if (newHeight > 0) {
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor bottomCenter = new Anchor(Color.GOLD, false, true, (oldX, oldY, newX, newY) -> {
double newHeight = boundary.getHeight() + (newY - oldY);
if (newHeight > 0) {
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor bottomLeft = new Anchor(Color.GOLD, true, true, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() - (newX - oldX);
if (newWidth > 0) {
boundary.setX(newX);
boundary.setWidth(newWidth);
}
double newHeight = boundary.getHeight() + (newY - oldY);
if (newHeight > 0) {
boundary.setHeight(newHeight);
}
updateAnchorPositions();
resizeTargetNode();
});
private Anchor leftCenter = new Anchor(Color.GOLD, true, false, (oldX, oldY, newX, newY) -> {
double newWidth = boundary.getWidth() - (newX - oldX);
if (newWidth > 0) {
boundary.setX(newX);
boundary.setWidth(newWidth);
}
updateAnchorPositions();
resizeTargetNode();
});
ResizingControl(Node targetNode) {
this.targetNode = targetNode;
attachBoundingRectangle(targetNode);
attachAnchors();
boundary.toBack();
}
private void attachBoundingRectangle(Node node) {
Bounds bounds = node.getBoundsInParent();
boundary.setStyle(
"-fx-stroke: forestgreen; " +
"-fx-stroke-width: 2px; " +
"-fx-stroke-dash-array: 12 2 4 2; " +
"-fx-stroke-dash-offset: 6; " +
"-fx-stroke-line-cap: butt; " +
"-fx-fill: rgba(255, 228, 118, .5);"
);
boundary.setX(bounds.getMinX());
boundary.setY(bounds.getMinY());
boundary.setWidth(bounds.getWidth());
boundary.setHeight(bounds.getHeight());
Util.makeDraggable(boundary, (oldX, oldY, newX, newY) -> {
updateAnchorPositions();
relocateTargetNode(newX, newY);
});
getChildren().add(boundary);
}
private void relocateTargetNode(double newX, double newY) {
if (targetNode instanceof Ellipse) {
Ellipse ellipse = (Ellipse) targetNode;
ellipse.setCenterX(newX + ellipse.getRadiusX());
ellipse.setCenterY(newY + ellipse.getRadiusY());
} else if (targetNode instanceof Rectangle) {
Rectangle rectangle = (Rectangle) targetNode;
rectangle.setX(newX);
rectangle.setY(newY);
}
}
private void resizeTargetNode() {
if (targetNode instanceof Ellipse) {
Ellipse ellipse = (Ellipse) targetNode;
ellipse.setRadiusX(boundary.getWidth() / 2);
ellipse.setRadiusY(boundary.getHeight() / 2);
relocateTargetNode(boundary.getX(), boundary.getY());
} else if (targetNode instanceof Rectangle) {
Rectangle rectangle = (Rectangle) targetNode;
rectangle.setWidth(boundary.getWidth());
rectangle.setHeight(boundary.getHeight());
relocateTargetNode(boundary.getX(), boundary.getY());
}
}
private void attachAnchors() {
updateAnchorPositions();
getChildren().addAll(
topLeft,
topCenter,
topRight,
rightCenter,
bottomRight,
bottomCenter,
bottomLeft,
leftCenter
);
}
private void updateAnchorPositions() {
topLeft.setCenterX(boundary.getX());
topLeft.setCenterY(boundary.getY());
topCenter.setCenterX(boundary.getX() + boundary.getWidth() / 2);
topCenter.setCenterY(boundary.getY());
topRight.setCenterX(boundary.getX() + boundary.getWidth());
topRight.setCenterY(boundary.getY());
rightCenter.setCenterX(boundary.getX() + boundary.getWidth());
rightCenter.setCenterY(boundary.getY() + boundary.getHeight() / 2);
bottomRight.setCenterX(boundary.getX() + boundary.getWidth());
bottomRight.setCenterY(boundary.getY() + boundary.getHeight());
bottomCenter.setCenterX(boundary.getX() + boundary.getWidth() / 2);
bottomCenter.setCenterY(boundary.getY() + boundary.getHeight());
bottomLeft.setCenterX(boundary.getX());
bottomLeft.setCenterY(boundary.getY() + boundary.getHeight());
leftCenter.setCenterX(boundary.getX());
leftCenter.setCenterY(boundary.getY() + boundary.getHeight() / 2);
}
}
interface DragHandler {
void handle(double oldX, double oldY, double newX, double newY);
}
// a draggable anchor displayed around a point.
class Anchor extends Circle {
Anchor(Color color, boolean canDragX, boolean canDragY, DragHandler dragHandler) {
super(0, 0, 5);
setFill(color.deriveColor(1, 1, 1, 0.5));
setStroke(color);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
Util.enableDrag(this, canDragX, canDragY, dragHandler);
}
}
class Util {
// make a targetNode movable by dragging it around with the mouse.
static void enableDrag(Circle node, boolean canDragX, boolean canDragY, DragHandler dragHandler) {
final Delta dragDelta = new Delta();
node.setOnMousePressed(mouseEvent -> {
// record a delta distance for the drag and drop operation.
dragDelta.x = node.getCenterX() - mouseEvent.getX();
dragDelta.y = node.getCenterY() - mouseEvent.getY();
node.getScene().setCursor(Cursor.MOVE);
});
node.setOnMouseReleased(mouseEvent -> {
node.getScene().setCursor(Cursor.HAND);
});
node.setOnMouseDragged(mouseEvent -> {
double oldX = node.getCenterX();
double oldY = node.getCenterY();
double newX = mouseEvent.getX() + dragDelta.x;
if (canDragX && newX > 0 && newX < node.getScene().getWidth()) {
node.setCenterX(newX);
}
double newY = mouseEvent.getY() + dragDelta.y;
if (canDragY && newY > 0 && newY < node.getScene().getHeight()) {
node.setCenterY(newY);
}
newX = node.getCenterX();
newY = node.getCenterY();
if (dragHandler != null && (newX != oldX || newY != oldY)) {
dragHandler.handle(oldX, oldY, newX, newY);
}
});
node.setOnMouseEntered(mouseEvent -> {
if (!mouseEvent.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.HAND);
}
});
node.setOnMouseExited(mouseEvent -> {
if (!mouseEvent.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
}
// make a targetNode movable by dragging it around with the mouse.
static void makeDraggable(Rectangle node, DragHandler dragHandler) {
final Delta dragDelta = new Delta();
node.setOnMouseEntered(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.HAND);
}
});
node.setOnMouseExited(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMousePressed(me -> {
if (me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
dragDelta.x = me.getX() - node.getX();
dragDelta.y = me.getY() - node.getY();
node.getScene().setCursor(Cursor.MOVE);
});
node.setOnMouseReleased(me -> {
if (!me.isPrimaryButtonDown()) {
node.getScene().setCursor(Cursor.DEFAULT);
}
});
node.setOnMouseDragged(me -> {
double oldX = node.getX();
double oldY = node.getY();
node.setX(me.getX() - dragDelta.x);
node.setY(me.getY() - dragDelta.y);
double newX = node.getX();
double newY = node.getY();
if (dragHandler != null && (newX != oldX || newY != oldY)) {
dragHandler.handle(oldX, oldY, newX, newY);
}
});
}
// records relative x and y co-ordinates.
private static class Delta {
double x, y;
}
}
关于java - 如何使用 JavaFX 中的边框更改形状属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41267418/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!