there was such a problem. I need to draw a graph on the Turing Machine (dependence of the number of steps on the length of a word) when I try to draw it from the initialize () method, then it draws correctly enter image description here

But as soon as I try to make the graph drawn when I press a button, this happens Wrong schedule

The class itself, which draws the schedule

package sample; import javafx.scene.chart.LineChart; import javafx.scene.chart.XYChart; import java.util.HashMap; public class FourGraphic extends Thread{ LineChart<Number, Number> chart; HashMap<String, HashMap<Integer, String>> rules; FourGraphic(LineChart<Number, Number> chart, HashMap<String, HashMap<Integer, String>> rules){ this.chart = chart; this.rules = rules; } public void run(){ System.out.println(Integer.toString(7, 2)); XYChart.Series<Number, Number> series = new XYChart.Series(); series.getData().add(new XYChart.Data(0, 0)); chart.getData().add(series); int n = 0; while(true){ int max = 0; for(long i = (int)Math.pow(2, n); i < Math.pow(2, n+1); i++){ FourRibbersMT mt = new FourRibbersMT(new StringBuffer(Long.toString(i, 2)), rules); mt.start(); try{ synchronized (mt) { while(!mt.ready){ mt.wait(); } } } catch(InterruptedException e){ e.printStackTrace(); } synchronized (mt) { if (mt.getStepsCount() > max) max = mt.getStepsCount(); } } series.getData().add(new XYChart.Data<Number, Number>(n + 1, max)); n++; } } 

}

Initalize () method

  @FXML public void initialize(){ readSingleRules(); readFourRibbRules(); singleRibbonField.setOnKeyTyped(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { char c = event.getCharacter().charAt(0); if(((c < '0') || (c > '1')) && c != java.awt.event.KeyEvent.VK_PERIOD && c != 45){ singleRibbonField.deleteText(singleRibbonField.getText().length() - 1, singleRibbonField.getText().length()); } } }); fourRibbonField.setOnKeyTyped(new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent event) { char c = event.getCharacter().charAt(0); if(((c < '0') || (c > '1')) && c != java.awt.event.KeyEvent.VK_PERIOD && c != 45){ fourRibbonField.deleteText(fourRibbonField.getText().length() - 1, fourRibbonField.getText().length()); } } }); singleStartBtn.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { singleGraphic.getData().clear(); // graph = new SingleGraphic(singleGraphic, singleRules); FourGraphic graph = new FourGraphic(fourGraphic, fourRibbRules); graph.start(); stopSingleBtn.setVisible(true); } }); } 

Tell me what's the problem

    1 answer 1

    Probably late, but try to create a new method. Create a thread in it:

     Platform.runLater(()-> { //код по отрисовке графика });