[英]JavaFX: Convert Rectangle to a Polygon
I wish to convert a rectangle to a polygon when a user tries to click on one of the edges and drags it around the scene. 我希望在用户尝试单击边缘之一并将其拖到场景周围时将矩形转换为多边形。 I started to do a simple implementation of this using the following code, but it seems that there is no way I can get to(at-least in my knowledge) the edges of the rectangle/polygon to facilitate this operation.
我开始使用以下代码对此进行简单的实现,但似乎没有办法(就我所知)到达矩形/多边形的边缘以方便此操作。 Any inputs on this would be appreciated.
任何对此的投入将不胜感激。
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);
}
}
You can use this approach: 您可以使用这种方法:
I wrote a small demo based on the default Ensemble example : 我根据默认的Ensemble示例编写了一个小演示:
https://gist.github.com/sgrinev/9238167 https://gist.github.com/sgrinev/9238167
You can drag the vertices of this "rectangle" to achieve something like this: 您可以拖动此“矩形”的顶点以实现如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.