Results 1 to 14 of 14
- 04-25-2012, 06:50 PM #1
Problem when resize graph JavaFX 2
Hi all,
I have a code to draw an XYChart and draw freehand with left mouse click
Problem arise when I do resize the frame, chart resize according to frame while freehand draws do not resize, as the attached pictures.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);
How do I have to modify my code to solve this problem?
Thanks!

Last edited by susieferrari; 04-25-2012 at 06:52 PM.
- 04-26-2012, 12:07 AM #2
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.
You owe me a beer. Heck, you owe me a six-pack!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); } }
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-26-2012, 09:20 AM #3
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
- 04-26-2012, 10:16 AM #4
- 04-26-2012, 10:24 AM #5
- 04-26-2012, 10:51 AM #6
Re: Problem when resize graph JavaFX 2
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-26-2012, 10:54 AM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-26-2012, 11:06 AM #8
Re: Problem when resize graph JavaFX 2
What I have done was just copy and paste your code, the only changes made was
andJava FX Code:stage.setTitle("Darryl's code line plot");
nothing else.Java FX Code:path.setStroke(Color.CHOCOLATE);
Below is the full code
I am missing something?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()); } }
Thanks
susie
- 04-26-2012, 11:29 AM #9
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:Do you have a different version?Java FX Code:Java Version : 2.0.0-beta Java getHudsonBuildNumber: 250 Java getReleaseMilestone : beta Java getRuntimeVersion : 2.0.0-beta-b42
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-26-2012, 11:39 AM #10
Re: Problem when resize graph JavaFX 2
I am using this version:
Maybe the problems are because I am using a different version. In your opinion, should I have to downgrade to your 2.0 version?Java FX Code:Java Version : 2.2.0-beta Java getHudsonBuildNumber: 28 Java getReleaseMilestone : beta Java getRuntimeVersion : 2.2.0-beta-b03
Thanks
Susie
- 04-26-2012, 03:03 PM #11
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-26-2012, 03:10 PM #12
Re: Problem when resize graph JavaFX 2
Thanks Darryl for your support.
Regards,
Susie
- 04-26-2012, 04:41 PM #13
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
- 04-26-2012, 07:01 PM #14
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
scrollable graph on JFrame. (I've made the graph and I need it scrollable)
By noobplus in forum AWT / SwingReplies: 16Last Post: 03-21-2012, 09:41 AM -
Having problem with Ajacency Graph
By kursist in forum Advanced JavaReplies: 7Last Post: 06-20-2011, 03:32 PM -
Graph Problem Java Directed
By daxter in forum Advanced JavaReplies: 4Last Post: 05-05-2011, 03:00 AM -
Problem resizing JPanel on window resize
By Nyet in forum AWT / SwingReplies: 4Last Post: 11-27-2009, 03:13 AM


LinkBack URL
About LinkBacks
Reply With Quote





Bookmarks