[英]JavaFX: Convert Rectangle to a Polygon
我希望在用戶嘗試單擊邊緣之一並將其拖到場景周圍時將矩形轉換為多邊形。 我開始使用以下代碼對此進行簡單的實現,但似乎沒有辦法(就我所知)到達矩形/多邊形的邊緣以方便此操作。 任何對此的投入將不勝感激。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
class DrawPane extends Pane
{
public DrawPane(final Polygon poly)
{
poly.setFill(Color.BEIGE);
poly.setStroke(Color.CHARTREUSE);
poly.setStrokeWidth(1);
setDragHandler(poly);
getChildren().addAll(poly);
}
private double dragDeltaX, dragDeltaY;
private void setDragHandler(final Polygon poly)
{
poly.setOnMousePressed(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
dragDeltaX = poly.getLayoutX()
- mouseEvent.getSceneX();
dragDeltaY = poly.getLayoutY()
- mouseEvent.getSceneY();
}
});
poly.setOnMouseDragged(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
poly.setRotate(dragDeltaX
+ dragDeltaY);
poly.setCursor(Cursor.MOVE);
}
});
poly.setOnMouseEntered(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
poly.setCursor(Cursor.HAND);
}
});
poly.setOnMouseReleased(new EventHandler<MouseEvent>()
{
@Override
public void handle(MouseEvent mouseEvent)
{
poly.setCursor(Cursor.HAND);
}
});
}
}
public class Rectangle2Polygon extends Application
{
@Override
public void start(Stage stage)
{
Polygon poly = new Polygon(10, 10, 100, 10, 100, 100, 10, 100);
stage.setScene(new Scene(new DrawPane(poly), 450, 300));
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
您可以使用這種方法:
我根據默認的Ensemble示例編寫了一個小演示:
https://gist.github.com/sgrinev/9238167
您可以拖動此“矩形”的頂點以實現如下所示:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.