Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well I think your code is OK, seems to run fine for me and operate without any clear issue using jdk7u7 on windows 7.</p> <p>I think maybe you want to call <code>rect.setMouseTransparent(true)</code>, so that the rectangles don't catch the clicks. The mouse transparency thing isn't to do with the adding and removing of rectangles, it's just the way that picking works in JavaFX.</p> <p>You may want to consider placing your Test node in a pannable <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/control/ScrollPane.html" rel="nofollow noreferrer"><code>ScrollPane</code></a> rather than implementing the panning yourself - might need to wrap it in a <code>Group</code> to get the appropriate behaviour. But the code you have is simple and seems to work fine, so perhaps use of a <code>ScrollPane</code> is unnecessary and may even confuse things more.</p> <p>There is a <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/canvas/Canvas.html" rel="nofollow noreferrer"><code>Canvas</code></a> class in Java, which is something different from what you have, so calling your node something other than <code>Canvas</code> is probably a good idea.</p> <p>Not to do with your question, but using a <a href="http://docs.oracle.com/javafx/2/api/javafx/animation/Timeline.html" rel="nofollow noreferrer"><code>Timeline</code></a> is my preferred way to handle animation rather than a <code>Timer</code>, though the <code>Timer</code> method will still work as long as you correctly use <code>Platform.runLater</code> as you are doing.</p> <p>Here's a modified sample using a <code>Timeline</code> and <code>.mouseTransparent()</code> with an added frame counter so that it is clear that animation is happening.</p> <pre><code>import javafx.animation.*; import javafx.application.Application; import javafx.beans.binding.Bindings; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Duration; class Test extends StackPane { private Rectangle rect; private double pressedX, pressedY; private LongProperty frame = new SimpleLongProperty(); public Test() { setMinSize(600, 600); setStyle("-fx-border-color: blue;"); Label count = new Label(); count.textProperty().bind(Bindings.convert(frame)); getChildren().add(count); count.setMouseTransparent(true); setOnMousePressed(new EventHandler&lt;MouseEvent&gt;() { public void handle(MouseEvent event) { pressedX = event.getX(); pressedY = event.getY(); } }); setOnMouseDragged(new EventHandler&lt;MouseEvent&gt;() { public void handle(MouseEvent event) { setTranslateX(getTranslateX() + event.getX() - pressedX); setTranslateY(getTranslateY() + event.getY() - pressedY); event.consume(); } }); Timeline t = new Timeline(new KeyFrame(Duration.millis(100), new EventHandler&lt;ActionEvent&gt;() { @Override public void handle(ActionEvent event) { frame.set(frame.get() + 1); if (rect != null) { getChildren().remove(rect); } rect = new Rectangle(10, 10, 200, 200); rect.setFill(Color.RED); rect.setMouseTransparent(true); getChildren().add(0, rect); } })); t.setCycleCount(Timeline.INDEFINITE); t.play(); } } public class TestApplication extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) { stage.setScene(new Scene(new Test())); stage.show(); } } </code></pre> <p><img src="https://i.stack.imgur.com/8nvk3.png" alt="Sample program output"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload