JAVAFX线路出现故障 - 仅绘制了1条线路

时间:2016-12-21 10:45:54

标签: java eclipse javafx boolean illegalargumentexception

IMAGE OF GUI - 正如你在图片中看到的,我点击了三个城市(sion,sarnen和bellinzona),但我没有按预期获得3条线(它应该是一个三角形)。我有一个名为java.lang.IllegalArgumentException的异常。在for循环中,我正在创建重复项(子项),然后我想到了一个名为boolean-checking的解决方案但是它不起作用(只绘制了1行)。

这是控制器的代码 - 查找函数handleButtonAction()

package tsprealone;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.RadioButton;
import javafx.scene.shape.Line;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;

/**
 *
 * @author kbwschuler
 */
public class FXMLDocumentController implements Initializable {

    @FXML
    private AnchorPane anchorPane;
    @FXML
    private ImageView img;

    @FXML
    private ArrayList<Stadt> stadte = new ArrayList<Stadt>();
    @FXML
    private ArrayList<Line> lines = new ArrayList<Line>();
    @FXML
    private Button test;
    @FXML
    private RadioButton zug;
    @FXML
    private RadioButton zurich;
    @FXML
    private RadioButton schaffhausen;
    @FXML
    private RadioButton stgallen;
    @FXML
    private RadioButton aarau;
    @FXML
    private RadioButton chur;
    @FXML
    private RadioButton glarus;
    @FXML
    private RadioButton frauenfeld;
    @FXML
    private RadioButton schwyz;
    @FXML
    private RadioButton luzern;
    @FXML
    private RadioButton sarnen;
    @FXML
    private RadioButton bellinzona;
    @FXML
    private RadioButton sion;
    @FXML
    private RadioButton liestal;
    @FXML
    private RadioButton bern;
    @FXML
    private RadioButton delemont;
    @FXML
    private RadioButton solothurn;
    @FXML
    private RadioButton fribourg;
    @FXML
    private RadioButton neuchatel;
    @FXML
    private RadioButton lausanne;
    @FXML
    private RadioButton genf;
    @FXML
    private RadioButton altdorf;
    @FXML
    private RadioButton stans;
    @FXML
    private RadioButton basel;
    @FXML
    private RadioButton appenzell;
    @FXML
    private RadioButton herisau;
    private ArrayList<Stadt> ausgewaehlt = new ArrayList<Stadt>(); //check if is checked

    @FXML
    private void handleButtonAction(ActionEvent event) {
        addCities();
        for (Stadt stadt : stadte) {
            if (stadt.isIsClicked()) {
                ausgewaehlt.add(stadt);
            }
        }
        boolean added = false;
        starter();
        for (Stadt stadt : ausgewaehlt) {
            System.out.println(stadt.getName());
            for(Line line: lines)
            {

                if(!added)
                {
                    anchorPane.getChildren().addAll(line);
                    added = true;
                    System.out.println(line);

                }

            }





        }
    }

