删除形状后..我不能再旋转了.. - JavaFX
After Deleting Shape.. I Cannot Rotate Anymore.. - JavaFX
我目前有一个程序可以在场景中添加矩形。这些可以 select 编辑、旋转、调整大小和删除。如果我在一个矩形上 select 并单击旋转它有效,如果我添加另一个矩形并单击旋转它仍然有效。但是,如果我 'DELETE' 其中一个矩形,则我无法旋转另一个矩形,也无法再旋转任何其他添加的矩形。
我有以下字段和两个方法:
private List<Node> selectedShapes = new ArrayList<>();
private double angle[] = {0};
@FXML
private AnchorPane container2;
旋转:
public void rotateObject(ActionEvent event) throws IOException{
angle[0] = angle[0] + 45;
selectedShapes.get(0).setRotate(angle[0]);
}
删除
public void deleteButton(ActionEvent e) {
deletebutton.setOnAction(a -> container2.getChildren().removeAll(selectedShapes));
selectedShapes.remove(0);
}
我觉得我 select 编辑或删除 select 编辑的形状不正确。有人可以指导我 select 当前项目的正确方法。因此,如果我删除其中一个矩形,另一个应该仍然可以旋转。谢谢
您想创建一个矩形列表,而不是通过基础 class Node
为什么不直接使用 Rectangle/Shape 的列表:
List<Shape> selectedShapes = new ArrayList<>();
对于你的问题,你使用了一个数组angle[]
,这个不能扩展,而且你只调用索引 0,我明白(如果我没记错的话)这个数组是为了存储每个Rectangle
的角度来解决它也在那里使用列表:
List<Double> angle = new ArrayList<>();
最后要在您的列表中找到您自己,请使用您递增(在添加时)和递减(在删除时)的整数:
int Index = 0;
编辑:
以下是您可以用来组织项目的简单方法:
private List<Rectangle> recs = new ArrayList<>();
private final double width = 50, height = 50;
private final double x = 200, y = 200;
public void add(){
recs.add(new Rectangle(x, y, width, height));
/*Properties of your rectangle (fill,shape...)*/
}
public void delete(int deleteIndex){
/*Here the root is the container of your Rectangles*/
root.getChildren().remove(recs.get(deleteIndex));
recs.remove(deleteIndex);
}
public void rotate(int recIndex, double angle){
recs.get(recIndex).setRotate(recs.get(recIndex).getRotate()+angle);
}
祝你好运!
我目前有一个程序可以在场景中添加矩形。这些可以 select 编辑、旋转、调整大小和删除。如果我在一个矩形上 select 并单击旋转它有效,如果我添加另一个矩形并单击旋转它仍然有效。但是,如果我 'DELETE' 其中一个矩形,则我无法旋转另一个矩形,也无法再旋转任何其他添加的矩形。
我有以下字段和两个方法:
private List<Node> selectedShapes = new ArrayList<>();
private double angle[] = {0};
@FXML
private AnchorPane container2;
旋转:
public void rotateObject(ActionEvent event) throws IOException{
angle[0] = angle[0] + 45;
selectedShapes.get(0).setRotate(angle[0]);
}
删除
public void deleteButton(ActionEvent e) {
deletebutton.setOnAction(a -> container2.getChildren().removeAll(selectedShapes));
selectedShapes.remove(0);
}
我觉得我 select 编辑或删除 select 编辑的形状不正确。有人可以指导我 select 当前项目的正确方法。因此,如果我删除其中一个矩形,另一个应该仍然可以旋转。谢谢
您想创建一个矩形列表,而不是通过基础 class Node
为什么不直接使用 Rectangle/Shape 的列表:
List<Shape> selectedShapes = new ArrayList<>();
对于你的问题,你使用了一个数组angle[]
,这个不能扩展,而且你只调用索引 0,我明白(如果我没记错的话)这个数组是为了存储每个Rectangle
的角度来解决它也在那里使用列表:
List<Double> angle = new ArrayList<>();
最后要在您的列表中找到您自己,请使用您递增(在添加时)和递减(在删除时)的整数:
int Index = 0;
编辑: 以下是您可以用来组织项目的简单方法:
private List<Rectangle> recs = new ArrayList<>();
private final double width = 50, height = 50;
private final double x = 200, y = 200;
public void add(){
recs.add(new Rectangle(x, y, width, height));
/*Properties of your rectangle (fill,shape...)*/
}
public void delete(int deleteIndex){
/*Here the root is the container of your Rectangles*/
root.getChildren().remove(recs.get(deleteIndex));
recs.remove(deleteIndex);
}
public void rotate(int recIndex, double angle){
recs.get(recIndex).setRotate(recs.get(recIndex).getRotate()+angle);
}
祝你好运!