Making a music program
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String> playlistView;
|
||||
|
||||
private ArrayList<String> playlist = new ArrayList<>();
|
||||
private ArrayList<Song> 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.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> items;
|
||||
@FXML private ListView<String> itemPlaylist;
|
||||
|
||||
public void setPlaylist(ArrayList<String> items){
|
||||
this.items = items;
|
||||
for(String item : items){
|
||||
itemPlaylist.getItems().add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;}
|
||||
}
|
||||
Reference in New Issue
Block a user