Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Problem when resize graph JavaFX 2

    Hi all,

    I have a code to draw an XYChart and draw freehand with left mouse click

    Java FX Code:
    public class JavaFXApplication2 extends Application {
    
        Path path;//Add path for freehand
        
        @Override
        public void start(Stage stage) {
           stage.setTitle("Lines plot");
           
           final CategoryAxis xAxis = new CategoryAxis();
           final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005);
           
           yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis){
                @Override
            public String toString(Number object){
                return String.format("%7.5f", object);
            }
        });
    final LineChart<String, Number>lineChart = new LineChart<String, Number>(xAxis, yAxis);
           
           //lineChart.setTitle("Stock quotes");
           lineChart.setCreateSymbols(false);
           lineChart.setAlternativeRowFillVisible(false);
                  
           XYChart.Series series1 = new XYChart.Series();
            //series1.setName("Stock 1");
            series1.getData().add(new XYChart.Data("Jan", 0.53185));
            series1.getData().add(new XYChart.Data("Feb", 0.532235));
            series1.getData().add(new XYChart.Data("Mar", 0.53234));
            series1.getData().add(new XYChart.Data("Apr", 0.538765));
            series1.getData().add(new XYChart.Data("May", 0.53442));
            series1.getData().add(new XYChart.Data("Jun", 0.534658));
            series1.getData().add(new XYChart.Data("Jul", 0.53023));
            series1.getData().add(new XYChart.Data("Aug", 0.53001));
            series1.getData().add(new XYChart.Data("Sep", 0.53589));
            series1.getData().add(new XYChart.Data("Oct", 0.53476));
            series1.getData().add(new XYChart.Data("Nov", 0.530123));
            series1.getData().add(new XYChart.Data("Dec", 0.53035));
    
    BorderPane pane = new BorderPane();
            pane.setCenter(lineChart);  
            //Scene scene = new Scene(lineChart, 800, 600);
            Scene scene = new Scene(pane, 800, 600);
            lineChart.getData().addAll(series1/*, series2*/);
            
            stage.setScene(scene);
            //scene.getStylesheets().add(this.getClass().getResource("linechart.css").toExternalForm());
            
            path = new Path();
            path.setStrokeWidth(1);
            path.setStroke(Color.BLACK);
          
            scene.setOnMouseClicked(mouseHandler);
            scene.setOnMouseDragged(mouseHandler);
            scene.setOnMouseEntered(mouseHandler);
            scene.setOnMouseExited(mouseHandler);
            scene.setOnMouseMoved(mouseHandler);
            scene.setOnMousePressed(mouseHandler);
            scene.setOnMouseReleased(mouseHandler);
          
            //root.getChildren().add(lineChart);
            pane.getChildren().add(path);
                    
            stage.show();
        }
            
             EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>(){
             
                @Override
              public void handle(MouseEvent mouseEvent){
                
                  if(mouseEvent.getEventType()==MouseEvent.MOUSE_PRESSED){                  
                      //path.getElements().clear();
                      path.getElements().add(new MoveTo(mouseEvent.getX(), mouseEvent.getY()));
                  }
                  else if(mouseEvent.getEventType()==MouseEvent.MOUSE_DRAGGED){
                      path.getElements().add(new LineTo(mouseEvent.getX(), mouseEvent.getY()));
                  }
              }
          };
               
        
        public static void main(String[] args) {
            launch(args);
    Problem arise when I do resize the frame, chart resize according to frame while freehand draws do not resize, as the attached pictures.

    How do I have to modify my code to solve this problem?

    Thanks!

    Problem when resize graph JavaFX 2-before.jpgProblem when resize graph JavaFX 2-after.jpg
    Last edited by susieferrari; 04-25-2012 at 06:52 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    One way, probably not the best, and still not perfect. You'll need to discover what's causing the small discrepancies and fine-tune it. One factor is definitely the fixed gap/margin at the top of the chart, which needs to be used for a yOffset similar to the xOffset, but I couldn't find how to get a hold of it.
    Java FX Code:
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.ObservableList;
    import javafx.event.EventHandler;
    import javafx.geometry.Bounds;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.chart.LineChart;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
    import javafx.scene.shape.Path;
    import javafx.scene.transform.Scale;
    import javafx.scene.transform.Transform;
    import javafx.scene.transform.Translate;
    
    public class ChartAxes extends Application {
    
      private CategoryAxis xAxis = new CategoryAxis();
      private NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0001);
      private LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
      private Path path;
      private double initialWidth;
      private double iniitialheight;
      private Translate translate = new Translate(0, 0);
      private Scale scale = new Scale(1, 1);
      private ChangeListener<Number> changeListener = new ChangeListener<Number>() {
    
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
          Bounds axisBounds = yAxis.getBoundsInLocal();
          double xOffset = axisBounds.getMaxX();
          translate.setX(xOffset);
    
          Bounds chartBounds = lineChart.getBoundsInLocal();
          scale.setX((chartBounds.getWidth() - xOffset) / initialWidth);
          scale.setY((chartBounds.getHeight() - xAxis.getBoundsInLocal().getHeight()) / iniitialheight);
        }
      };
      private EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
    
        @Override
        public void handle(MouseEvent mouseEvent) {
    
          double targetX = (mouseEvent.getX() - translate.getX()) / scale.getX();
          double targetY = mouseEvent.getY() / scale.getY();
    
          if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
            path.getElements().add(new MoveTo(targetX, targetY));
          } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            path.getElements().add(new LineTo(targetX, targetY));
          }
        }
      };
    
      @Override
      public void start(Stage stage) {
        stage.setTitle("Lines plot");
    
        yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
    
          @Override
          public String toString(Number object) {
            return String.format("%6.4f", object);
          }
        });
    
        lineChart.setCreateSymbols(false);
        lineChart.setAlternativeRowFillVisible(false);
    
        XYChart.Series series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data("Jan", 0.53185));
        series1.getData().add(new XYChart.Data("Feb", 0.532235));
        series1.getData().add(new XYChart.Data("Mar", 0.53234));
        series1.getData().add(new XYChart.Data("Apr", 0.538765));
        series1.getData().add(new XYChart.Data("May", 0.53442));
        series1.getData().add(new XYChart.Data("Jun", 0.534658));
        series1.getData().add(new XYChart.Data("Jul", 0.53023));
        series1.getData().add(new XYChart.Data("Aug", 0.53001));
        series1.getData().add(new XYChart.Data("Sep", 0.53589));
        series1.getData().add(new XYChart.Data("Oct", 0.53476));
        series1.getData().add(new XYChart.Data("Nov", 0.530123));
        series1.getData().add(new XYChart.Data("Dec", 0.53035));
        lineChart.getData().addAll(series1);
    
        BorderPane pane = new BorderPane();
        pane.setCenter(lineChart);
    
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);
    
        path = new Path();
        path.setStrokeWidth(1);
        path.setStroke(Color.BLACK);
    
        ObservableList<Transform> transforms = path.getTransforms();
        transforms.add(0, translate);
        transforms.add(1, scale);
    
        scene.setOnMouseClicked(mouseHandler);
        scene.setOnMouseDragged(mouseHandler);
        scene.setOnMouseEntered(mouseHandler);
        scene.setOnMouseExited(mouseHandler);
        scene.setOnMouseMoved(mouseHandler);
        scene.setOnMousePressed(mouseHandler);
        scene.setOnMouseReleased(mouseHandler);
    
        pane.getChildren().add(path);
    
        scene.widthProperty().addListener(changeListener);
        scene.heightProperty().addListener(changeListener);
    
        stage.show();
        Bounds axisBounds = yAxis.getBoundsInLocal();
        double xOffset = axisBounds.getMaxX();
        translate.setX(xOffset);
    
        Bounds chartBounds = lineChart.getBoundsInLocal();
        initialWidth = chartBounds.getWidth() - xOffset;
        iniitialheight = chartBounds.getHeight() - xAxis.getBoundsInLocal().getHeight();
      }
    
      public static void main(String[] args) {
        launch(args);
      }
    }
    You owe me a beer. Heck, you owe me a six-pack!

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    Thanks DarryBurke, no problem for the beer I owe you, the problem is that I still have same resize problem even after your code.

    Susie

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    Quote Originally Posted by susieferrari View Post
    I still have same resize problem even after your code.
    I tested that code. You can't possibly have the same problem. Run the code I posted and post screenshots here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    Quote Originally Posted by DarrylBurke View Post
    I tested that code. You can't possibly have the same problem. Run the code I posted and post screenshots here.

    db
    Here are two pictures after using your code

    Susie

    Problem when resize graph JavaFX 2-a01698.jpg

    Problem when resize graph JavaFX 2-a01699.jpg

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    Here's what I get, sizing the chart as close as I could get to the sizes in your screenshots.

    I coped the code back from the forum on the off chance that I'd made some change that broke it just before copying it here. Nope, it works fine.

    db

    Problem when resize graph JavaFX 2-before.png
    Problem when resize graph JavaFX 2-after.png
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    You must have made some change in the code.

    By the way, I think I uncovered a JavaFX bug when capturing the screenshot -- a couple of NullPointerExceptions from deep inside the FX classes when I pressed Alt-PrintScreen.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    What I have done was just copy and paste your code, the only changes made was

    Java FX Code:
    stage.setTitle("Darryl's code line plot");
    and

    Java FX Code:
    path.setStroke(Color.CHOCOLATE);
    nothing else.

    Below is the full code

    Java FX Code:
    package javafxapplication2;
    
    import javafx.application.Application;
    import javafx.beans.value.ChangeListener;
    import javafx.beans.value.ObservableValue;
    import javafx.collections.ObservableList;
    import javafx.event.EventHandler;
    import javafx.geometry.Bounds;
    import javafx.scene.chart.CategoryAxis;
    import javafx.scene.chart.NumberAxis;
    import javafx.scene.chart.XYChart;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.chart.LineChart;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.BorderPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
    import javafx.scene.shape.Path;
    import javafx.scene.transform.Scale;
    import javafx.scene.transform.Transform;
    import javafx.scene.transform.Translate;
    
    public class ChartAxes extends Application {
    
        private CategoryAxis xAxis = new CategoryAxis();
      private NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0001);
      private LineChart<String, Number> lineChart = new LineChart<String, Number>(xAxis, yAxis);
      private Path path;
      private double initialWidth;
      private double iniitialheight;
      private Translate translate = new Translate(0, 0);
      private Scale scale = new Scale(1, 1);
      private ChangeListener<Number> changeListener = new ChangeListener<Number>() {
     
            @Override
        public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
          Bounds axisBounds = yAxis.getBoundsInLocal();
          double xOffset = axisBounds.getMaxX();
          translate.setX(xOffset);
     
          Bounds chartBounds = lineChart.getBoundsInLocal();
          scale.setX((chartBounds.getWidth() - xOffset) / initialWidth);
          scale.setY((chartBounds.getHeight() - xAxis.getBoundsInLocal().getHeight()) / iniitialheight);
        }
      };
      private EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() {
     
        @Override
        public void handle(MouseEvent mouseEvent) {
     
          double targetX = (mouseEvent.getX() - translate.getX()) / scale.getX();
          double targetY = mouseEvent.getY() / scale.getY();
     
          if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) {
            path.getElements().add(new MoveTo(targetX, targetY));
          } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            path.getElements().add(new LineTo(targetX, targetY));
          }
        }
      };
        
        @Override
        public void start(Stage stage) {
           stage.setTitle("Darryl's code line plot");
           
           yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) {
     
          @Override
          public String toString(Number object) {
            return String.format("%6.4f", object);
          }
        });
     
        lineChart.setCreateSymbols(false);
        lineChart.setAlternativeRowFillVisible(false);
     
        XYChart.Series series1 = new XYChart.Series();
        series1.getData().add(new XYChart.Data("Jan", 0.53185));
        series1.getData().add(new XYChart.Data("Feb", 0.532235));
        series1.getData().add(new XYChart.Data("Mar", 0.53234));
        series1.getData().add(new XYChart.Data("Apr", 0.538765));
        series1.getData().add(new XYChart.Data("May", 0.53442));
        series1.getData().add(new XYChart.Data("Jun", 0.534658));
        series1.getData().add(new XYChart.Data("Jul", 0.53023));
        series1.getData().add(new XYChart.Data("Aug", 0.53001));
        series1.getData().add(new XYChart.Data("Sep", 0.53589));
        series1.getData().add(new XYChart.Data("Oct", 0.53476));
        series1.getData().add(new XYChart.Data("Nov", 0.530123));
        series1.getData().add(new XYChart.Data("Dec", 0.53035));
        lineChart.getData().addAll(series1);
     
        BorderPane pane = new BorderPane();
        pane.setCenter(lineChart);
     
        Scene scene = new Scene(pane, 800, 600);
        stage.setScene(scene);
     
        path = new Path();
        path.setStrokeWidth(2);
        path.setStroke(Color.CHOCOLATE);
     
        ObservableList<Transform> transforms = path.getTransforms();
        transforms.add(0, translate);
        transforms.add(1, scale);
     
        scene.setOnMouseClicked(mouseHandler);
        scene.setOnMouseDragged(mouseHandler);
        scene.setOnMouseEntered(mouseHandler);
        scene.setOnMouseExited(mouseHandler);
        scene.setOnMouseMoved(mouseHandler);
        scene.setOnMousePressed(mouseHandler);
        scene.setOnMouseReleased(mouseHandler);
     
        pane.getChildren().add(path);
     
        scene.widthProperty().addListener(changeListener);
        scene.heightProperty().addListener(changeListener);
     
        stage.show();
        Bounds axisBounds = yAxis.getBoundsInLocal();
        double xOffset = axisBounds.getMaxX();
        translate.setX(xOffset);
     
        Bounds chartBounds = lineChart.getBoundsInLocal();
        initialWidth = chartBounds.getWidth() - xOffset;
        iniitialheight = chartBounds.getHeight() - xAxis.getBoundsInLocal().getHeight();
      }
        
        public static void main(String[] args) {
            launch(args);
    //        System.out.println("Java Version             : "+com.sun.javafx.runtime.VersionInfo.getVersion());
    //        System.out.println("Java getHudsonBuildNumber: "+com.sun.javafx.runtime.VersionInfo.getHudsonBuildNumber());
    //        System.out.println("Java getReleaseMilestone : "+com.sun.javafx.runtime.VersionInfo.getReleaseMilestone());
    //        System.out.println("Java getRuntimeVersion   : "+com.sun.javafx.runtime.VersionInfo.getRuntimeVersion());
        }
      }
    I am missing something?

    Thanks

    susie

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    I copied the code you just posted and I get the desired result. I could post more screenshots, but they would look like those I posted already, except for the stage title and path color.

    This is what I get from uncommenting your sysout lines:
    Java FX Code:
    Java Version             : 2.0.0-beta
    Java getHudsonBuildNumber: 250
    Java getReleaseMilestone : beta
    Java getRuntimeVersion   : 2.0.0-beta-b42
    Do you have a different version?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    I am using this version:

    Java FX Code:
    Java Version             : 2.2.0-beta
    Java getHudsonBuildNumber: 28
    Java getReleaseMilestone : beta
    Java getRuntimeVersion   : 2.2.0-beta-b03
    Maybe the problems are because I am using a different version. In your opinion, should I have to downgrade to your 2.0 version?

    Thanks

    Susie

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    You could look around the FX bug/RFE database and see if there's anything interesting to be found there, or file a new bug report if there isn't.
    https://forums.oracle.com/forums/ann.jspa?annID=1713

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    Thanks Darryl for your support.

    Regards,

    Susie

  13. #13
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default Re: Problem when resize graph JavaFX 2

    Well in the meantime I've removed any previous FX and re-installed starting directly from FX 2.2

    Now freehand draw do resize, not perfectly as you said but really better than previous when it doesn't resize at all.

    Wonder how to improve what's left.

    Thanks

    Susie

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Problem when resize graph JavaFX 2

    You need to look closely at the boundsInLocal and/or boundsInParent of anything you can get your mitts on. One definite contributor to discrepancy is the gap/margin I mentioned n #2. There's also the matter of the yAxis moving to a negative x-location if you resize the Stage to be very narrow across, when the algorithm I used fails miserably.

    You also may need to use the Scale's pivotX / pivotY.

    I keep feeling there must be a better way to do this with Bindings, but couldn't identify which properties to bind to. And old Swing habits of using Listeners don't easily go away.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 16
    Last Post: 03-21-2012, 09:41 AM
  2. Having problem with Ajacency Graph
    By kursist in forum Advanced Java
    Replies: 7
    Last Post: 06-20-2011, 03:32 PM
  3. Graph Problem Java Directed
    By daxter in forum Advanced Java
    Replies: 4
    Last Post: 05-05-2011, 03:00 AM
  4. Problem resizing JPanel on window resize
    By Nyet in forum AWT / Swing
    Replies: 4
    Last Post: 11-27-2009, 03:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •