我有这段代码:
package javafxapplication9;
import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.*;
import javafx.collections.*;
import javafx.event.EventHandler;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;
public class JavaFXApplication9 extends Application {
StackPane pane;
public static void main(String[] args) throws Exception { launch(args); }
@Override public void start(final Stage stage) throws Exception {
Polygon triangle = createStartingTriangle();
pane=new StackPane();
Group root = new Group();
root.getChildren().add(triangle);
root.getChildren().addAll(createControlAnchorsFor(triangle.getPoints()));
pane.getChildren().add(root);
pane.setPrefWidth(600);
pane.setPrefHeight(600);
stage.setTitle("Triangle Manipulation Sample");
stage.setScene(
new Scene(
pane,
400, 400, Color.ALICEBLUE
)
);
stage.show();
}
// creates a triangle.
private Polygon createStartingTriangle() {
Polygon triangle = new Polygon();
triangle.getPoints().setAll(
100d, 100d,
150d, 50d,
250d, 150d
);
triangle.setStroke(Color.FORESTGREEN);
triangle.setStrokeWidth(4);
triangle.setStrokeLineCap(StrokeLineCap.ROUND);
triangle.setFill(Color.CORNSILK.deriveColor(0, 1.2, 1, 0.6));
return triangle;
}
private ObservableList<Anchor> createControlAnchorsFor(final ObservableList<Double> points) {
ObservableList<Anchor> anchors = FXCollections.observableArrayList();
for (int i = 0; i < points.size(); i+=2) {
final int idx = i;
DoubleProperty xProperty = new SimpleDoubleProperty(points.get(i));
DoubleProperty yProperty = new SimpleDoubleProperty(points.get(i + 1));
xProperty.addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> ov, Number oldX, Number x) {
points.set(idx, (double) x);
}
});
yProperty.addListener(new ChangeListener<Number>() {
@Override public void changed(ObservableValue<? extends Number> ov, Number oldY, Number y) {
points.set(idx + 1, (double) y);
}
});
Anchor an=new Anchor(Color.GOLD, xProperty, yProperty,pane);
anchors.add(an);
}
return anchors;
}
class Anchor extends Circle {
Anchor(Color color, DoubleProperty x, DoubleProperty y,StackPane pane) {
super(x.get(), y.get(), 10);
setFill(color.deriveColor(1, 1, 1, 0.5));
setStroke(color);
setStrokeWidth(2);
setStrokeType(StrokeType.OUTSIDE);
x.bind(centerXProperty());
y.bind(centerYProperty());
enableDrag();
}
// make a node movable by dragging it around with the mouse.
private void enableDrag() {
final Delta dragDelta = new Delta();
setOnMousePressed(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
// record a delta distance for the drag and drop operation.
dragDelta.x = getCenterX() - mouseEvent.getX();
dragDelta.y = getCenterY() - mouseEvent.getY();
pane.setCursor(Cursor.MOVE);
}
});
setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
pane.setCursor(Cursor.HAND);
}
});
setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
double newX = mouseEvent.getX() + dragDelta.x;
if (newX > 0 && newX < pane.getWidth()) {
setCenterX(newX);
}
double newY = mouseEvent.getY() + dragDelta.y;
if (newY > 0 && newY < pane.getHeight()) {
setCenterY(newY);
}
}
});
setOnMouseEntered(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
pane.setCursor(Cursor.HAND);
}
}
});
setOnMouseExited(new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent mouseEvent) {
if (!mouseEvent.isPrimaryButtonDown()) {
pane.setCursor(Cursor.DEFAULT);
}
}
});
}
// records relative x and y co-ordinates.
private class Delta { double x, y; }
}
}
问题在于我无法将三角形的边缘一直移动到场景的顶部。 如果我不使用stackPane,只有一个它运作良好的场景,但我需要Stack Pane,因为我要做的下一件事是将三角形保存为图像并使用图像作为背景。每次我都会保存一个新的三角形,它的形象将与其他三角形重叠。欢迎任何消化。谢谢。
答案 0 :(得分:2)
起初我想说NICE PROGRAM ;-)
而现在你的问题,这个问题很容易解决。 ;-)
只需使用AnchorPane而不是Group。
以下是代码:
@Override
public void start(final Stage stage) throws Exception {
Polygon triangle = createStartingTriangle();
pane = new StackPane();
//Use an AnchorPane instead of a Group ;)
AnchorPane ap = new AnchorPane();
ap.getChildren().add(triangle);
ap.getChildren().addAll(createControlAnchorsFor(triangle.getPoints()));
pane.getChildren().add(ap);
pane.setPrefWidth(600);
pane.setPrefHeight(600);
stage.setTitle("Triangle Manipulation Sample");
stage.setScene(
new Scene(
pane,
400, 400, Color.ALICEBLUE));
stage.show();
}