所以,我的橡皮筋课上有一个错误,我似乎无法修复。

我基本上要做的是:我有一个borderpane,它是

我要调整大小的节点的外部窗格。我为该borderpane分配了一个宽度为1

像素的边框(查看CSS)。我还为该边框窗格分配了四个矩形,每个矩形位于一个

角(NE,SE,SW,NW)。在此边框中,我具有所谓的

“ contentPane”。该窗格包含所有内容(矩形,

图像视图等)。

它工作得很好,但是我似乎无法修复错误。

错误:

如果我调整一个像素的大小,则宽度,高度/ x和y会使用

未知值进行调整。之后,调整大小就可以了。在此处输入图片说明

RubberBand2.java

package org.displee.javafx.mod;

import javafx.scene.Cursor;

import javafx.scene.input.MouseEvent;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.scene.shape.Rectangle;

/**

* A class representing a rubberband that can be used to resize panes.

* @author Displee

*/

public class RubberBand2 {

/**

* The parent of this rectangle.

*/

private final Pane parent;

/**

* The rectangle.

*/

private final Pane node;

/**

* The corner rectangles.

*/

private final Rectangle[] rectangles = new Rectangle[4];

/**

* The last known node width.

*/

private double nodeWidth;

/**

* The last known node height.

*/

private double nodeHeight;

/**

* The last known node x-coordinate.

*/

private double nodeX;

/**

* The last known node y-coordinate.

*/

private double nodeY;

/**

* The current selected component.

*/

public static RubberBand2 SELECTED;

/**

* The size of the corners of a rectangle.

*/

public static final double RECTANGLE_CORNER_SIZE = 5.0;

/**

* The minimum width of the {@code node}.

*/

private static final double MIN_WIDTH = 10.0;

/**

* The minimum height of the {@code node}

*/

private static final double MIN_HEIGHT = 10.0;

public RubberBand2(Pane node) {

this.node = node;

this.parent = (Pane) node.getParent();

parent.getStyleClass().add("transparent_border");

createCorners();

bind();

}

/**

* Create the corners.

*/

public void createCorners() {

final Pane inheritPane = node;

for (int i = 0; i < rectangles.length; i++) {

final Rectangle rectangle = rectangles[i] = new Rectangle();

rectangle.setWidth(RECTANGLE_CORNER_SIZE);

rectangle.setHeight(RECTANGLE_CORNER_SIZE);

rectangle.setFill(Color.BLACK);

rectangle.getStyleClass().add("rectangle_corner");

rectangle.setArcHeight(4);

rectangle.setArcWidth(4);

if (i == 0) {

rectangle.xProperty().bind(inheritPane.layoutXProperty().subtract(RECTANGLE_CORNER_SIZE));

rectangle.yProperty().bind(inheritPane.layoutYProperty().subtract(RECTANGLE_CORNER_SIZE));

rectangle.setOnMouseEntered((e) -> {

rectangle.setCursor(Cursor.NW_RESIZE);

});

rectangle.setOnMouseDragged((event) -> {

resize(event, 0);

});

} else if (i == 1) {

rectangle.xProperty().bind(inheritPane.layoutXProperty().add(inheritPane.widthProperty()));

rectangle.yProperty().bind(inheritPane.layoutYProperty().subtract(RECTANGLE_CORNER_SIZE));

rectangle.setOnMouseEntered((e) -> {

rectangle.setCursor(Cursor.NE_RESIZE);

});

rectangle.setOnMouseDragged((event) -> {

resize(event, 1);

});

} else if (i == 2) {

rectangle.xProperty().bind(inheritPane.layoutXProperty().add(inheritPane.widthProperty()));

rectangle.yProperty().bind(inheritPane.layoutYProperty().add(inheritPane.heightProperty()));

rectangle.setOnMouseEntered((e) -> {

rectangle.setCursor(Cursor.SE_RESIZE);

});

rectangle.setOnMouseDragged((event) -> {

resize(event, 2);

});

} else {

rectangle.xProperty().bind(inheritPane.layoutXProperty().subtract(RECTANGLE_CORNER_SIZE));

rectangle.yProperty().bind(inheritPane.layoutYProperty().add(inheritPane.heightProperty()));

rectangle.setOnMouseEntered((e) -> {

rectangle.setCursor(Cursor.SW_RESIZE);

});

rectangle.setOnMouseDragged((event) -> {

resize(event, 3);

});

}

rectangle.setOnMousePressed((e) -> {

setDefaults();

e.consume();

});

rectangle.setVisible(false);

parent.getChildren().add(rectangle);

}

}

/**

* Bind the mouse events.

*/

public void bind() {

node.setOnMouseEntered((e) -> {

node.setCursor(Cursor.MOVE);

});

parent.setOnMouseClicked((e) -> {

if (SELECTED != null) {

SELECTED.setRubberBandSelection(false);

}

SELECTED = this;

setRubberBandSelection(true);

e.consume();

});

node.setOnMouseClicked((e) -> {

if (SELECTED != null) {

SELECTED.setRubberBandSelection(false);

}

SELECTED = this;

setRubberBandSelection(true);

e.consume();

});

node.setOnMousePressed((e) -> {

setDefaults();

e.consume();

});

node.setOnMouseMoved((e) -> {

});

node.setOnMouseReleased((e) -> {

});

}

/**

* Resize the argued resize type.

* @param event The mouse event

* @param type The type (0 = NW, 1 = NE, 2 = SE, 3 = SW);

*/

public void resize(MouseEvent event, int type) {

final double mouseX = parent.getBoundsInParent().getMinX() + event.getX();

final double mouseY = parent.getBoundsInParent().getMinY() + event.getY();

double newX = nodeX;

double newY = nodeY;

double newW = nodeWidth;

double newH = nodeHeight;

switch (type) {

case 0:

newX = mouseX;

newY = mouseY;

newW = nodeWidth + nodeX - newX;

newH = nodeHeight + nodeY - newY;

break;

case 1:

newY = mouseY;

newW = mouseX - nodeX;

newH = nodeHeight + nodeY - newY;

break;

case 2:

newW = mouseX - nodeX;

newH = mouseY - nodeY;

break;

case 3:

newX = mouseX;

newW = nodeWidth + nodeX - newX;

newH = mouseY - nodeY;

break;

}

parent.setLayoutX(newX);

parent.setLayoutY(newY);

node.setPrefSize(newW, newH);

}

/**

* Set the defaults before we resize anything.

*/

public void setDefaults() {

nodeX = node.getBoundsInParent().getMinX();

nodeY = node.getBoundsInParent().getMinY();

nodeHeight = node.getBoundsInParent().getHeight();

nodeWidth = node.getBoundsInParent().getWidth();

}

/**

* Set the rubber band selection for the rectangle.

* @param show If we have to show the corner rectangles.

*/

public void setRubberBandSelection(boolean show) {

if (show) {

parent.getStyleClass().remove("transparent_border");

parent.getStyleClass().add("dotted_pane");

} else {

parent.getStyleClass().remove("dotted_pane");

parent.getStyleClass().add("transparent_border");

}

for (Rectangle rectangle : rectangles) {

rectangle.setVisible(show);

}

}

}

