Reputation: 1
Here is a gif showing my problem:
In my example: When I click on the radio buttons, lines have to be generated in this order -> sion -> sarnen -> altdorf -> bellinzona
and form into a trapeze (trapezium) but unfortunately my code switches the coordinates of the second and third position. I've tried it with other cities so maybe it could be something wrong with the fxml
(which I don't think). It must be the code in class FXMLDocumentController
. I get in the console this output:
Sarnen
Line[startX=600.0, startY=347.0, endX=803.0, endY=594.0, stroke=0x000000ff, strokeWidth=1.0]
Line[startX=803.0, startY=594.0, endX=373.0, endY=585.0, stroke=0x000000ff, strokeWidth=1.0]
Line[startX=373.0, startY=585.0, endX=702.0, endY=347.0, stroke=0x000000ff, strokeWidth=1.0]
Line[startX=702.0, startY=347.0, endX=600.0, endY=347.0, stroke=0x000000ff, strokeWidth=1.0]
Bellinzona
Sion
Altdorf
This means just that first sarnen has been created then bellinzona then sion and then altdorf.
FXMLDocumentController
: I personally think the method starter()
isn't doing that what it should so have there a first look
package tsprealone;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
//import javafx.scene.control.Alert;
//import javafx.scene.control.Alert.AlertType;
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 {
// private Alert alert = new Alert(AlertType.WARNING); just a message box
@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 generiere;
@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 clearPane(ActionEvent event)
{
//anchorPane.getChildren().clear();
anchorPane.getChildren().removeAll(lines);
lines.clear();
anchorPane.getChildren().removeAll(ausgewaehlt);
ausgewaehlt.clear();
RadioButton[] allRadioButtons = new RadioButton[] {
zug, zurich, schaffhausen, stgallen, aarau, chur, glarus, frauenfeld, schwyz, luzern, sarnen, bellinzona, sion, liestal, bern, delemont, solothurn, fribourg, neuchatel, lausanne, genf, altdorf, stans, basel, appenzell, herisau
};
for (int i = 0; i < allRadioButtons.length; i++) {
RadioButton setToUnSelected = allRadioButtons[i];
for (Stadt stadtToUnSelected : stadte) {
stadtToUnSelected.setIsClicked(false);
}
setToUnSelected.setSelected(false);
}
}
@FXML
private void handleButtonAction(ActionEvent event) {
addCities();
for (Stadt stadt : stadte) {
if (stadt.isIsClicked()) {
ausgewaehlt.add(stadt);
if(ausgewaehlt.size() == 1)
{
/*alert.setTitle("Warnung");
alert.setHeaderText("Schau, eine Warnung wurde herausgegeben!");
alert.setContentText("Du hast nur eine Stadt ausgewählt. Um das Programm korrekt laufen zu lassen, muss man mehr als eine Stadt auswählen.");
alert.showAndWait();
System.err.println("Nur eine Stadt ausgewählt");*/
//alles kommentiere adel
}
}
}
starter();
for (Stadt stadt : ausgewaehlt) {
System.out.println(stadt.getName());
for(Line line: lines)
{
try {
anchorPane.getChildren().add(line);
do {
System.out.println(line);
}
while(ausgewaehlt.size() < lines.size());
}catch(IllegalArgumentException e)
{
}
}
}
}
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() + 9, ausgewaehlt.get(i).getyCoord() + 9, ausgewaehlt.get(i+1).getxCoord() + 9, ausgewaehlt.get(i+1).getyCoord() + 9));
}else {
lines.add(new Line(ausgewaehlt.get(i).getxCoord() + 9, ausgewaehlt.get(i).getyCoord() + 9, ausgewaehlt.get(0).getxCoord() + 9, ausgewaehlt.get(0).getyCoord() + 9));
}
}
}
public boolean check(RadioButton rb) {
if (rb.isSelected()) {
return true;
} else {
return false;
}
}
@Override
public void initialize(URL location, ResourceBundle resources) {
//addCities();
//starter();
}
}
FXMLDocument.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.net.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.image.*?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.*?>
<AnchorPane fx:id="anchorPane" prefHeight="773.0" prefWidth="1184.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="tsprealone.FXMLDocumentController">
<children>
<ImageView fx:id="img" fitHeight="771.0" fitWidth="1184.0">
<image>
<Image url="@schweizerkarte-schweiz.png" />
</image>
</ImageView>
<RadioButton fx:id="zug" layoutX="659.0" layoutY="228.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="zurich" layoutX="659.0" layoutY="166.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="stgallen" layoutX="873.0" layoutY="145.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="schaffhausen" layoutX="682.0" layoutY="51.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="aarau" layoutX="538.0" layoutY="159.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="chur" layoutX="909.0" layoutY="344.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="glarus" layoutX="794.0" layoutY="275.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="frauenfeld" layoutX="746.0" layoutY="94.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="schwyz" layoutX="694.0" layoutY="293.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="luzern" layoutX="599.0" layoutY="275.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="bellinzona" layoutX="794.0" layoutY="585.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="sion" layoutX="364.0" layoutY="576.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="liestal" layoutX="462.0" layoutY="121.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="bern" layoutX="382.0" layoutY="315.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="delemont" layoutX="367.0" layoutY="168.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="solothurn" layoutX="408.0" layoutY="225.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="fribourg" layoutX="319.0" layoutY="367.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="neuchatel" layoutX="258.0" layoutY="302.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="lausanne" layoutX="180.0" layoutY="474.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="genf" layoutX="53.0" layoutY="576.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="stans" layoutX="622.0" layoutY="306.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="basel" layoutX="423.0" layoutY="94.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="appenzell" layoutX="882.0" layoutY="169.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="herisau" layoutX="849.0" layoutY="154.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<Button fx:id="generiere" layoutX="1035.0" layoutY="640.0" mnemonicParsing="false" onAction="#handleButtonAction" text="Generiere Linien" />
<Button fx:id="clear" layoutX="1035.0" layoutY="690.0" mnemonicParsing="false" onAction="#clearPane" text="Lösche Linien" />
<RadioButton fx:id="sarnen" layoutX="591.0" layoutY="338.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="altdorf" layoutX="693.0" layoutY="338.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
</children>
</AnchorPane>
Upvotes: 0
Views: 59
Reputation: 96
It looks like your second and third marked points are third and forth in your ArrayList.
It seems so because in this loop
for (Stadt stadt : stadte) {
if (stadt.isIsClicked()) {
ausgewaehlt.add(stadt);
}
cities are ordered by their order in FXMLDocument.
I advice you to add your cities in list when you are clicking on the city points. When you end marking and click on your "Generiere Linien" button, you just should render the linked lines.
Upvotes: 0
Reputation: 82461
for (Stadt stadt : stadte) {
if (stadt.isIsClicked()) {
ausgewaehlt.add(stadt);
if(ausgewaehlt.size() == 1)
{
/*alert.setTitle("Warnung");
alert.setHeaderText("Schau, eine Warnung wurde herausgegeben!");
alert.setContentText("Du hast nur eine Stadt ausgewählt. Um das Programm korrekt laufen zu lassen, muss man mehr als eine Stadt auswählen.");
alert.showAndWait();
System.err.println("Nur eine Stadt ausgewählt");*/
//alles kommentiere adel
}
}
}
This adds the cities in the same order in which they occur in the stadte
list. This may or may not be the same order the user clicked the buttons...
In this case it is not...
The better idea would be adding a RadioButton
to the list when it's selected. I recommmend adding the Stadt
instances as to the RadioButton
s instead of adding them to a list. Also remove the clicked
property (or how you call it) from the class, since this doesn't seem necessary.
private static void createStadt(RadioButton button, String name) {
button.setUserData(new Stadt(name, button.getLayoutX(), button.getLayoutY()));
}
private final List<Stadt> selectedCities = new ArrayList<>();
public void initialize(URL location, ResourceBundle resources) {
createStadt(zug, "Zug");
createStadt(zurich, "Zurich");
...
allRadioButtons = new RadioButton[] {
zug, zurich, schaffhausen, stgallen, aarau, chur, glarus, frauenfeld, schwyz, luzern, sarnen, bellinzona, sion, liestal, bern, delemont, solothurn, fribourg, neuchatel, lausanne, genf, altdorf, stans, basel, appenzell, herisau
};
}
@FXML
private void citySelectionAction(ActionEvent event) {
RadioButton source = (RadioButton) event.getSource();
Stadt city = (Stadt) source.getUserData();
if (source.isSelected()) {
selectedCities.add(city);
} else {
selectedCities.remove(city);
}
}
private RadioButton[] allRadioButtons;
@FXML
private void clearPane(ActionEvent event) {
anchorPane.getChildren().removeAll(lines);
lines.clear();
selectedCities.clear();
for (RadioButton button : allRadioButtons) {
button.setSelected(false);
}
}
@FXML
private void handleButtonAction(ActionEvent event) {
final int size = selectedCities.size();
switch (size) {
case 1:
/*alert.setTitle("Warnung");
alert.setHeaderText("Schau, eine Warnung wurde herausgegeben!");
alert.setContentText("Du hast nur eine Stadt ausgewählt. Um das Programm korrekt laufen zu lassen, muss man mehr als eine Stadt auswählen.");
alert.showAndWait();
System.err.println("Nur eine Stadt ausgewählt");*/
case 0:
break;
default:
Stadt c1 = selectedCities.get(0);
Stadt c2 = selectedCities.get(size-1);
lines.add(new Line(c1.getxCoord() + 9, c1.getyCoord() + 9, c2.getxCoord() + 9, c2.getyCoord() + 9));
// intentional fallthrough
case 2:
Stadt currentCity = selectedCities.get(0);
for(int i = 1; i < size; i++) {
Stadt nextCity = selectedCities.get(i);
lines.add(new Line(currentCity.getxCoord() + 9, currentCity.getyCoord() + 9, nextCity.getxCoord() + 9, nextCity.getyCoord() + 9));
currentCity = nextCity;
}
}
selectedCities.clear();
// start adding lines starting at the end until a line is found that is
// already added to the anchorPane
Line line;
for (int i = lines.size()-1; (i >= 0) && ((line = lines.get(i)).getParent() == null); i--) {
anchorPane.getChildren().add(line);
}
}
...
<RadioButton fx:id="zug" onAction="#citySelectionAction" layoutX="659.0" layoutY="228.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
<RadioButton fx:id="zurich" onAction="#citySelectionAction" layoutX="659.0" layoutY="166.0" mnemonicParsing="false" prefHeight="0.0" prefWidth="0.0" style="-fx-color: red;" />
...
Upvotes: 1