Question

I have this code:

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; }
 }  
}

The problem is that I can't move the edges of the triangle all the way to the top of the scene. If I don't use a stackPane,only a scene it works well but I need the Stack Pane because the next thing I want to do is to save the triangle as an image and use the image as background.Every time I will save a new triangle,it;s image will overlap with the others.Any sugestion is welcomed.Thank you.

Was it helpful?

Solution

At First I wantet to say NICE PROGRAM ;-)
And now to your problem, this problem is easy to fix. ;-)
Just use an AnchorPane and not a Group.

Here is the code:

@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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top