Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. //Graph.java
  2.  
  3. package graphs;
  4.  
  5. import java.io.File;
  6. import javafx.application.Application;
  7. import javafx.scene.Scene;
  8. import javafx.scene.chart.LineChart;
  9. import javafx.scene.chart.NumberAxis;
  10. import javafx.scene.chart.XYChart;
  11. import javafx.stage.Stage;
  12.  
  13.  
  14. public class Graphs extends Application {
  15.  
  16. @Override public void start(Stage stage) {
  17. stage.setTitle("Graph App");
  18. int firstFileCount=0;
  19. int secondFileCount=0;
  20. int thirdFileCount=0;
  21. int firstFileChar=0;
  22. int secondFileChar=0;
  23. int thirdFileChar=0;
  24.  
  25. final NumberAxis xAxis = new NumberAxis();
  26. final NumberAxis yAxis = new NumberAxis();
  27. xAxis.setLabel("Numer lini");
  28. yAxis.setLabel("Długość lini");
  29. final LineChart<Number,Number> lineChart =
  30. new LineChart<Number,Number>(xAxis,yAxis);
  31.  
  32. lineChart.setTitle("Wykres zależności długości lini od jej numeru w pliku tekstowym");
  33.  
  34. LineCounter firstFile= new LineCounter(new File("C:\\Users\\202166\\Documents\\NetBeansProjects\\Graphs\\plik.txt"));
  35. firstFileCount=firstFile.readLines();
  36. firstFileChar=firstFile.readChars();
  37.  
  38. LineCounter secondFile= new LineCounter(new File("C:\\Users\\202166\\Documents\\NetBeansProjects\\Graphs\\plik2.txt"));
  39. secondFileCount=secondFile.readLines();
  40. secondFileChar=secondFile.readChars();
  41.  
  42. LineCounter thirdFile= new LineCounter(new File("C:\\Users\\202166\\Documents\\NetBeansProjects\\Graphs\\plik3.txt"));
  43. thirdFileCount=thirdFile.readLines();
  44. thirdFileChar=thirdFile.readLines();
  45.  
  46.  
  47. System.out.println(secondFileCount);
  48.  
  49. XYChart.Series series = new XYChart.Series();
  50. //populating the series with data
  51. series.getData().add(new XYChart.Data(firstFileChar,firstFileCount));
  52. series.getData().add(new XYChart.Data(secondFileChar, secondFileCount));
  53. series.getData().add(new XYChart.Data(thirdFileChar, thirdFileCount));
  54.  
  55.  
  56. Scene scene = new Scene(lineChart,800,600);
  57. lineChart.getData().add(series);
  58.  
  59. stage.setScene(scene);
  60. stage.show();
  61. }
  62.  
  63. public static void main(String[] args) {
  64. launch(args);
  65. }
  66. }
  67.  
  68. //LineCounter.java
  69.  
  70. package graphs;
  71. import java.io.File;
  72. import java.util.Scanner;
  73.  
  74.  
  75. public class LineCounter {
  76. private File file;
  77. private int lineCount=0;
  78. private int charCount=0;
  79.  
  80. LineCounter(File file){
  81. this.file=file;
  82. }
  83.  
  84. public int readLines(){
  85. try
  86. {
  87. Scanner myFile = new Scanner(file);
  88. while (myFile.hasNextLine())
  89. {
  90. lineCount++;
  91. myFile.nextLine();
  92. }
  93. }
  94. catch(Exception e)
  95. {
  96. System.out.println("Exception occured");
  97. }
  98. return lineCount;
  99. }
  100. public int readChars(){
  101. try{
  102. Scanner myFile= new Scanner(file);
  103. int wordsCount=0;
  104. while(myFile.hasNext()) {
  105. wordsCount++;
  106. String word = myFile.next();
  107. charCount += word.length();
  108. }
  109. } catch(Exception e){
  110. System.out.println("Exception occured");
  111. }
  112. return charCount;
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement