From b5959e532999ab2af70857531519527481e57511 Mon Sep 17 00:00:00 2001 From: Matin Date: Tue, 9 Jun 2026 13:58:56 +0330 Subject: [PATCH] Making a music program --- src/main/java/sbu/javafx/Launcher.java | 3 +- src/main/java/sbu/javafx/MusicApp.java | 5 +- src/main/java/sbu/javafx/MusicController.java | 104 +++++++++++++++++- .../java/sbu/javafx/PlaylistController.java | 18 +++ src/main/java/sbu/javafx/Song.java | 17 +++ src/main/resources/sbu/javafx/App-view.fxml | 48 +++++++- src/main/resources/sbu/javafx/icon.png | Bin 0 -> 32718 bytes .../resources/sbu/javafx/playlist-view.fxml | 14 +++ 8 files changed, 202 insertions(+), 7 deletions(-) create mode 100644 src/main/java/sbu/javafx/PlaylistController.java create mode 100644 src/main/java/sbu/javafx/Song.java create mode 100644 src/main/resources/sbu/javafx/icon.png create mode 100644 src/main/resources/sbu/javafx/playlist-view.fxml diff --git a/src/main/java/sbu/javafx/Launcher.java b/src/main/java/sbu/javafx/Launcher.java index c288d10..a3ffd1f 100644 --- a/src/main/java/sbu/javafx/Launcher.java +++ b/src/main/java/sbu/javafx/Launcher.java @@ -4,5 +4,6 @@ import javafx.application.Application; public class Launcher{ public static void main(String[] args) { - // TODO: Launch the JavaFX application by calling Application.launch(Main.class) } + Application.launch(MusicApp.class); + } } diff --git a/src/main/java/sbu/javafx/MusicApp.java b/src/main/java/sbu/javafx/MusicApp.java index a50badc..64c786b 100644 --- a/src/main/java/sbu/javafx/MusicApp.java +++ b/src/main/java/sbu/javafx/MusicApp.java @@ -3,6 +3,7 @@ package sbu.javafx; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; +import javafx.scene.image.Image; import javafx.stage.Stage; import java.io.IOException; @@ -16,7 +17,9 @@ public class MusicApp extends Application { Scene scene = new Scene(fxmlLoader.load()); stage.setTitle("Music Player"); stage.setScene(scene); - //TODO: add icon to the stage + stage.setWidth(900); + stage.setHeight(500); + stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png"))); stage.show(); } diff --git a/src/main/java/sbu/javafx/MusicController.java b/src/main/java/sbu/javafx/MusicController.java index 5da27a9..fb6a11c 100644 --- a/src/main/java/sbu/javafx/MusicController.java +++ b/src/main/java/sbu/javafx/MusicController.java @@ -1,31 +1,127 @@ package sbu.javafx; +//main import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; + +//event import javafx.event.ActionEvent; + +//UI controller +import javafx.scene.control.Button; import javafx.scene.control.Label; +import javafx.scene.control.ListView; +import javafx.scene.image.Image; import javafx.scene.input.MouseEvent; + +//layout import javafx.scene.layout.VBox; +//stage and scene +import javafx.stage.Stage; +import javafx.scene.Scene; + +import java.util.ArrayList; +import java.io.IOException; + public class MusicController { - // TODO: Define your UI components using the @FXML annotation. + @FXML private Label nowPlayingLabel; + @FXML private VBox playlistContainer; + @FXML private ListView playlistView; + + private ArrayList playlist = new ArrayList<>(); + private ArrayList songList = new ArrayList<>(); + private Stage primaryStage; + + public void setPrimaryStage(Stage stage) { + this.primaryStage = stage; + } + + + + //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() { + public void handlePlayAction(ActionEvent event) { //Update labels, change button icons, etc. + String songName; + + Button clickedButton = (Button) event.getSource(); + String buttonText = clickedButton.getText(); + + if(clickedButton.getId() != null){ + switch (clickedButton.getId()){ + case "playM1" : + songName = "Song of Dawn"; + break; + case "playM2" : + songName = "Midnight Drive"; + break; + case "playM3" : + songName = "Echoes of You"; + break; + default: + songName = "Unknown Song"; + } + } + else + songName = "Selected Song"; + + nowPlayingLabel.setText("Now playing: " + songName); } //TODO: Logic to add a specific song to the user's playlist. + @FXML public void addToPlaylist(String songName) { //Add the song to a list or update a ListView. + playlist.add(songName); + nowPlayingLabel.setText(songName + " added to playlist :)"); + + if (playlistView != null) { + playlistView.getItems().add(songName); + } + } + + @FXML + public void handleAddToPlaylist (ActionEvent event){ + Button clickButton = (Button) event.getSource(); + String songName; + if(clickButton.getId() != null){ + switch (clickButton.getId()){ + case "addM1" : + songName = "Song of Dawn | Alice Johnson | 03:45"; + break; + case "addM2" : + songName = "Midnight Drive | The Night Owls | 04:12"; + break; + case "addM3" : + songName = "Echoes of You | Luna Sky | 05:08"; + break; + default: + songName = "Unknown Song"; + } + } + else + songName = "Selected Song"; + + addToPlaylist(songName); } // TODO:This method should open a new window or change the current scene to display the "Playlist" page. + @FXML + public void openPlaylistWindow() throws IOException { + FXMLLoader loader = new FXMLLoader(getClass().getResource("playlist-view.fxml")); + Scene scene = new Scene(loader.load()); + PlaylistController pc = loader.getController(); + pc.setPlaylist(playlist); + Stage stage = new Stage(); + stage.setScene(scene); + stage.getIcons().add(new Image(getClass().getResourceAsStream("icon.png"))); + stage.show(); - public void openPlaylistWindow() { - //Create a new stage and show the playlist UI. } } diff --git a/src/main/java/sbu/javafx/PlaylistController.java b/src/main/java/sbu/javafx/PlaylistController.java new file mode 100644 index 0000000..d5e9e46 --- /dev/null +++ b/src/main/java/sbu/javafx/PlaylistController.java @@ -0,0 +1,18 @@ +package sbu.javafx; + +import javafx.fxml.FXML; +import javafx.scene.control.ListView; + +import java.util.ArrayList; + +public class PlaylistController { + private ArrayList items; + @FXML private ListView itemPlaylist; + + public void setPlaylist(ArrayList items){ + this.items = items; + for(String item : items){ + itemPlaylist.getItems().add(item); + } + } +} diff --git a/src/main/java/sbu/javafx/Song.java b/src/main/java/sbu/javafx/Song.java new file mode 100644 index 0000000..14fe53b --- /dev/null +++ b/src/main/java/sbu/javafx/Song.java @@ -0,0 +1,17 @@ +package sbu.javafx; + +public class Song { + private String title; + private String artist; + private String duration; + + public Song (String title, String artist, String duration){ + this.title = title; + this.artist = artist; + this.duration = duration; + } + + public String getTitle () { return title;} + public String getArtist () { return artist;} + public String getDuration () { return duration;} +} diff --git a/src/main/resources/sbu/javafx/App-view.fxml b/src/main/resources/sbu/javafx/App-view.fxml index 306c8d3..2a6be43 100644 --- a/src/main/resources/sbu/javafx/App-view.fxml +++ b/src/main/resources/sbu/javafx/App-view.fxml @@ -1,8 +1,54 @@ - + + + + + +