Ein herzliches Hallo an euch.
Ich hätte eine bitte an diejenigen unter euch, die sich mit JavaFX auskennen. Und zwar haben wir in der Uni nun begonnen mit JavaFX zu arbeiten. Wir haben eine erste Übungsaufgabe bekommen die ich auch gemacht habe und alles funktioniert, wie es funktionieren soll.
Jedoch bin ich mir ziemlich sicher, dass ich viel zu viele Umwege bis zum Ziel gegangen bin und mir das leben damit ziemlich schwer gemacht habe. Manchmal wusste ich intuitiv dass ich es viel zu umständlich mache, aber wusste einfach nicht wie ich es einfacher machen soll :D :rolleyes:
Ich habe sogar erfahren, dass manche Leute die Aufgabe mit 32 Zeilen Code (incl. imports) geschrieben haben. Ich habe 134 Zeilen :D:D.
Ich würde mich einfach sehr freuen, wenn sich jemand mal ein bisschen in meinen Code reinlesen könnte mir sagen kann, was ich besser machen kann.
Anbei mein Code und die Aufgabe
Danke und liebe Grüße
Code:
package game;
import javafx.scene.control.Control;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class AllLightsOn extends Application {
public Button[][] buttons = new Button[5][5];
public Button btn_reset = new Button("reset");
public boolean[] isButtonOn = new boolean[25];
public Label lbl_On = new Label("On: ");
public Label lbl_Off = new Label("Off: ");
public HBox hBox = new HBox(30);
public int counter = 0;
public String pressedButtonId;
public BorderPane root = new BorderPane();
public GridPane gridPane = new GridPane();
public Scene scene = new Scene(root, 350, 400);
public void changeColor(String index) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (buttons[i][j].getId() == index) {
if (isButtonOn[Integer.parseInt(index)] == true) {
buttons[i][j].setStyle("-fx-background-color: blue");
isButtonOn[Integer.parseInt(index)] = false;
} else {
buttons[i][j].setStyle("-fx-background-color: yellow");
isButtonOn[Integer.parseInt(index)] = true;
}
}
}
}
}
public void startGame(String index) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (buttons[i][j].getId() == index) {
changeColor(index);
if (i < 4) {
changeColor(buttons[i + 1][j].getId());
}
if (i > 0) {
changeColor(buttons[i - 1][j].getId());
}
if (j < 4) {
changeColor(buttons[i][j + 1].getId());
}
if (j > 0) {
changeColor(buttons[i][j - 1].getId());
}
}
}
}
refreshLabel();
}
public void refreshLabel() {
int t = 0, f = 0;
for (int i = 0; i < isButtonOn.length; i++) {
if (isButtonOn[i] == true) {
t++;
} else {
f++;
}
}
lbl_On.setText("On: " + t);
lbl_Off.setText("Off: " + f);
}
[MENTION=295804]Override[/MENTION]
public void start(Stage primaryStage) throws Exception {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
buttons[i][j] = new Button();
buttons[i][j].setId(String.valueOf(counter));
buttons[i][j].setMaxWidth(Double.MAX_VALUE);
buttons[i][j].setMaxHeight(Double.MAX_VALUE);
buttons[i][j].setStyle("-fx-background-color: blue");
isButtonOn[counter] = false;
gridPane.add(buttons[i][j], i, j);
gridPane.setHgrow(buttons[i][j], Priority.ALWAYS);
gridPane.setVgrow(buttons[i][j], Priority.ALWAYS);
GridPane.setFillWidth(buttons[i][j], true);
GridPane.setFillHeight(buttons[i][j], true);
lbl_On.setText("On: ");
lbl_Off.setText("Off: 25");
buttons[i][j].setOnAction(new EventHandler<ActionEvent>() {
[MENTION=295804]Override[/MENTION]
public void handle(ActionEvent event) {
pressedButtonId = ((Control) event.getSource()).getId();
startGame(pressedButtonId);
}
});
counter++;
}
}
btn_reset.setOnAction(new EventHandler<ActionEvent>() {
[MENTION=295804]Override[/MENTION]
public void handle(ActionEvent event) {
for (int k = 0; k < isButtonOn.length; k++) {
isButtonOn[k] = false;
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
buttons[i][j].setStyle("-fx-background-color: blue");
}
}
lbl_Off.setText("Off: 25");
lbl_On.setText("On: ");
}
});
gridPane.setGridLinesVisible(true);
root.setCenter(gridPane);
hBox.getChildren().addAll(btn_reset, lbl_On, lbl_Off);
hBox.setAlignment(Pos.CENTER);
root.setBottom(hBox);
primaryStage.setTitle("JavaFX All Lights On");
primaryStage.setScene(scene);
primaryStage.show();
}
}