Files
HW-07-JavaFX/src/main/java/sbu/javafx/MusicController.java
T

191 lines
5.5 KiB
Java

package sbu.javafx;
import javafx.fxml.FXML;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
public class MusicController {
@FXML
public ListView<Song> playlistListView;
@FXML
public ListView<Song> musicsListView;
@FXML
private VBox root;
@FXML
Label nowPlayingLabel;
@FXML
Button playPauseBtn;
@FXML
Button openPlaylistBtn;
@FXML
Button themeToggleBtn;
private boolean isDarkMode=true;
private boolean isPlaying=false;
private Song currentSong = null;
ImageView playIcon;
ImageView pauseIcon;
// TODO: Define your UI components using the @FXML annotation.
@FXML
public void initialize() {
Image playImg = new Image(getClass().getResourceAsStream("/image/play.png"));
Image pauseImg = new Image(getClass().getResourceAsStream("/image/pause.png"));
playIcon = new ImageView(playImg);
pauseIcon = new ImageView(pauseImg);
playIcon.setFitWidth(35);
playIcon.setFitHeight(35);
pauseIcon.setFitWidth(35);
pauseIcon.setFitHeight(35);
playPauseBtn.setGraphic(playIcon);
}
public void setupListView(ListView<Song> listView) {
listView.setCellFactory(param -> new ListCell<Song>() {
@Override
protected void updateItem(Song song, boolean empty) {
super.updateItem(song, empty);
if (empty || song == null) {
setText(null);
setGraphic(null);
return;
}
Label nameLabel = new Label(song.getName());
nameLabel.setStyle("-fx-font-weight: bold; -fx-font-size: 14px;");
Label singerLabel = new Label(song.getSinger());
singerLabel.setStyle("-fx-text-fill: gray; -fx-font-size: 12px;");
VBox songInfo = new VBox(3);
songInfo.setAlignment(Pos.CENTER_LEFT);
songInfo.getChildren().addAll(nameLabel, singerLabel);
Button addToPlaylistBtn = new Button("Add to playlist");
addToPlaylistBtn.getStyleClass().add("playlist-button");
addToPlaylistBtn.setOnAction(e -> {
addToPlaylist(e, song);
});
Button playBtn = new Button("Play");
playBtn.getStyleClass().add("play-button");
playBtn.setOnAction(e -> {
play(e, song);
});
HBox row = new HBox(10);
row.setPadding(new Insets(10));
row.setAlignment(Pos.CENTER_LEFT);
Region spacer = new Region();
HBox.setHgrow(spacer, Priority.ALWAYS);
row.getChildren().addAll(songInfo, spacer, playBtn, addToPlaylistBtn);
setGraphic(row);
}
});
}
@FXML
public void toggleTheme(ActionEvent event) {
if (isDarkMode) {
themeToggleBtn.setText("Light");
changeTheme();
isDarkMode = false;
} else {
themeToggleBtn.setText("Dark");
changeTheme();
isDarkMode = true;
}
}
public void changeTheme(){
Scene scene = root.getScene();
scene.getStylesheets().clear();
if(isDarkMode){
scene.getStylesheets().add(
getClass().getResource("/css/dark.css").toExternalForm()
);
}else{
scene.getStylesheets().add(
getClass().getResource("/css/light.css").toExternalForm()
);
}
isDarkMode=!isDarkMode;
}
//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 play(ActionEvent event, Song song){
currentSong=song;
nowPlayingLabel.setText(song.getName());
if (!isPlaying)
playPause(event);
//کد پخش
}
@FXML
public void playPause(ActionEvent a){
if(isPlaying){
playPauseBtn.setGraphic(playIcon);
isPlaying=false;
//کد توقف اینجاست چون برای هر توقفی دکمه ی پاز رو میزنیم ولی برای پلی ممکنه از بین آهنگ ها بزنیم
}
else{
playPauseBtn.setGraphic(pauseIcon);
isPlaying=true;
}
}
//TODO: Logic to add a specific song to the user's playlist.
public void addToPlaylist(ActionEvent event, Song song) {
//Add the song to a list or update a ListView.
playlistListView.getItems().add(song);
}
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
public void openPlaylistWindow() {
//Create a new stage and show the playlist UI.
musicsListView.setVisible(false);
if(playlistListView!=null)
playlistListView.setVisible(true);
else
throw new RuntimeException("Playlist is empty");
}
}