    public void addCities(){

        stadte.add(new Stadt("Zug", zug.getLayoutX(), zug.getLayoutY(), check(zug)));
        stadte.add(new Stadt("Zurich", zurich.getLayoutX(), zurich.getLayoutY(), check(zurich)));
        stadte.add(new Stadt("Schaffhausen", schaffhausen.getLayoutX(), schaffhausen.getLayoutY(), check(schaffhausen)));
        stadte.add(new Stadt("Aarau", aarau.getLayoutX(), aarau.getLayoutY(), check(aarau)));
        stadte.add(new Stadt("Chur", chur.getLayoutX(), chur.getLayoutY(), check(chur)));
        stadte.add(new Stadt("Glarus", glarus.getLayoutX(), glarus.getLayoutY(), check(glarus)));
        stadte.add(new Stadt("Frauenfeld", frauenfeld.getLayoutX(), frauenfeld.getLayoutY(), check(frauenfeld)));
        stadte.add(new Stadt("Schwyz", schwyz.getLayoutX(), schwyz.getLayoutY(), check(schwyz)));
        stadte.add(new Stadt("Luzern", luzern.getLayoutX(), luzern.getLayoutY(), check(luzern)));
        stadte.add(new Stadt("Sarnen", sarnen.getLayoutX(), sarnen.getLayoutY(), check(sarnen)));
        stadte.add(new Stadt("Bellinzona", bellinzona.getLayoutX(), bellinzona.getLayoutY(), check(bellinzona)));
        stadte.add(new Stadt("Sion", sion.getLayoutX(), sion.getLayoutY(), check(sion)));
        stadte.add(new Stadt("Liestal", liestal.getLayoutX(), liestal.getLayoutY(), check(liestal)));
        stadte.add(new Stadt("Bern", bern.getLayoutX(), bern.getLayoutY(), check(bern)));
        stadte.add(new Stadt("Delemont", delemont.getLayoutX(), delemont.getLayoutY(), check(delemont)));
        stadte.add(new Stadt("Solothurn", solothurn.getLayoutX(), solothurn.getLayoutY(), check(solothurn)));
        stadte.add(new Stadt("Fribourg", fribourg.getLayoutX(), fribourg.getLayoutY(), check(fribourg)));
        stadte.add(new Stadt("Neuchatel", neuchatel.getLayoutX(), neuchatel.getLayoutY(), check(neuchatel)));
        stadte.add(new Stadt("Lausanne", lausanne.getLayoutX(), lausanne.getLayoutY(), check(lausanne)));
        stadte.add(new Stadt("Genf", genf.getLayoutX(), genf.getLayoutY(), check(genf)));
        stadte.add(new Stadt("Altdorf", altdorf.getLayoutX(), altdorf.getLayoutY(), check(altdorf)));
        stadte.add(new Stadt("Stans", stans.getLayoutX(), stans.getLayoutY(), check(stans)));
        stadte.add(new Stadt("Basel", basel.getLayoutX(), basel.getLayoutY(), check(basel)));
        stadte.add(new Stadt("Appenzell", appenzell.getLayoutX(), appenzell.getLayoutY(), check(appenzell)));
        stadte.add(new Stadt("Herisau", herisau.getLayoutX(), herisau.getLayoutY(), check(herisau)));
        stadte.add(new Stadt("St Gallen", stgallen.getLayoutX(), stgallen.getLayoutY(), check(stgallen)));

    }

    public void starter() {

        for(int i = 0; i<ausgewaehlt.size(); i++)
        {
                if(i != ausgewaehlt.size() - 1){ 

                    lines.add(new Line(ausgewaehlt.get(i).getxCoord(), ausgewaehlt.get(i).getyCoord(), ausgewaehlt.get(i+1).getxCoord(), ausgewaehlt.get(i+1).getyCoord()));
                }else {
                    lines.add(new Line(ausgewaehlt.get(i).getxCoord(), ausgewaehlt.get(i).getyCoord(), ausgewaehlt.get(0).getxCoord(), ausgewaehlt.get(0).getyCoord()));
                }

        }



    }

    public boolean check(RadioButton rb) {
        if (rb.isSelected()) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        addCities();
        starter();
    }

}

PS:我试图制作两个按钮..一个用于存储所选择的无线电按钮并将其添加到阵列列表&#34; ausgewaehlt&#34;另一个用于创建线但是没有用的

1 个答案:

答案 0 :(得分:0)

首先,添加后,您不会删除任何Line。这可能会在以后导致问题。

此外,您出于某种原因一遍又一遍地添加城市。这样做一次应该足够了(或者至少清楚明确清单)。

private final Set<Line> connections = new HashSet<>();

private void connect(Stadt city1, Stadt city2) {
    Line line = new Line(city1.getxCoord(), city1.getyCoord(), city2.getxCoord(), city2.getyCoord());
    anchorPane.getChildren().addAll(line);
    connections.add(line);
}

@FXML
private void handleButtonAction(ActionEvent event) {
    // remove old connections
    anchorPane.getChildren().removeAll(connections);
    connections.clear();

    findSelectedCities();

    if (stadte.size() >= 2) {
        // connect selected cities
        Iterator<Stadt> iter = stadte.iterator();
        Stadt city = iter.next();
        while (iter.hasNext()) {
            Stadt nextCity = iter.next();
            connect(city, nextCity);
            city = nextCity;
        }

        if (stadte.size() >= 3) {
            // connect to start, if there are more than 2 selected cities 
            connect(city, stadte.get(0));
        }
    }
}

private void addSelectedCity(String name, RadioButton radio) {
    if (radio.isSelected()) {
        stadte.add(new Stadt(name, radio.getLayoutX(), radio.getLayoutY(), true));
    }
}

private void findSelectedCities() {
    stadte.clear();
    addSelectedCity("Zug", zug);
    addSelectedCity("Zurich", zurich);
    ...
}

请注意,这不会保留选择顺序。为此,您应该为onAction的{​​{1}}事件添加一个侦听器,并在RadioButton列表中添加/删除它们。

stadte