[英]JavaFX : Draw on Center Pane based on user input in Left Pane
Here is my summary code 这是我的摘要代码
public class KlArgon extends Application {
BorderPane border ;
Scene scene ;
Stage stage;
@Override
public void start(Stage stage) {
:
border = new BorderPane();
:
HBox infoBox = addInfoHBox();
border.setTop(infoBox);
:
VBox menuBox = addMenuVBox();
border.setLeft(menuBox);
:
border.setCenter(addAnchorPane(addGridPane()));
// setRight and setBottom is not used
:
scene = new Scene (border);
stage.setScene(scene);
stage.show();
}
private Node addAnchorPane(GridPane grid) {
AnchorPane anchorpane = new AnchorPane();
anchorpane.getChildren().add(grid);
AnchorPane.setTopAnchor(grid, 10.0);
return anchorpane;
}
private GridPane addGridPane() {
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(0, 10, 0, 10));
grid.add(addWhiteboard(), 1, 0);
return grid;
}
private Node addWhiteboard() {
Canvas canvas = new Canvas (wboardWd, wdboardHt);
GraphicsContext gc = canvas.getGraphicsContext2D();
drawShapes(gc);
drawfromClipboard(gc);
return canvas;
}
}
I refer to the Center pane as the "Whiteboard". 我将“中心”窗格称为“白板”。 Among other things, I have two buttons in
menuBox
- btnCopyFromClipboard
and btnClearWhiteboard
. 除其他外,我在
menuBox
有两个按钮btnCopyFromClipboard
和btnClearWhiteboard
。
When user presses btnCopyFromClipboard
- the user should be able to draw an rectangle in the "Whiteboard" only (ie Center pane only) and then the clipboard image will be copied (scaled) into that rectangle. 当用户按下
btnCopyFromClipboard
-用户应该只能在“白板”中绘制一个矩形(即仅限于中心窗格),然后剪贴板图像将被复制(缩放)到该矩形中。
So I made border
, scene
, stage
as global and I am trying to get this to work - not only it is buggy/ugly- to me it looks like a hack. 所以我将
border
, scene
, stage
为全局,并且我试图使它生效-不仅是越野车/丑陋的-对我来说,它看起来像是黑客。 Is there a cleaner way to do this ie manage Center Pane when button in left pane is pressed? 有没有更清洁的方法来执行此操作,即在按下左窗格中的按钮时管理中心窗格? Basically I want the Center Pane to be the
Canvas
and the GraphicsContext
operations are performed whe the Buttons in Left Pane is pressed. 基本上,我希望将“中窗格”设为“
Canvas
并在按下“左窗格”中的“按钮”时执行GraphicsContext
操作。
What I have working is pressing the btnCopyFromClipboard
lets me draw the rectangle anywhere/everywhere (instead of limiting it to the Center Pane / the whiteboard). 我正在按的是
btnCopyFromClipboard
使我可以在任何地方/任何地方绘制矩形(而不是将其限制在中心窗格/白板上)。 I want to restrict the rectangle to be drawn inside the Center Pane / the whiteboard only. 我想限制仅在中心窗格/白板内部绘制的矩形。
Some inputs/pointers from someone who has been through this will be very much appreciated. 非常感谢通过此操作的某人的一些输入/指针。
http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm was helpful to get me started. http://docs.oracle.com/javafx/2/layout/builtin_layouts.htm有助于我入门。
I was in a fix as well and here is a question which I asked in Oracle Forums, to which, James gave me a vivid reply. 我也处于修复状态,这是我在Oracle论坛中提出的一个问题,James给了我生动的答复。 Please go through this, it has your answer
请通过这个,它有你的答案
https://community.oracle.com/thread/2598756 https://community.oracle.com/thread/2598756
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.