IMAGE OF GUI - Comme vous le voyez dans l'image que j'ai cliqué sur trois citys (SION, Sarnen et Bellinzone), mais je n'ai pas 3 lignes comme prévu (il devrait être un triangle). J'ai une exception nommée java.lang.IllegalArgumentException. Dans la boucle for, je crée des doublons (enfants) et ensuite j'ai pensé à une solution nommée boolean-checking mais ça ne marche pas (juste 1 ligne tracée).Problèmes avec les lignes JavaFX - seulement 1 ligne a été tracée
Voici le code du contrôleur - Regardez jusqu'à fonctionner 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: J'ai essayé de faire deux boutons .. un pour stocker les boutons radio sélectionnés et l'ajouter au tableau liste "ausgewaehlt" et un autre pour créer les lignes mais cela n'a pas fonctionné
Pourquoi vous appelez 'addCities();' deux temps? le premier dans la méthode 'initialize' et le second dans le' handleButtonAction'? Avec ce concept, il y aura des valeurs dupliquées à vérifier puisque l'ArrayList accepte des valeurs similaires. –
Pouvez-vous nous montrer votre fichier FXML et quelle est la classe 'Stadt'? Je suppose que c'est dessiner les «cercles»? Encore une dernière chose, pour le 'circle' vous utilisez la méthode' CenterX' et 'CenterY' pour que le' line' soit dessiné au milieu! –