Drawing rounded rectangle while dragging fails


Question

I am trying to draw a simple rectangle with rounded corners in javafx while dragging the mouse. First issue: I can't make the upper-left corner rounded. Second issue: I want my application to be able to draw nice and full rounded rectangles not like the second one from my picture. Please, how can I fix these? Thanks in advance for your help.

Here is my code:

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
      launch(args);
  }

  public void start(Stage stage){
      stage.setTitle("Test");        
      root = new BorderPane();     
      Scene scene = new Scene(root, 400, 400);
      paintings = new Group();
      stage.setScene(scene);

      canvas = new Rectangle(0, 0, stage.getScene().getWidth(), stage.getScene().getHeight());
      canvas.setFill(Color.WHITE);

      addHandlers();

      root.setCenter(canvas);  
      root.getChildren().add(paintings);
      stage.show();
   }

  private void paintRectangle(){
      roundedRect = new Rectangle(pressedX, 
                    pressedY, 
                    draggedX - pressedX, 
                    draggedY - pressedY);
      roundedRect.setFill(Color.RED);
      roundedRect.setArcHeight(40);
      roundedRect.setArcWidth(40);        
      paintings.getChildren().add(roundedRect);
  }

  private void addHandlers(){
     canvas.setOnMousePressed(new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {                
              pressedX = me.getX();
              pressedY = me.getY();
          }
      });

     canvas.setOnMouseReleased(new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {                
              /* If I make the drawing in here, I get round corners, but it doesn't get drawn while dragging*/
              //paintRectangle();
          }
      });

      canvas.setOnMouseDragged(new EventHandler<MouseEvent>() {

          @Override
          public void handle(MouseEvent me) {                
              draggedX =  me.getX();
              draggedY =  me.getY();
              paintRectangle();

          }
      });
  }
  double pressedX, pressedY;
  double draggedX, draggedY;
  Rectangle canvas;
  Group paintings;
  Rectangle roundedRect;
  BorderPane root;

}

And here is my picture: not the rounded rectangles that I expected

1
0
5/7/2012 9:19:34 AM

I could not examine your code but here is an alternative approach. You may merge this with your code:

import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Daer extends Application {

    BorderPane pane;
    Rectangle rect;
    SimpleDoubleProperty rectinitX = new SimpleDoubleProperty();
    SimpleDoubleProperty rectinitY = new SimpleDoubleProperty();
    SimpleDoubleProperty rectX = new SimpleDoubleProperty();
    SimpleDoubleProperty rectY = new SimpleDoubleProperty();

    @Override
    public void start(Stage stage) {

        pane = new BorderPane();
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);

        scene.setOnMouseDragged(mouseHandler);
        scene.setOnMousePressed(mouseHandler);
        scene.setOnMouseReleased(mouseHandler);

        rect = getNewRectangle();
        rect.widthProperty().bind(rectX.subtract(rectinitX));
        rect.heightProperty().bind(rectY.subtract(rectinitY));
        pane.getChildren().add(rect);
        stage.show();
    }

    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent mouseEvent) {

            if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
                rect.setX(mouseEvent.getX());
                rect.setY(mouseEvent.getY());
                rectinitX.set(mouseEvent.getX());
                rectinitY.set(mouseEvent.getY());
            } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
                rectX.set(mouseEvent.getX());
                rectY.set(mouseEvent.getY());
            } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED) {
                // Clone the rectangle
                Rectangle r = getNewRectangle();
                r.setX(rect.getX());
                r.setY(rect.getY());
                r.setWidth(rect.getWidth());
                r.setHeight(rect.getHeight());
                pane.getChildren().add(r);

                // Hide the rectangle
                rectX.set(0);
                rectY.set(0);
            }
        }
    };

    private Rectangle getNewRectangle() {
        Rectangle r = new Rectangle();
        r.setFill(Color.web("blue", 0.1));
        r.setStroke(Color.BLUE);
        r.setArcHeight(40);
        r.setArcWidth(40);
        return r;
    }

    public static void main(String[] args) {
        launch(args);
    }
}
3
5/7/2012 9:46:28 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon