import distributions EvenDistribution import javafx application Platfo

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import distributions.EvenDistribution;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ResourceBundle;
/**
* Created by Kudryavtsev_A on 10-Apr-14.
*/
public class GraphControl implements Initializable
{
private static double start = 0.0;
private static double end = 1.0;
private static double step = 0.001;
private static int count = 100;
private Runnable runnable = new Runnable()
{
@Override
public void run()
{
int counter;
for (double i = start; i < end; i += step)
{
MyRandom random = new MyRandom(new EvenDistribution(i));
counter = 0;
for (int j = 0; j < count; j++)
{
if (new Percolation(random.getBitMatrix(size), size, mode).isPercolated())
counter++;
}
addPoint(i, (double) counter / count);
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
exportButton.setDisable(false);
}
};
private static int size = 300;
private static Mode mode = Mode.ONE;
public LineChart<Double, Double> lineChart;
public NumberAxis xAxis;
public NumberAxis yAxis;
public Button exportButton = new Button();
public static void setStart(double start)
{
if (start < 0.0 || start > 1.0)
throw new IllegalArgumentException("start must me between 0.0 and 1.0");
GraphControl.start = start;
}
public static void setEnd(double end)
{
if (end < 0.0 || end > 1.0)
throw new IllegalArgumentException("end must me between 0.0 and 1.0");
GraphControl.end = end;
}
public static void setStep(double step)
{
if (step < 0.0 || step > 1.0)
throw new IllegalArgumentException("step must me between 0.0 and 1.0");
GraphControl.step = step;
}
public static void setSize(int size)
{
if (size < 1)
throw new IllegalArgumentException("size must me greater than 0");
GraphControl.size = size;
}
public static void setMode(Mode mode)
{
GraphControl.mode = mode;
}
public static void setCount(int count)
{
if (count < 1)
throw new IllegalArgumentException("count must me greater than 0");
GraphControl.count = count;
}
public void makeGraph()
{
clearGraph();
exportButton.setDisable(true);
Thread newThread = new Thread(runnable);
newThread.setDaemon(true);
newThread.start();
}
private void clearGraph()
{
lineChart.getData().get(0).getData().clear();
}
private void addPoint(final double x, final double y)
{
Platform.runLater(new Runnable()
{
@Override
public void run()
{
XYChart.Data<Double, Double> e = new XYChart.Data<Double, Double>(x, y);
lineChart.getData().get(0).getData().add(e);
}
});
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
XYChart.Series<Double, Double> series = new XYChart.Series<Double, Double>();
ObservableList<XYChart.Series<Double, Double>> lineChartData = FXCollections.observableArrayList();
xAxis.setForceZeroInRange(false);
xAxis.setLowerBound(start);
xAxis.setUpperBound(end);
xAxis.setTickUnit(step);
lineChartData.add(series);
lineChart.setData(lineChartData);
lineChart.setCreateSymbols(false);
GraphSettingsController.setGraphControl(this);
}
public void export(ActionEvent actionEvent)
{
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose File To Save");
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("File CSV (.csv)", "csv"));
fileChooser.setInitialFileName("export.csv");
File file = fileChooser.showSaveDialog(exportButton.getScene().getWindow());
if (file != null)
{
try
{
//PrintWriter обеспечит возможности записи в файл
PrintWriter out = new PrintWriter(file.getAbsoluteFile());
try
{
out.println("X;Y;");
for (XYChart.Data<Double, Double> data : lineChart.getData().get(0).getData())
{
out.println(String.format("%,f;%,f;", data.getXValue(), data.getYValue()));
}
} finally
{
out.close();
}
} catch (IOException e)
{
throw new RuntimeException(e);
}
}
}
}