import distributions EvenDistribution import distributions NormalDistr

  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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import distributions.EvenDistribution;
import distributions.NormalDistribution;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.geometry.HPos;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Window;
import jfx.messagebox.MessageBox;
import java.net.URL;
import java.util.ResourceBundle;
public class DemoFormController implements Initializable
{
private final int cellSize = 20;
private final int maxSize = 30;
public TextField sizeField = new TextField();
public GridPane panel = new GridPane();
public Label statusField = new Label();
public ChoiceBox<String> nType = new ChoiceBox<String>();
public ChoiceBox<String> distrType = new ChoiceBox<String>();
public TextField param1 = new TextField();
public TextField param2 = new TextField();
private MyRandom r;
private int size = 0;
private double a = 0.5;
private double b = 10;
private boolean percolated = false;
private boolean changed = true;
private boolean parse()
{
int newSize;
try
{
newSize = Integer.parseInt(sizeField.getText());
a = Double.parseDouble(param1.getText().replace(',', '.'));
b = Double.parseDouble(param2.getText().replace(',', '.'));
} catch (NumberFormatException e)
{
return false;
}
changed = newSize != size;
size = newSize;
return true;
}
private boolean checkInput()
{
return size <= maxSize && size != 0;
}
private void showMessageBox(String title, String message, int option)
{
Window thisWindow = panel.getScene().getWindow();
MessageBox.show(thisWindow, message, title, option);
}
private void checkChanges()
{
if (changed)
{
javafx.application.Platform.runLater(new Runnable()
{
@Override
public void run()
{
redraw();
}
});
}
if (distrType.getValue().equals("Равномерное распределение"))
r = new MyRandom(new EvenDistribution(a));
else
r = new MyRandom(new NormalDistribution(a, b));
}
public void generate(ActionEvent actionEvent)
{
percolated = false;
if (!parse())
{
showMessageBox("Внимание!", "Проверьте ввод! Некорректные параметры распределения.", MessageBox.ICON_ERROR | MessageBox.OK);
return;
}
if (!checkInput())
{
showMessageBox("Внимание!", "Проверьте ввод! Некорректный размер.", MessageBox.ICON_ERROR | MessageBox.OK);
return;
}
checkChanges();
fill();
if (percolated)
{
statusField.setText("Перколирует :)");
statusField.setTextFill(Color.LIGHTGREEN);
} else
{
statusField.setText("Не перколирует :(");
statusField.setTextFill(Color.LIGHTPINK);
}
}
private void fill()
{
panel.getChildren().clear();
final byte[][] matrix = r.getBitMatrix(size);
Percolation percolation = new Percolation(matrix, size, getMode());
percolated = percolation.isPercolated();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (matrix[i][j] == 1)
{
//if(percolation.isFull(i, j))
// panel.add(new Rectangle(cellSize - 1, cellSize - 1, Color.BLUE), j, i);
//else
panel.add(new Rectangle(cellSize - 1, cellSize - 1, Color.LIGHTGREEN), j, i);
} else
{
panel.add(new Rectangle(cellSize - 1, cellSize - 1, Color.LIGHTPINK), j, i);
}
}
}
}
private Mode getMode()
{
Mode mode;
if (nType.getValue().equals("4 соседа"))
mode = Mode.ONE;
else
mode = Mode.TWO;
return mode;
}
private void redraw()
{
int i1 = size * cellSize;
panel.setPrefSize(i1, i1);
panel.setMinSize(i1, i1);
panel.getColumnConstraints().clear();
panel.getRowConstraints().clear();
for (int i = 0; i < size; i++)
{
ColumnConstraints columnConstraints = new ColumnConstraints(cellSize);
columnConstraints.setHalignment(HPos.CENTER);
panel.getColumnConstraints().add(columnConstraints);
panel.getRowConstraints().add(new RowConstraints(cellSize));
}
}
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{
distrType.getItems().add("Равномерное распределение");
distrType.getItems().add("Нормальное распределение");
distrType.setValue("Равномерное распределение");
nType.getItems().add("4 соседа");
nType.getItems().add("8 соседей");
nType.setValue("4 соседа");
sizeField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent inputevent)
{
if (!inputevent.getCharacter().matches("\\d"))
{
inputevent.consume();
}
}
});
EventHandler<KeyEvent> eventHandler = new EventHandler<KeyEvent>()
{
@Override
public void handle(KeyEvent keyEvent)
{
if (!keyEvent.getCharacter().matches("[\\d , \\.]"))
{
keyEvent.consume();
}
}
};
param1.addEventFilter(KeyEvent.KEY_TYPED, eventHandler);
param2.addEventFilter(KeyEvent.KEY_TYPED, eventHandler);
distrType.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener<Number>()
{
@Override
public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2)
{
if (number2.equals(0))
param2.setDisable(true);
else
param2.setDisable(false);
}
});
}
}