style.css

.dotted_pane {

-fx-background-insets: 0;

-fx-border-color: white;

-fx-border-width: 1;

-fx-border-style: dashed;

}

.transparent_border {

-fx-background-insets: 0;

-fx-border-color: transparent;

-fx-border-width: 1;

-fx-border-style: solid;

}

Test2.java

package org.test;

import org.displee.javafx.mod.RubberBand2;

import javafx.application.Application;

import javafx.geometry.Insets;

import javafx.scene.Group;

import javafx.scene.Scene;

import javafx.scene.layout.Background;

import javafx.scene.layout.BackgroundFill;

import javafx.scene.layout.Border;

import javafx.scene.layout.BorderPane;

import javafx.scene.layout.BorderStroke;

import javafx.scene.layout.BorderStrokeStyle;

import javafx.scene.layout.BorderWidths;

import javafx.scene.layout.CornerRadii;

import javafx.scene.layout.Pane;

import javafx.scene.paint.Color;

import javafx.stage.Stage;

public class Test2 extends Application {

public static void main(String[] args) {

Application.launch(args);

}

@Override

public void start(Stage primaryStage) throws Exception {

Group root = new Group();

Scene scene = new Scene(root, 900, 600);

Pane inner = new Pane();

inner.setBackground(new Background(new BackgroundFill(Color.web("red"), CornerRadii.EMPTY, Insets.EMPTY)));

BorderPane border = new BorderPane(inner);

inner.setPrefSize(100, 100);

border.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, BorderWidths.DEFAULT)));

Pane outer = new Pane(border);

outer.layoutXProperty().bind(scene.widthProperty().divide(4));

outer.layoutYProperty().bind(scene.heightProperty().divide(4));

root.getChildren().addAll(outer);

primaryStage.setScene(scene);

primaryStage.show();

new RubberBand2(inner);

}

}

Any suggestions and improvements are of course much appreciated.

Edit: Sorry, my documentation is pretty outdated xD.

Thanks.

