diff --git a/lib/jl1.0.1.jar b/lib/jl1.0.1.jar new file mode 100644 index 0000000..bd5fb8b Binary files /dev/null and b/lib/jl1.0.1.jar differ diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..1574df4 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/src/task_5/Application.java b/src/task_5/Application.java new file mode 100644 index 0000000..5245470 --- /dev/null +++ b/src/task_5/Application.java @@ -0,0 +1,103 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package task_5; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.ArrayList; + + +/** + * + * @author lailis + */ +public class Application { + private ArrayList musicList; + private ThreadPlayer thread; + private int nowPlaying; + + + public Application(){ + musicList = new ArrayList<>(); + } + + public void addMusic(String path){ + musicList.add(path); + } + + public String[] getMusicList(){ + return (String[]) musicList.toArray(new String[0]); + } + + public String getNowPlayed(){ + return musicList.get(nowPlaying); + } + + public void removeMusic(int i){ + musicList.remove(i); + } + + public void play(int i){ + nowPlaying = i; + String filepath = musicList.get(i); + thread = new ThreadPlayer(filepath); + thread.start(); + } + + public void stop(){ + if(thread != null){ + thread.stop(); + } + } + + public void next(){ + if(nowPlaying < musicList.size() - 1){ + stop(); + play(nowPlaying + 1); + } + } + + public void prev(){ + if(nowPlaying != 0){ + stop(); + play(nowPlaying - 1); + } + } + + private static class Player { + + public Player() { + } + + private Player(FileInputStream fis) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + private void play() { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + } + + private class ThreadPlayer extends Thread{ + private String filepath; + private Player player; + + public ThreadPlayer(String filepath){ + this.filepath = filepath; + } + + @Override + public void run(){ + try{ + FileInputStream fis = new FileInputStream(filepath); + player = new Player(fis); + player.play(); + }catch(FileNotFoundException e){ + throw new IllegalStateException("File " + filepath + " not found"); + } + } + } +} diff --git a/src/task_5/Controller.java b/src/task_5/Controller.java new file mode 100644 index 0000000..5a8213b --- /dev/null +++ b/src/task_5/Controller.java @@ -0,0 +1,73 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package task_5; + +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; +import javax.swing.JOptionPane; +import javax.swing.JFileChooser; +import javax.swing.filechooser.FileNameExtensionFilter; +/** + * + * @author lailis + */ +public class Controller implements ActionListener{ + private Application model; + private View view; + + public Controller(){ + model = new Application(); + view = new View(); + view.setVisible(true); + view.addActionListener(this); + view.setListMusic(model.getMusicList()); + } + + @Override + public void actionPerformed(ActionEvent ae) { + Object click = ae.getSource(); + + try{ + if(click.equals(view.getButtonAdd())){ + JFileChooser fc = new JFileChooser(); + FileNameExtensionFilter filter = new FileNameExtensionFilter("mp3 Files", "mp3", "mp3"); + fc.setFileFilter(filter); + int returnVal = fc.showOpenDialog(view); + + if(returnVal == JFileChooser.APPROVE_OPTION){ + String path = fc.getSelectedFile().getAbsolutePath(); + model.addMusic(path); + view.setListMusic(model.getMusicList()); + } + } + else if(click.equals(view.getButtonDelete())){ + int selected = view.getSelectedMusic(); + model.removeMusic(selected); + view.setListMusic(model.getMusicList()); + } + else if(click.equals(view.getButtonNext())){ + model.next(); + view.setFieldPlaying(model.getNowPlayed()); + } + else if(click.equals(view.getButtonPlay())){ + int selected = view.getSelectedMusic(); + model.stop(); + model.play(selected); + view.setFieldPlaying(model.getNowPlayed()); + } + else if(click.equals(view.getButtonPrev())){ + model.prev(); + view.setFieldPlaying(model.getNowPlayed()); + } + else if(click.equals(view.getButtonStop())){ + model.stop(); + view.setFieldPlaying(""); + } + }catch(Exception e){ + JOptionPane.showMessageDialog(view, e.getMessage()); + } + } +} diff --git a/src/task_5/Driver.java b/src/task_5/Driver.java new file mode 100644 index 0000000..7682ae4 --- /dev/null +++ b/src/task_5/Driver.java @@ -0,0 +1,21 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package task_5; + +/** + * + * @author lailis + */ +public class Driver { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + Controller c = new Controller(); + } + +} diff --git a/src/task_5/View.form b/src/task_5/View.form new file mode 100644 index 0000000..9f3a867 --- /dev/null +++ b/src/task_5/View.form @@ -0,0 +1,163 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/task_5/View.java b/src/task_5/View.java new file mode 100644 index 0000000..0f47c31 --- /dev/null +++ b/src/task_5/View.java @@ -0,0 +1,227 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package task_5; + +import java.awt.event.ActionListener; +import javax.swing.JButton; + +/** + * + * lailis + */ +public class View extends javax.swing.JFrame { + + /** + * Creates new form View + */ + public View() { + initComponents(); + setLocationRelativeTo(null); + } + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + jScrollPane2 = new javax.swing.JScrollPane(); + jScrollPane1 = new javax.swing.JScrollPane(); + jList = new javax.swing.JList<>(); + jLabel1 = new javax.swing.JLabel(); + jTextFieldNP = new javax.swing.JTextField(); + jButtonAdd = new javax.swing.JButton(); + jButtonDelete = new javax.swing.JButton(); + jButtonPrev = new javax.swing.JButton(); + jButtonPlay = new javax.swing.JButton(); + jButtonStop = new javax.swing.JButton(); + jButtonNext = new javax.swing.JButton(); + jLabel2 = new javax.swing.JLabel(); + + setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); + setResizable(false); + + jScrollPane1.setViewportView(jList); + + jLabel1.setFont(new java.awt.Font("Segoe UI Light", 0, 12)); // NOI18N + jLabel1.setText("Now Playing"); + + jTextFieldNP.setEditable(false); + + jButtonAdd.setText("Add"); + + jButtonDelete.setText("Delete"); + + jButtonPrev.setText("<<"); + + jButtonPlay.setText("Play"); + + jButtonStop.setText("Stop"); + + jButtonNext.setText(">>"); + + jLabel2.setFont(new java.awt.Font("Segoe UI Light", 0, 24)); // NOI18N + jLabel2.setText("Simple Music Player"); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel2) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) + .addComponent(jScrollPane1) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() + .addComponent(jLabel1) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) + .addComponent(jTextFieldNP)) + .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup() + .addComponent(jButtonAdd) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButtonDelete) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButtonPrev) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButtonPlay) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButtonStop) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jButtonNext))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addComponent(jLabel2) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE) + .addGap(18, 18, 18) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jLabel1) + .addComponent(jTextFieldNP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(jButtonAdd) + .addComponent(jButtonDelete) + .addComponent(jButtonPrev) + .addComponent(jButtonPlay) + .addComponent(jButtonStop) + .addComponent(jButtonNext)) + .addContainerGap(24, Short.MAX_VALUE)) + ); + + pack(); + }// //GEN-END:initComponents + + /** + * @param args the command line arguments + */ + public static void main(String args[]) { + /* Set the Nimbus look and feel */ + // + /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. + * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html + */ + try { + for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + javax.swing.UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (ClassNotFoundException ex) { + java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (InstantiationException ex) { + java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (IllegalAccessException ex) { + java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } catch (javax.swing.UnsupportedLookAndFeelException ex) { + java.util.logging.Logger.getLogger(View.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); + } + // + + /* Create and display the form */ + java.awt.EventQueue.invokeLater(new Runnable() { + public void run() { + new View().setVisible(true); + } + }); + } + + public void addActionListener(ActionListener a){ + jButtonAdd.addActionListener(a); + jButtonDelete.addActionListener(a); + jButtonNext.addActionListener(a); + jButtonPlay.addActionListener(a); + jButtonPrev.addActionListener(a); + jButtonStop.addActionListener(a); + } + + public JButton getButtonAdd(){ + return jButtonAdd; + } + + public JButton getButtonDelete(){ + return jButtonDelete; + } + + public JButton getButtonNext(){ + return jButtonNext; + } + + public JButton getButtonPlay(){ + return jButtonPlay; + } + + public JButton getButtonPrev(){ + return jButtonPrev; + } + + public JButton getButtonStop(){ + return jButtonStop; + } + + public void setListMusic(String[] musicList){ + jList.setListData(musicList); + } + + public void setFieldPlaying(String filename){ + jTextFieldNP.setText(filename); + } + + public int getSelectedMusic(){ + if(jList.getSelectedIndex() == -1){ + throw new IllegalStateException("Please select any music"); + } + return jList.getSelectedIndex(); + } + + + + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton jButtonAdd; + private javax.swing.JButton jButtonDelete; + private javax.swing.JButton jButtonNext; + private javax.swing.JButton jButtonPlay; + private javax.swing.JButton jButtonPrev; + private javax.swing.JButton jButtonStop; + private javax.swing.JLabel jLabel1; + private javax.swing.JLabel jLabel2; + private javax.swing.JList jList; + private javax.swing.JScrollPane jScrollPane1; + private javax.swing.JScrollPane jScrollPane2; + private javax.swing.JTextField jTextFieldNP; + // End of variables declaration//GEN-END:variables +}