forked from AdvancedProgramming1404/HW-07-JavaFX
implement all TODOs
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -36,7 +36,12 @@
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency> </dependencies>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjfx</groupId>
|
||||
<artifactId>javafx-media</artifactId>
|
||||
<version>20</version> </dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -4,5 +4,7 @@ import javafx.application.Application;
|
||||
|
||||
public class Launcher{
|
||||
public static void main(String[] args) {
|
||||
Application.launch(MusicApp.class);
|
||||
}
|
||||
// TODO: Launch the JavaFX application by calling Application.launch(Main.class) }
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package sbu.javafx;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
@@ -8,7 +9,6 @@ import javafx.stage.Stage;
|
||||
import java.io.IOException;
|
||||
|
||||
public class MusicApp extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException
|
||||
{
|
||||
|
||||
@@ -1,31 +1,127 @@
|
||||
package sbu.javafx;
|
||||
|
||||
import javafx.collections.FXCollections;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.control.ListView;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.media.Media;
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class MusicController {
|
||||
|
||||
// TODO: Define your UI components using the @FXML annotation.
|
||||
|
||||
@FXML private Label nowPlayingLabel;
|
||||
private Button currentlyPlayingButton = null;
|
||||
private ObservableList<String> playlistItems = FXCollections.observableArrayList();
|
||||
private ListView<String> playlistView = new ListView<>(playlistItems);
|
||||
private Stage playlistStage = null;
|
||||
private MediaPlayer mediaPlayer = null;
|
||||
//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.
|
||||
Button clickedButton = (Button) event.getSource();
|
||||
if(clickedButton == currentlyPlayingButton)
|
||||
{
|
||||
currentlyPlayingButton.setText("Play");
|
||||
clickedButton.setStyle("-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;"); // برگشت به رنگ آبی
|
||||
nowPlayingLabel.setText("-");
|
||||
currentlyPlayingButton = null;
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.stop();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (currentlyPlayingButton != null)
|
||||
{
|
||||
currentlyPlayingButton.setText("Play");
|
||||
currentlyPlayingButton.setStyle("-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;");
|
||||
if (mediaPlayer != null) {
|
||||
mediaPlayer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
clickedButton.setText("Stop");
|
||||
clickedButton.setStyle("-fx-background-color: #e74c3c; -fx-text-fill: white; -fx-background-radius: 3;");
|
||||
currentlyPlayingButton = clickedButton;
|
||||
HBox hBox = (HBox) clickedButton.getParent();
|
||||
String title = ((Label)hBox.getChildren().getFirst()).getText();
|
||||
nowPlayingLabel.setText(title);
|
||||
try {
|
||||
String fileURI = getMusicFilePath(title); // گرفتن آدرس استاندارد فایل
|
||||
Media media = new Media(fileURI); // گذاشتن نوار کاست
|
||||
mediaPlayer = new MediaPlayer(media); // قراردادن در دستگاه پخش
|
||||
mediaPlayer.play(); // 🚀 شروع پخش!
|
||||
} catch (Exception e) {
|
||||
System.out.println("Error playing file: " + e.getMessage());
|
||||
// اگر فایلی پیدا نشد یا خطایی رخ داد، اینجا پرینت میشود تا برنامه کرش نکند
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//TODO: Logic to add a specific song to the user's playlist.
|
||||
|
||||
public void addToPlaylist(String songName) {
|
||||
@FXML
|
||||
public void addToPlaylist(ActionEvent event) {
|
||||
//Add the song to a list or update a ListView.
|
||||
Button clickedButton = (Button) event.getSource();
|
||||
HBox row = (HBox) clickedButton.getParent();
|
||||
Label songTitleLabel = (Label) row.getChildren().getFirst();
|
||||
String songName = songTitleLabel.getText();
|
||||
if(!playlistItems.contains(songName))
|
||||
{
|
||||
playlistItems.add(songName);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
|
||||
|
||||
@FXML
|
||||
public void openPlaylistWindow() {
|
||||
//Create a new stage and show the playlist UI.
|
||||
if (playlistStage != null && playlistStage.isShowing())
|
||||
{
|
||||
playlistStage.toFront();
|
||||
return;
|
||||
}
|
||||
playlistStage = new Stage();
|
||||
playlistStage.setTitle("My Playlist");
|
||||
playlistView.setStyle(
|
||||
"-fx-background-color: #ffffff; " +
|
||||
"-fx-background-radius: 5; " +
|
||||
"-fx-border-color: #dcdde1; " +
|
||||
"-fx-border-width: 1; " +
|
||||
"-fx-font-size: 14px; " +
|
||||
"-fx-padding: 5;"
|
||||
);
|
||||
VBox root = new VBox(playlistView);
|
||||
root.setStyle("-fx-background-color: #f5f5f5; -fx-padding: 15;");
|
||||
VBox.setVgrow(playlistView, javafx.scene.layout.Priority.ALWAYS);
|
||||
Scene scene = new Scene(root, 300, 400);
|
||||
playlistStage.setScene(scene);
|
||||
playlistStage.show();
|
||||
}
|
||||
|
||||
private String getMusicFilePath(String songName) {
|
||||
String fileName = switch (songName) {
|
||||
case "Jana" -> "src\\main\\resources\\music\\Jana.mp3";
|
||||
case "Jadoo" -> "src\\main\\resources\\music\\@RapRelease - Jadoo - Pidar.mp3";
|
||||
case "we never dated" -> "src\\main\\resources\\music\\we never dated.mp3";
|
||||
default -> "";
|
||||
};
|
||||
File file = new File(fileName);
|
||||
return file.toURI().toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,8 +1,76 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10">
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.layout.Region?>
|
||||
|
||||
<!-- TODO: Add your code here! -->
|
||||
<!--take care about fx:ids-->
|
||||
|
||||
<VBox spacing="15" xmlns="http://javafx.com/javafx/20" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sbu.javafx.MusicController" style="-fx-background-color: #f5f5f5;" alignment="CENTER">
|
||||
|
||||
<padding>
|
||||
<Insets top="20" right="20" bottom="0" left="20"/>
|
||||
</padding>
|
||||
|
||||
<!-- عنوان -->
|
||||
<Label text="Music Player" style="-fx-font-size: 24px; -fx-font-weight: bold; -fx-text-fill: #2c3e50;"/>
|
||||
|
||||
<!-- جدول آهنگها -->
|
||||
<VBox spacing="5" style="-fx-background-color: white; -fx-border-color: #dcdde1; -fx-border-width: 1;">
|
||||
<padding>
|
||||
<Insets top="10" right="10" bottom="10" left="10"/>
|
||||
</padding>
|
||||
|
||||
<!-- ردیف عنوان -->
|
||||
<HBox spacing="50" style="-fx-padding: 10 0 10 0; -fx-border-width: 0 0 1 0; -fx-border-color: #bdc3c7;" alignment="CENTER">
|
||||
<Label text="Song Title" minWidth="150" style="-fx-font-weight: bold;"/>
|
||||
<Label text="Artist Name" minWidth="150" style="-fx-font-weight: bold;"/>
|
||||
<Label text="Duration" minWidth="60" style="-fx-font-weight: bold;"/>
|
||||
<Label text="Add to Playlist" minWidth="120" style="-fx-font-weight: bold;"/>
|
||||
<Label text="Play" minWidth="50" style="-fx-font-weight: bold;"/>
|
||||
</HBox>
|
||||
|
||||
<!-- آهنگ 1 -->
|
||||
<HBox spacing="50" style="-fx-padding: 10 0 10 0; -fx-border-width: 0 0 1 0; -fx-border-color: #bdc3c7;" alignment="CENTER">
|
||||
<Label text="Jana" minWidth="150"/>
|
||||
<Label text="SaM" minWidth="150"/>
|
||||
<Label text="04:16" minWidth="60"/>
|
||||
<Button text="Add to Playlist" minWidth="120" onAction="#addToPlaylist" style="-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
<Button text="Play" minWidth="50" onAction="#handlePlayAction" style="-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
</HBox>
|
||||
|
||||
<!-- آهنگ 2 -->
|
||||
<HBox spacing="50" style="-fx-padding: 10 0 10 0; -fx-border-width: 0 0 1 0; -fx-border-color: #bdc3c7;" alignment="CENTER">
|
||||
<Label text="we never dated" minWidth="150"/>
|
||||
<Label text="sombr" minWidth="150"/>
|
||||
<Label text="03:16" minWidth="60"/>
|
||||
<Button text="Add to Playlist" onAction="#addToPlaylist" minWidth="120" style="-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
<Button text="Play" minWidth="50" onAction="#handlePlayAction" style="-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
</HBox>
|
||||
|
||||
<!-- آهنگ 3 -->
|
||||
<HBox spacing="50" style="-fx-padding: 10 0 10 0;" alignment="CENTER">
|
||||
<Label text="Jadoo" minWidth="150"/>
|
||||
<Label text="Pidar" minWidth="150"/>
|
||||
<Label text="03:30" minWidth="60"/>
|
||||
<Button text="Add to Playlist" minWidth="120" onAction="#addToPlaylist" style="-fx-background-color: #27ae60; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
<Button text="Play" minWidth="50" onAction="#handlePlayAction" style="-fx-background-color: #3498db; -fx-text-fill: white; -fx-background-radius: 3;"/>
|
||||
</HBox>
|
||||
</VBox>
|
||||
|
||||
<!-- بخش پایین: Now Playing و Open Playlist -->
|
||||
<HBox alignment="CENTER_LEFT" style="-fx-background-color: #ecf0f1; -fx-padding: 15; -fx-background-radius: 8;">
|
||||
<HBox spacing="10">
|
||||
<Label text="🎵 Now Playing:" style="-fx-font-weight: bold;"/>
|
||||
<Label fx:id="nowPlayingLabel" text="-" style="-fx-text-fill: #3498db;"/>
|
||||
</HBox>
|
||||
<Region HBox.hgrow="ALWAYS"/>
|
||||
<Button text="Open Playlist" onAction="#openPlaylistWindow" style="-fx-background-color: #3498db; -fx-text-fill: white; -fx-font-weight: bold; -fx-padding: 8 20; -fx-background-radius: 5;"/>
|
||||
</HBox>
|
||||
|
||||
<!--learn and use view lsit-->
|
||||
|
||||
</VBox>
|
||||
Reference in New Issue
Block a user