2016-04-01 22 views
2

Amacım, tıklattığım yerde daire oluşturan bir program oluşturmak ve merkezden en uzak dairelerin konumlarına göre bir sınırlayıcı kutu oluşturmak.Dikdörtgeni gruptan kaldırma sorunları javafx

Sadece iyi çalışıyor, yalnızca bir sorunum var ve bu, tüm grubu temizlemeden eski sınırlayıcı kutunun nasıl kaldırılacağını anlayamıyorum. Sorunun

Resim:

//The main class 
public class createCircles extends Application 
{ 
    circleCreator circle1 = new circleCreator(50,50); 
    Group root = new Group(circle1); 
    static double xMin = 300; 
    static double xMax = 300; 
    static double yMin = 300; 
    static double yMax = 300; 
    static int flip = 0; 

    public void start(Stage primaryStage) 
    { 
     Scene scene = new Scene(root, 600, 600); 
     createCircleAtMouseHandler(scene); 

     primaryStage.setTitle("Assignment 5"); // Set the stage title 
     primaryStage.setScene(scene); // Place the scene in the stage 
     primaryStage.show(); // Display the stage 
    } 

    void createCircleAtMouseHandler (Scene scene) 
    { 
     scene.setOnMousePressed(new EventHandler<MouseEvent>() { 
      @Override public void handle(MouseEvent event) { 
       if (!event.isControlDown()) 
       { 
        //Create the circle 
        double mouseX = event.getX(); 
        double mouseY = event.getY(); 
        System.out.println(mouseX + " " + mouseY); 

        circleCreator circle = new circleCreator(mouseX,mouseY); 
        root.getChildren().add(circle); 

        //Find bounding box values 
        if(yMin >= mouseY) 
        { 
         yMin = mouseY; 
        } 
        if(yMax <= mouseY) 
        { 
         yMax = mouseY; 
        } 
        if (xMax <= mouseX) 
        { 
         xMax = mouseX; 
        } 
        if(xMin >= mouseX) 
        { 
         xMin = mouseX; 
        } 
        System.out.println("ymin: " + yMin + "\nyMax: " + yMax + "\nxMax: " + xMax + "\nxMin: " + xMin); 

        int xTopLeft = (int)xMin; 
        int yTopLeft = (int)yMin; 
        int xBottomRight = (int)xMax; 
        int yBottomRight = (int)yMax; 

        //create/update the bounding box 
        BoundingBoxCreator boundingBox = new BoundingBoxCreator(xTopLeft, yTopLeft, xBottomRight, yBottomRight); 
        root.getChildren().remove(boundingBox); //Doesn't do anything 
        root.getChildren().add(boundingBox); 
       } 

      } 
     }); 
    } 
} 
//Creates the bounding box 
public class BoundingBoxCreator extends Pane 
{ 
    public BoundingBoxCreator(int xTopLeft, int yTopLeft, int xBottomRight, int yBottomRight) 
    { 

     Rectangle r = new Rectangle(xTopLeft, yTopLeft, 50, 50); 
     r.setFill(Color.TRANSPARENT); 
     r.setWidth(xBottomRight-xTopLeft); 
     r.setHeight(yBottomRight - yTopLeft); 
     r.setStroke(Color.BLACK); 
     r.setStrokeWidth(1); 
     getChildren().add(r); 
     System.out.println("Width: " + (xBottomRight - xTopLeft) + " Height: " + (yBottomRight - yTopLeft)); 



    } 
} 

    //Creates the circles 
public class circleCreator extends Pane 
{ 
    public circleCreator(double xPos, double yPos) 
    { 
     Circle circle = new Circle(xPos,yPos,15); 
     circle.setFill(Color.SKYBLUE); 
     getChildren().add(circle); 
     circle.setStroke(Color.DARKBLUE); 
    } 
} 

cevap

0

ben bunu anladım: enter image description here

İşte şu anda benim kodu (spagetti kodu dilerim) bulunuyor. Bir pencere oluşturdum ve ana kök sınıfının bir çocuğunu yaptım ve ana kök yerine bölmenin içindeki sınırlayıcı kutuyu koyup pane.clear(); sınırlayıcı kutuları silmek için.