java 调错_Java FX Rubberband调整大小错误相关推荐

  1. java常见的报错_Java中常见的错误有哪些?

    原标题:Java中常见的错误有哪些? 1.java.lang.Error 错误.是所有错误的基类,用于标识严重的程序运行问题.这些问题通常描述一些不应被应用程序捕获的反常情况. 原因: 1.对系统所访 ...

  2. java outofmemory 处理_java.lang.OutOfMemoryError处理错误

    原因: 常见的有以下几种: 1.内存中加载的数据量过于庞大,如一次从数据库取出过多数据: 2.集合类中有对对象的引用,使用完后未清空,使得JVM不能回收: 3.代码中存在死循环或循环产生过多重复的对象 ...

  3. java小括号报错_JAVA新人常犯错误集锦

    学习程序设计,最怕的事情可能就是遇到错误却找不到错在哪里.这里列举一下笔者发现的JAVA新人常犯的错误,供目前正在上笔者JAVA课程的学生参考,其他JAVA初学者亦可借鉴. 1.没有区分中英文 在JA ...

  4. java hadoop2.6.0 读取文件报错_java 程序访问hdfs错误 hadoop2.2.0

    很奇怪的问题,程序在eclipse上跑没问题: 这就代码:FileSystem fs = FileSystem.get(URI.create(hdfs_file),  conf , "use ...

  5. java易错_java易错基础知识点

    一. Switch 1.其能接受的数据类型有四个,char , byte, short, int 2.Default 可放在switch中的任何一个地方,但只有给定的条件匹配不到时,才会执行 3.Ca ...

  6. java 调内存_java内存设置

    最近进入天猫物流做运营支撑,需要在不同系统之间切换来切换去.由于各个系统的规模不一,所以遇到了一下在eclipse里调整VM启动参数的问题,拿出来分享下. JVM启动以后,会分配两类内存区域,一类用于 ...

  7. java序列化错在哪里_Spark序列化错误:java.io.NotSerializableException

    由于spark算子用到的class没有实现序列化,报错如下所示 15/11/23 14:43:47 ERROR Executor: Exception in task 0.0 in stage 4.0 ...

  8. instanceof java 报错_java中instanceof怎么理解?java中instanc 爱问知识人

    1.用法表达式    result = object instanceof class     result:布尔类型.     object:必选项.任意对象表达式.     class:必选项.任 ...

  9. java异常标记_java.lang.RuntimeException:错误:0D0680A8:asn1编码例程:ASN1_CHECK_TLEN:错误的标记...

    我收到此错误(在标题中).我不确定为什么,请帮忙.代码如下: public static String decryptRSA(Context mContext, byte[] message) thr ...

  10. java常问的报错_java常见报错及解决

    Java常见报错信息: Java Exception: 1.Error 2.Runtime Exception 运行时异常 3.Exception 4.throw 用户自定义异常 异常类分两大类型:E ...

最新文章

  1. golang 安装一个项目下的所有依赖
  2. java uml 为什么_Java开发为什么需要UML
  3. 用 gdb 调试 GCC 程序
  4. React Native 集成
  5. 【Java Web开发指南】Mybatis一对多关联映射
  6. matlab绘制贝叶斯曲线,Matlab建立SVM,KNN和朴素贝叶斯模型分类绘制ROC曲线
  7. bio-linux_Bio-Linux:稳定,可移植的科学研究Linux发行版
  8. 蓝桥杯 基础练习 高精度加法
  9. EDSR dataloader.py代码问题
  10. An Empirical Analysis of Anonymity in Zcash
  11. 常用APP签名存档以及获取签名的几种方式介绍
  12. 斯特林公式 ——Stirling公式(取N阶乘近似值)
  13. android透明背景边框线
  14. windows server 2008 enterprise r2 x64 激活小记
  15. 针对跨页三线表,在Word2016及以上版本中设置表标题和表头在下一页重复以及解决表格跨页处没有下框线的问题
  16. 重装系统后必装的5大软件,让你大幅度提升工作效率
  17. ES-07-ElasticSearch常用插件
  18. 2021-6-9-今日收获
  19. sqlServer 身份认证 登录
  20. 迷阵突围(dijkstra求次短路径)

热门文章

  1. loss下降auc下降_从基本原理到梯度下降算法:零基础也能看懂的神经网络教程...
  2. java打印取消页眉页脚_Javascript页面打印的页眉页脚的清除与设置
  3. 有趣--等额本息还款
  4. XCELSIUS例子(CX钻取).
  5. 经济学常识之破窗谬论
  6. Visual Basic
  7. 湖北省211大学计算机分数线,武汉7所211大学湖北省录取分数线2020
  8. python:随机产生n个数
  9. MySQL联合主键保存_mysql联合主键
  10. {转]太经典了,我不得不收藏