【发布时间】:2014-02-25 19:48:09
【问题描述】:
当用户尝试单击其中一条边并将其拖动到场景中时,我希望将矩形转换为多边形。我开始使用以下代码对此进行简单实现,但似乎我无法(至少据我所知)矩形/多边形的边缘来促进此操作。对此的任何意见将不胜感激。
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);
}
}
【问题讨论】:
-
您到底想达到什么目标?矩形在拖动过程中应该改变形式吗?还是被多边形取代?
-
抱歉我的问题含糊不清。期望的是,假设用户单击矩形的边缘,创建了一个额外的顶点,并且用户应该能够拖动这个新点来根据他的需要调整新的形状(在这种情况下是多边形)。如果需要更多说明,请告诉我。所以简而言之,为了回答你的疑问,矩形应该在拖动过程中改变形状。
标签: java javafx-2 polygon rectangles