Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8115723

Region hit test is wrong for pie slices

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • fx2.1
    • 7-pool
    • javafx

      When you have a pie chart, its slices are technically regions with shapes. When you click on a slice, a different slice is often picked, you can also pick slices outside of the chart, because the slices are picked "on bounds" - this is caused by RT-17024 (region picking on bounds by default). However, when you set slice's pickOnBounds to false manually, it doesn't help, because the hit test is wrong. I experimented and found out that the area outside of the slice is picked because of the code branch in Region's picking that says:

                                  // pt within fill and not within area with a rounded corner either because
                                  // there are no rounded corners or the pt doesn't fall inside one

      Here is a code to reproduce the problem:

      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.event.EventHandler;
      import javafx.scene.Group;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.chart.PieChart;
      import javafx.scene.control.Label;
      import javafx.scene.input.MouseEvent;
      import javafx.scene.paint.Color;
      import javafx.stage.Stage;

      public class PieChartExample extends Application
      {
      private void init(Stage primaryStage)
      {
      Group root = new Group();
      primaryStage.setScene(new Scene(root));
      PieChart p = createChart();
      root.getChildren().add(p);
      }

      protected PieChart createChart()
      {
      final PieChart pc = new PieChart(FXCollections.observableArrayList(
      new PieChart.Data("Apples", 40),
      new PieChart.Data("Strawberries", 11),
      new PieChart.Data("Grapes", 30),
      new PieChart.Data("Oranges", 10)
      ));
      // setup chart
      pc.setId("BasicPie");
      pc.setTitle("Fruits");

      for (final PieChart.Data data : pc.getData())
      {
      data.getNode().setPickOnBounds(false);
      data.getNode().setUserData(data.getName());
      data.getNode().addEventHandler(MouseEvent.MOUSE_PRESSED,
      new EventHandler<MouseEvent>()
      {
      @Override
      public void handle(MouseEvent e)
      {
      if(e.getSource() instanceof Node)
      {
      Node node = (Node)e.getSource();
      System.out.println("Slice: " + node.getUserData()+ " hashCode: " + node.hashCode());
      }
      }
      });
      }
      return pc;
      }

      @Override
      public void start(Stage primaryStage) throws Exception
      {
      init(primaryStage);
      primaryStage.show();
      }

      public static void main(String[] args)
      {
      launch(args);
      }
      }

            kwwong Kinsley Wong (Inactive)
            psafrata Pavel Ĺ afrata
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: