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

    Default Draw text and lines on a XYChart

    Hi all,

    I have a XYChart to plot data as line and a CSS file for graph settings: my code is this

    Java FX Code:
    public class JavaFXApplication2 extends Application {
    
        @Override
        public void start(Stage stage) {
           stage.setTitle("Lines plot");
           
           final CategoryAxis xAxis = new CategoryAxis();
           final NumberAxis yAxis = new NumberAxis();
                  
           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", 23));
            series1.getData().add(new XYChart.Data("Feb", 14));
            series1.getData().add(new XYChart.Data("Mar", 15));
            series1.getData().add(new XYChart.Data("Apr", 24));
            series1.getData().add(new XYChart.Data("May", 34));
            series1.getData().add(new XYChart.Data("Jun", 36));
            series1.getData().add(new XYChart.Data("Jul", 22));
            series1.getData().add(new XYChart.Data("Aug", 45));
            series1.getData().add(new XYChart.Data("Sep", 43));
            series1.getData().add(new XYChart.Data("Oct", 17));
            series1.getData().add(new XYChart.Data("Nov", 29));
            series1.getData().add(new XYChart.Data("Dec", 25));
    
    Scene scene = new Scene(lineChart, 800, 600);
            lineChart.getData().addAll(series1, series2);
            
            stage.setScene(scene);
            scene.getStylesheets().add(this.getClass().getResource("linechart.css").toExternalForm());
            stage.show();
               
        }
        public static void main(String[] args) {
            launch(args);
        }
    }
    I would like to draw text and lines on this chart by selecting a "Text" button or a "Line" button, then write a custom text by keyboard input or draw a line by mouse click and drag, anywhere on the chart.

    I have read about how to write text in this doc
    XML Code:
    http://docs.oracle.com/javafx/2.0/text/jfxpub-text.htm
    , but there are no clues about what I would like to accomplish.

    Any help greatly appreciated

    Thanks

    Susanna
    skype:susie.ferrari

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

    Default Re: Draw text and lines on a XYChart

    None reply...

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

    Default Re: Draw text and lines on a XYChart

    Found a good example to draw free hand on a scene

    Java FX Code:
    package javafxapplication3;
    
    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.paint.Color;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.scene.shape.LineTo;
    import javafx.scene.shape.MoveTo;
    import javafx.scene.shape.Path;
    import javafx.stage.Stage;
    
    
    /**
     *
     * @author Susie
     */
    public class JavaFX_MouseEvent extends Application {
    
    
       Path path;
       
        public static void main(String[] args) {
            launch(args);
        }
        
        @Override
        public void start(Stage primaryStage) {
          primaryStage.setTitle("Mouse free hand drawing");  
          Group root = new Group();
          Scene scene = new Scene(root, 600, 350);
          
          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(path);
          primaryStage.setScene(scene);
          primaryStage.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()));
                  }
              }
          };
        }
    How to modify this code to draw Text or straight lines?

    Thanks

    Susie

Similar Threads

  1. Can I have two lines of text on a JButton?
    By fatabass in forum New To Java
    Replies: 2
    Last Post: 02-21-2012, 11:17 PM
  2. Writing multiple lines to text file at once
    By Plex in forum New To Java
    Replies: 14
    Last Post: 11-08-2010, 10:17 PM
  3. Replies: 1
    Last Post: 06-30-2010, 08:12 PM
  4. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 04:54 PM
  5. Adding lines at start and end of the text file
    By phani532 in forum New To Java
    Replies: 5
    Last Post: 08-27-2008, 04:12 PM

Posting Permissions

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