complete the main page
This commit is contained in:
@@ -4,5 +4,8 @@ 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);
|
||||
}
|
||||
}
|
||||
//the icon is not displayed
|
||||
|
||||
|
||||
@@ -1,22 +1,45 @@
|
||||
package sbu.javafx;
|
||||
|
||||
import de.codecentric.centerdevice.javafxsvg.SvgImageLoaderFactory;
|
||||
import javafx.application.Application;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
public class MusicApp extends Application {
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException
|
||||
{
|
||||
// SvgImageLoaderFactory.install();
|
||||
// InputStream iconStream = getClass().getResourceAsStream("icon.svg");
|
||||
// if (iconStream != null)
|
||||
// {
|
||||
// Image icon = new Image(iconStream);
|
||||
// stage.getIcons().add(icon);
|
||||
// System.out.println("yesss");
|
||||
// } else
|
||||
// {
|
||||
// System.out.println("not found");
|
||||
// }
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(MusicApp.class.getResource("App-view.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
stage.setTitle("Music Player");
|
||||
stage.setScene(scene);
|
||||
|
||||
//TODO: add icon to the stage
|
||||
// InputStream iconStream = new FileInputStream("musicIcon.png");
|
||||
// if (iconStream != null)
|
||||
// {
|
||||
// stage.getIcons().add(new Image(iconStream));
|
||||
// } else {
|
||||
// System.out.println("not found");
|
||||
// }
|
||||
stage.show();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,30 +2,182 @@ package sbu.javafx;
|
||||
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Parent;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.control.Button;
|
||||
import javafx.scene.control.Label;
|
||||
import javafx.scene.input.MouseEvent;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.media.Media;
|
||||
import javafx.scene.media.MediaPlayer;
|
||||
import javafx.stage.Stage;
|
||||
|
||||
|
||||
import javax.imageio.IIOException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MusicController {
|
||||
|
||||
// TODO: Define your UI components using the @FXML annotation.
|
||||
@FXML private Label nowPlayingSong;
|
||||
@FXML private Button playBtn1;
|
||||
@FXML private Button playBtn2;
|
||||
@FXML private Button playBtn3;
|
||||
@FXML private Button playBtn4;
|
||||
@FXML private Button add1;
|
||||
@FXML private Button add2;
|
||||
@FXML private Button add3;
|
||||
@FXML private Button add4;
|
||||
|
||||
private List<Song> playlist = new ArrayList<>();
|
||||
private Song currentSong = null;
|
||||
private MediaPlayer mediaPlayer;
|
||||
private Song song1 = new Song("The Comfort of a Laugh Track", "Roar", "04:52", "songs/song1.mp3");
|
||||
private Song song2 = new Song("Perfect Day", "Lou Reed", "03:44", "songs/song2.mp3");
|
||||
private Song song3 = new Song("Wish You Were Here", "Pink Floyd", "05:34", "songs/song3.mp3");
|
||||
private Song song4 = new Song("No Surprises", "Radiohead", "03:49", "songs/song4.mp3");
|
||||
|
||||
//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 event) {
|
||||
|
||||
Button clickedBotton = (Button) event.getSource();
|
||||
resetPlayButtons();
|
||||
String buttonId = clickedBotton.getId();
|
||||
switch (buttonId)
|
||||
{
|
||||
case "playBtn1":
|
||||
playSong(song1);
|
||||
playBtn1.setText("Playing");
|
||||
playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
|
||||
break;
|
||||
|
||||
case "playBtn2":
|
||||
playSong(song2);
|
||||
playBtn2.setText("Playing");
|
||||
playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
|
||||
break;
|
||||
|
||||
case "playBtn3":
|
||||
playSong(song3);
|
||||
playBtn3.setText("Playing");
|
||||
playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
|
||||
break;
|
||||
|
||||
case "playBtn4":
|
||||
playSong(song4);
|
||||
playBtn4.setText("Playing");
|
||||
playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px; -fx-background-color: lightBlue;");
|
||||
break;
|
||||
}
|
||||
|
||||
//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.
|
||||
}
|
||||
|
||||
// TODO:This method should open a new window or change the current scene to display the "Playlist" page.
|
||||
@FXML
|
||||
public void addToPlaylist(ActionEvent event) {
|
||||
|
||||
Button clickedButton = (Button) event.getSource();
|
||||
String btnId = clickedButton.getId();
|
||||
Song selectedSong = null;
|
||||
|
||||
switch (btnId)
|
||||
{
|
||||
case "add1":
|
||||
selectedSong = song1;
|
||||
break;
|
||||
case "add2":
|
||||
selectedSong = song2;
|
||||
break;
|
||||
case "add3":
|
||||
selectedSong = song3;
|
||||
break;
|
||||
case "add4":
|
||||
selectedSong = song4;
|
||||
break;
|
||||
}
|
||||
if (selectedSong != null)
|
||||
{
|
||||
playlist.add(selectedSong);
|
||||
}
|
||||
}
|
||||
|
||||
@FXML
|
||||
public void openPlaylistWindow() {
|
||||
//Create a new stage and show the playlist UI.
|
||||
try
|
||||
{
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("playlist.fxml"));
|
||||
Parent root = loader.load();
|
||||
|
||||
PlaylistController playlistController = loader.getController();
|
||||
playlistController.setPlaylist(playlist, this);
|
||||
|
||||
Stage playlistStage = new Stage();
|
||||
playlistStage.setTitle("Playlist");
|
||||
playlistStage.setScene(new Scene(root, 500, 400));
|
||||
playlistStage.show();
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void playSong(Song song) {
|
||||
if (mediaPlayer != null)
|
||||
{
|
||||
mediaPlayer.stop();
|
||||
mediaPlayer.dispose();
|
||||
}
|
||||
|
||||
try {
|
||||
File musicFile = new File(song.getFilePath());
|
||||
if (!musicFile.exists()) {
|
||||
nowPlayingSong.setText("Error: File not found - " + song.getFilePath());
|
||||
System.out.println("Not found: " + musicFile.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
Media media = new Media(musicFile.toURI().toString());
|
||||
mediaPlayer = new MediaPlayer(media);
|
||||
mediaPlayer.setOnEndOfMedia(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
resetPlayButtons();
|
||||
nowPlayingSong.setStyle("-fx-font-weight: bold; -fx-font-size: 20px;");
|
||||
nowPlayingSong.setText("-");
|
||||
if (mediaPlayer != null)
|
||||
{
|
||||
mediaPlayer.dispose();
|
||||
mediaPlayer = null;
|
||||
}
|
||||
}
|
||||
});
|
||||
mediaPlayer.play();
|
||||
currentSong = song;
|
||||
nowPlayingSong.setStyle("-fx-font-size: 20px;");
|
||||
nowPlayingSong.setText(song.getTitle() + " - " + song.getArtist());
|
||||
} catch (Exception e) {
|
||||
nowPlayingSong.setText("Playback error");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void resetPlayButtons()
|
||||
{
|
||||
playBtn1.setText("Play");
|
||||
playBtn1.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
|
||||
playBtn2.setText("Play");
|
||||
playBtn2.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
|
||||
playBtn3.setText("Play");
|
||||
playBtn3.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
|
||||
playBtn4.setText("Play");
|
||||
playBtn4.setStyle("-fx-pref-width: 120; -fx-font-size: 20px;");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,76 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<?import javafx.scene.layout.VBox?>
|
||||
<VBox xmlns="http://javafx.com/fxml" alignment="CENTER" spacing="10">
|
||||
<?import javafx.scene.control.Label?>
|
||||
<?import javafx.scene.layout.HBox?>
|
||||
<?import javafx.scene.control.Separator?>
|
||||
<?import javafx.scene.control.Button?>
|
||||
<VBox xmlns="http://javafx.com/fxml" xmlns:fx ="http://javafx.com/fxml" fx:controller="sbu.javafx.MusicController" alignment="TOP_CENTER" spacing="10" style="-fx-padding: 60">
|
||||
|
||||
<Label text="Music Player" style="-fx-font-size: 40px; -fx-font-weight: bold;"/>
|
||||
<Separator/>
|
||||
|
||||
<!--songs-->
|
||||
<VBox spacing="5" alignment="CENTER" style="-fx-padding: 0 0 5 0;">
|
||||
<HBox spacing="20" alignment="CENTER">
|
||||
<Label text="Song Title" style="-fx-font-weight: bold; -fx-pref-width: 290; -fx-font-size: 20px"/>
|
||||
<Label text="Artist Name" style="-fx-font-weight: bold; -fx-pref-width: 150; -fx-font-size: 20px;"/>
|
||||
<Label text="Duration" style="-fx-font-weight: bold; -fx-pref-width: 90; -fx-font-size: 20px"/>
|
||||
<Label alignment="CENTER" text="Add to Playlist" style="-fx-font-weight: bold; -fx-pref-width: 150; -fx-font-size: 20px"/>
|
||||
<Label alignment="CENTER" text="Play" style="-fx-font-weight: bold; -fx-pref-width: 120; -fx-font-size: 20px"/>
|
||||
</HBox>
|
||||
|
||||
<Separator/>
|
||||
|
||||
<!--song1-->
|
||||
<HBox spacing="20" alignment="CENTER">
|
||||
<Label text="The Comfort of a Laugh Track" style="-fx-pref-width: 290; -fx-font-size: 20px;"/>
|
||||
<Label text="Roar" style="-fx-pref-width: 150; -fx-font-size: 20px;"/>
|
||||
<Label text="04:52" style="-fx-pref-width: 90; -fx-font-size: 20px;"/>
|
||||
<Button fx:id="add1" text="Add to Playlist" style="-fx-pref-width: 150; -fx-font-size: 19px;" onAction="#addToPlaylist"/>
|
||||
<Button fx:id="playBtn1" text="Play" style="-fx-pref-width: 120; -fx-font-size: 20px;" onAction="#handlePlayAction"/>
|
||||
</HBox>
|
||||
|
||||
<!--song2-->
|
||||
<HBox spacing="20" alignment="CENTER">
|
||||
<Label text="Perfect Day" style="-fx-pref-width: 290; -fx-font-size: 20px;"/>
|
||||
<Label text="Lou Reed" style="-fx-pref-width: 150; -fx-font-size: 20px;"/>
|
||||
<Label text="03:44" style="-fx-pref-width: 90; -fx-font-size: 20px;"/>
|
||||
<Button fx:id="add2" text="Add to Playlist" style="-fx-pref-width: 150; -fx-font-size: 19px;" onAction="#addToPlaylist"/>
|
||||
<Button fx:id="playBtn2" text="Play" style="-fx-pref-width: 120; -fx-font-size: 20px;" onAction="#handlePlayAction"/>
|
||||
</HBox>
|
||||
|
||||
<!--song3-->
|
||||
<HBox spacing="20" alignment="CENTER">
|
||||
<Label text="Wish You Were Here" style="-fx-pref-width: 290; -fx-font-size: 20px;"/>
|
||||
<Label text="Pink Floyd" style="-fx-pref-width: 150; -fx-font-size: 20px;"/>
|
||||
<Label text="05:34" style="-fx-pref-width: 90; -fx-font-size: 20px;"/>
|
||||
<Button fx:id="add3" text="Add to Playlist" style="-fx-pref-width: 150; -fx-font-size: 19px;" onAction="#addToPlaylist"/>
|
||||
<Button fx:id="playBtn3" text="Play" style="-fx-pref-width: 120; -fx-font-size: 20px;" onAction="#handlePlayAction"/>
|
||||
</HBox>
|
||||
|
||||
<!--song4-->
|
||||
<HBox spacing="20" alignment="CENTER">
|
||||
<Label text="No Surprises" style="-fx-pref-width: 290; -fx-font-size: 20px;"/>
|
||||
<Label text="Radiohead" style="-fx-pref-width: 150; -fx-font-size: 20px;"/>
|
||||
<Label text="03:49" style="-fx-pref-width: 90; -fx-font-size: 20px;"/>
|
||||
<Button fx:id="add4" text="Add to Playlist" style="-fx-pref-width: 150; -fx-font-size: 19px;" onAction="#addToPlaylist"/>
|
||||
<Button fx:id="playBtn4" text="Play" style="-fx-pref-width: 120; -fx-font-size: 20px;" onAction="#handlePlayAction"/>
|
||||
</HBox>
|
||||
|
||||
</VBox>
|
||||
|
||||
<Separator/>
|
||||
<HBox style="-fx-border-color: lightBlue; -fx-border-width: 5; -fx-padding: 10; -fx-pref-width: 400;" spacing="10" alignment="CENTER_LEFT">
|
||||
<HBox>
|
||||
<Label text="Now Playing: " style="-fx-font-weight: bold; -fx-font-size: 20px;"/>
|
||||
<Label fx:id="nowPlayingSong" text="-" style="-fx-font-weight: bold; -fx-font-size: 20px;"/>
|
||||
</HBox>
|
||||
|
||||
<HBox alignment="CENTER_RIGHT" HBox.hgrow="ALWAYS">
|
||||
<Button text="Open Playlist" style="-fx-pref-width: 120; -fx-font-size: 15px;" onAction="#openPlaylistWindow"/>
|
||||
</HBox>
|
||||
</HBox>
|
||||
|
||||
<!-- TODO: Add your code here! -->
|
||||
|
||||
</VBox>
|
||||
|
||||
Reference in New Issue
Block a user