From 939d6e0cba0700e48622d7be8a6f6f917daca612 Mon Sep 17 00:00:00 2001 From: Fateme Azizi Date: Thu, 28 May 2026 17:00:17 +0330 Subject: [PATCH] implement MusicController.java --- src/main/java/sbu/javafx/MusicController.java | 143 ++++++++++++++++-- 1 file changed, 131 insertions(+), 12 deletions(-) diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index 5da27a9..fcdb184 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,31 +1,150 @@ package sbu.javafx; -import javafx.fxml.FXML; import javafx.event.ActionEvent; +import javafx.fxml.FXML; +import javafx.scene.Scene; +import javafx.scene.control.Button; import javafx.scene.control.Label; -import javafx.scene.input.MouseEvent; +import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; +import javafx.stage.Stage; + +import java.util.ArrayList; public class MusicController { + ; + public Button PlayListButton; - // TODO: Define your UI components using the @FXML annotation. + public Label Name1; + public Label Singer1; + public Label Duration1; + public Button AddToPlaylist1; + public Button play1; + + public Label Name2; + public Label Singer2; + public Label Duration2; + public Button AddToPlaylist2; + public Button play2; + + public Label Name3; + public Label Singer3; + public Label Duration3; + public Button AddToPlaylist3; + public Button play3; + public Label nowPlayingLabel; + public Label nowPlayingSong; + public Button themeToggle; + + public HBox song1; + public HBox song2; + public HBox song3; + public HBox header; + public HBox playingBox; + + boolean isDark = false; + Button currentPlayingSong = null; + + ArrayList playlist = new ArrayList<>(); + boolean onPlaylist1 = false; + boolean onPlaylist2 = false; + boolean onPlaylist3 = false; - //TODO: Implement the logic to play a song. This method should update the UI to show which song is currently playing. @FXML - public void handlePlayAction() { - //Update labels, change button icons, etc. + public void handlePlayAction(ActionEvent actionEvent) { + Button button = (Button) actionEvent.getSource(); + + if(currentPlayingSong == button) { + currentPlayingSong.setText("Play"); + nowPlayingSong.setText(" "); + currentPlayingSong = null; + return; + } + + if(currentPlayingSong != null) { + currentPlayingSong.setText("Play"); + } + + + currentPlayingSong = button; + currentPlayingSong.setText("Playing..."); + + if(currentPlayingSong == play1) { + nowPlayingSong.setText(Name1.getText() + " | " + Singer1.getText()); + } + else if(currentPlayingSong == play2) { + nowPlayingSong.setText(Name2.getText() + " | " + Singer2.getText()); + } + else if (currentPlayingSong == play3) { + nowPlayingSong.setText(Name3.getText() + " | " + Singer3.getText()); + } } - //TODO: Logic to add a specific song to the user's playlist. - public void addToPlaylist(String songName) { - //Add the song to a list or update a ListView. + @FXML + public void addToPlaylist(ActionEvent event) { + + Button button = (Button) event.getSource(); + + if(button == AddToPlaylist1 && !onPlaylist1) { + playlist.add(song1); + AddToPlaylist1.setText("On Playlist"); + } + else if(button == AddToPlaylist2 && !onPlaylist2) { + playlist.add(song2); + AddToPlaylist2.setText("On Playlist"); + } + else if(button == AddToPlaylist3 && !onPlaylist3) { + playlist.add(song3); + AddToPlaylist3.setText("On Playlist"); + } } - // TODO:This method should open a new window or change the current scene to display the "Playlist" page. + public void openPlaylistWindow(ActionEvent event) { - public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. + Button button = (Button) event.getSource(); + + Stage stage = (Stage) button.getScene().getWindow(); + + stage.setTitle("Playlist"); + VBox root = new VBox(); + + if(!playlist.isEmpty()){ + root.getChildren().add(header); + root.getChildren().addAll(playlist); + root.getChildren().add(playingBox); + } + else { + Label label = new Label("Your playlist is empty."); + if(!isDark) + label.setStyle("-fx-font-size: 24px;"); + else + label.setStyle("-fx-font-size: 24px; ; -fx-text-fill: #e0e0e0;"); + root.getChildren().add(label); + } + + + Scene scene = new Scene(root); + if(!isDark) + scene.getStylesheets().add(getClass().getResource("light.css").toExternalForm()); + else + scene.getStylesheets().add(getClass().getResource("dark.css").toExternalForm()); + + stage.setScene(scene); + } + + @FXML + public void toggleTheme() { + isDark = !isDark; + themeToggle.getScene().getStylesheets().clear(); + if(isDark) { + themeToggle.getScene().getStylesheets().add(getClass().getResource("dark.css").toExternalForm()); + themeToggle.setText("Switch to Light Mode"); + } + else { + themeToggle.getScene().getStylesheets().add(getClass().getResource("light.css").toExternalForm()); + themeToggle.setText("Switch to Dark Mode"); + } } }