我上面有一个画布和一个graphicsContext。我正在使用clearRect()从画布上加载的图像中删除一些像素。我应该可以放大和缩小和平移图像。我已将画布放在scrollPane中,因此可以在其上平移。我需要知道如何使用鼠标滚动进行缩放。
我尝试调整画布的大小,但这样做时我不得不重新绘制图像,以致编辑丢失!
FXML:
<ScrollPane pannable="true" prefHeight="200.0" prefWidth="200.0" style="-fx-background-color: green;" BorderPane.alignment="CENTER">
<content>
<Canvas fx:id="imageCanvas" height="425.0" width="564.0">
<cursor>
<Cursor fx:constant="OPEN_HAND" />
</cursor>
</Canvas>
</content>
</ScrollPane>
控制器:
imageCanvas.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
if (e.getButton() == MouseButton.PRIMARY) {
System.out.println(e.getX() + " " + e.getY());
gc.drawImage(
source,
(e.getX() - 25) * source.getWidth() / imageCanvas.getWidth(),
(e.getY() - 25) * source.getHeight() / imageCanvas.getHeight(),
50 * source.getWidth() / imageCanvas.getWidth(),
50 * source.getHeight() / imageCanvas.getHeight(),
e.getX() - 25,
e.getY() - 25,
50,
50
);
} else if (e.getButton() == MouseButton.SECONDARY) {
gc.clearRect(e.getX() - 50, e.getY() - 50, 100, 100);
}
}
});