Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JInstruments - JInstruments.java

JInstruments.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JInstruments.java
  8.  *
  9.  * Created on 7-dic-2010, 19.16.35
  10.  */
  11. package jinstruments.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.awt.Toolkit;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import java.awt.event.KeyAdapter;
  18. import java.awt.event.KeyEvent;
  19. import java.awt.event.MouseAdapter;
  20. import java.awt.event.MouseEvent;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import javax.sound.midi.Instrument;
  24. import javax.sound.midi.InvalidMidiDataException;
  25. import javax.sound.midi.MidiUnavailableException;
  26. import javax.swing.JButton;
  27. import javax.swing.JFileChooser;
  28. import javax.swing.JOptionPane;
  29. import javax.swing.UIManager;
  30. import javax.swing.UnsupportedLookAndFeelException;
  31. import jinstruments.InstrumentRoom;
  32. import jinstruments.Notes;
  33.  
  34. /**
  35.  *
  36.  * @author Paolo
  37.  */
  38. public class JInstruments extends javax.swing.JFrame {
  39.  
  40.     private InstrumentRoom room;
  41.     private Instrument selectendInstrument;
  42.     private Notes playNote;
  43.     private JButton[] notes;
  44.  
  45.     private class Key extends KeyAdapter {
  46.  
  47.         @Override
  48.         public void keyPressed(KeyEvent e) {
  49.             try {
  50.                 switch (e.getKeyChar()) {
  51.                     case 'a':
  52.                         playNote.playNote(60, selectendInstrument, reverber.isSelected());
  53.                         break;
  54.                     case 'w':
  55.                         playNote.playNote(61, selectendInstrument, reverber.isSelected());
  56.                         break;
  57.                     case 's':
  58.                         playNote.playNote(62, selectendInstrument, reverber.isSelected());
  59.                         break;
  60.                     case 'e':
  61.                         playNote.playNote(63, selectendInstrument, reverber.isSelected());
  62.                         break;
  63.                     case 'd':
  64.                         playNote.playNote(64, selectendInstrument, reverber.isSelected());
  65.                         break;
  66.                     case 'f':
  67.                         playNote.playNote(65, selectendInstrument, reverber.isSelected());
  68.                         break;
  69.                     case 't':
  70.                         playNote.playNote(66, selectendInstrument, reverber.isSelected());
  71.                         break;
  72.                     case 'g':
  73.                         playNote.playNote(67, selectendInstrument, reverber.isSelected());
  74.                         break;
  75.                     case 'y':
  76.                         playNote.playNote(68, selectendInstrument, reverber.isSelected());
  77.                         break;
  78.                     case 'h':
  79.                         playNote.playNote(69, selectendInstrument, reverber.isSelected());
  80.                         break;
  81.                     case 'j':
  82.                         playNote.playNote(70, selectendInstrument, reverber.isSelected());
  83.                         break;
  84.                     case 'k':
  85.                         playNote.playNote(71, selectendInstrument, reverber.isSelected());
  86.                         break;
  87.                 }
  88.             } catch (InvalidMidiDataException ex) {
  89.                 ex.printStackTrace();
  90.             }
  91.         }
  92.     }
  93.  
  94.     private class Action implements ActionListener {
  95.  
  96.         private int id;
  97.  
  98.         public Action(int pId) {
  99.             this.id = pId;
  100.         }
  101.  
  102.         public void actionPerformed(ActionEvent e) {
  103.             try {
  104.                 playNote.playNote(60 + this.id, selectendInstrument, reverber.isSelected());
  105.             } catch (InvalidMidiDataException ex) {
  106.                 ex.printStackTrace();
  107.             }
  108.         }
  109.     }
  110.  
  111.     private class Mouse extends MouseAdapter {
  112.  
  113.         private int id;
  114.  
  115.         public Mouse(int pId) {
  116.             this.id = pId;
  117.         }
  118.  
  119.         @Override
  120.         public void mouseEntered(MouseEvent e) {
  121.             if (soundOnPass.isSelected()) {
  122.                 try {
  123.                     playNote.playNote(60 + this.id, selectendInstrument, reverber.isSelected());
  124.                 } catch (InvalidMidiDataException ex) {
  125.                     ex.printStackTrace();
  126.                 }
  127.             }
  128.         }
  129.     }
  130.  
  131.     /** Creates new form JInstruments */
  132.     public JInstruments() {
  133.         try {
  134.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  135.             this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/jinstruments/graphics/images/icon.png")));
  136.             initComponents();
  137.             this.setFocusable(true);
  138.             this.requestFocus();
  139.             this.addKeyListener(new Key());
  140.             this.room = new InstrumentRoom();
  141.             this.playNote = new Notes();
  142.             this.initNotes();
  143.             this.initInstruments();
  144.             this.selectInstrument();
  145.         } catch (MidiUnavailableException ex) {
  146.             JOptionPane.showMessageDialog(this, "Errore nel caricamento degli strumenti.\nL'applicazione verrà chiusa.");
  147.             System.exit(0);
  148.         } catch (UnsupportedLookAndFeelException ex) {
  149.         } catch (InvalidMidiDataException ex) {
  150.         }
  151.     }
  152.  
  153.     private void initNotes() {
  154.         this.notes = new JButton[12];
  155.         for (int i = 0; i < this.notes.length; i++) {
  156.             this.notes[i] = new JButton(Notes.NOTES[i]);
  157.             this.notes[i].addActionListener(new Action(i));
  158.             this.notes[i].addMouseListener(new Mouse(i));
  159.             this.notes[i].setFocusable(false);
  160.             this.notePanel.add(this.notes[i]);
  161.         }
  162.     }
  163.  
  164.     private void initInstruments() {
  165.         String[] instrumentsName = this.room.getListOfInstruments();
  166.         for (String instrument : instrumentsName) {
  167.             this.instruments.addItem(instrument);
  168.         }
  169.     }
  170.  
  171.     private void selectInstrument() {
  172.         this.selectendInstrument = this.room.getInstrument(this.instruments.getSelectedIndex());
  173.     }
  174.  
  175.     /** This method is called from within the constructor to
  176.      * initialize the form.
  177.      * WARNING: Do NOT modify this code. The content of this method is
  178.      * always regenerated by the Form Editor.
  179.      */
  180.     @SuppressWarnings("unchecked")
  181.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  182.     private void initComponents() {
  183.  
  184.         panel = new javax.swing.JPanel();
  185.         selectLabel = new javax.swing.JLabel();
  186.         instruments = new javax.swing.JComboBox();
  187.         notePanel = new javax.swing.JPanel();
  188.         soundOnPass = new javax.swing.JToggleButton();
  189.         helpLabel = new javax.swing.JLabel();
  190.         reverber = new javax.swing.JCheckBox();
  191.         stop = new javax.swing.JButton();
  192.  
  193.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  194.         setTitle("JInstruments - by Paolo Ricciuti (C)");
  195.         setResizable(false);
  196.         addWindowListener(new java.awt.event.WindowAdapter() {
  197.             public void windowClosing(java.awt.event.WindowEvent evt) {
  198.                 formWindowClosing(evt);
  199.             }
  200.         });
  201.  
  202.         panel.setFocusable(false);
  203.  
  204.         selectLabel.setText("Seleziona strumento:");
  205.         selectLabel.setFocusable(false);
  206.  
  207.         instruments.setFocusable(false);
  208.         instruments.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
  209.             public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
  210.             }
  211.             public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
  212.                 instrumentsPopupMenuWillBecomeInvisible(evt);
  213.             }
  214.             public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
  215.             }
  216.         });
  217.  
  218.         notePanel.setFocusable(false);
  219.         notePanel.setLayout(new java.awt.GridLayout(2, 6));
  220.  
  221.         soundOnPass.setText("Suona al passaggio");
  222.         soundOnPass.setFocusable(false);
  223.  
  224.         helpLabel.setText("<html>Per utilizzare questo programma basta selezionare lo strumento desiderato e successivamente utilizzare una delle tre modalità per suonare: è possibile premere sui bottoni per suonare la nota scritta sul pulsante. In alternativa (premendo prima sul tasto Suona al passaggio) all'entrata del mouse su ogni pulsante suonerà la nota corrispondente. Infine è possibile utilizzare la tastiera come se fosse la tastiera di un piano con i tasti a,w,s,e,d,f,t,g,y,h,u,j,k. Buona composizione. Deselezionando l'opzione riverbero quando partirà una nota verrà fermata l'altra.</html>");
  225.  
  226.         reverber.setSelected(true);
  227.         reverber.setText("Riverbero");
  228.         reverber.setFocusable(false);
  229.  
  230.         stop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jinstruments/graphics/images/stop.png"))); // NOI18N
  231.         stop.setText("Stop");
  232.         stop.setFocusable(false);
  233.         stop.addActionListener(new java.awt.event.ActionListener() {
  234.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  235.                 stopActionPerformed(evt);
  236.             }
  237.         });
  238.  
  239.         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
  240.         panel.setLayout(panelLayout);
  241.         panelLayout.setHorizontalGroup(
  242.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  243.             .addGroup(panelLayout.createSequentialGroup()
  244.                 .addContainerGap()
  245.                 .addComponent(selectLabel)
  246.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  247.                 .addComponent(instruments, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
  248.                 .addContainerGap(146, Short.MAX_VALUE))
  249.             .addComponent(notePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
  250.             .addGroup(panelLayout.createSequentialGroup()
  251.                 .addContainerGap()
  252.                 .addComponent(soundOnPass)
  253.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  254.                 .addComponent(reverber)
  255.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
  256.                 .addComponent(stop)
  257.                 .addContainerGap())
  258.             .addGroup(panelLayout.createSequentialGroup()
  259.                 .addContainerGap()
  260.                 .addComponent(helpLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
  261.                 .addContainerGap())
  262.         );
  263.         panelLayout.setVerticalGroup(
  264.             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  265.             .addGroup(panelLayout.createSequentialGroup()
  266.                 .addContainerGap()
  267.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  268.                     .addComponent(selectLabel)
  269.                     .addComponent(instruments, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  270.                 .addGap(18, 18, 18)
  271.                 .addComponent(notePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
  272.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  273.                 .addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  274.                     .addComponent(soundOnPass)
  275.                     .addComponent(reverber)
  276.                     .addComponent(stop))
  277.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  278.                 .addComponent(helpLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
  279.                 .addContainerGap())
  280.         );
  281.  
  282.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  283.         getContentPane().setLayout(layout);
  284.         layout.setHorizontalGroup(
  285.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  286.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  287.         );
  288.         layout.setVerticalGroup(
  289.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  290.             .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  291.         );
  292.  
  293.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  294.         setBounds((screenSize.width-416)/2, (screenSize.height-365)/2, 416, 365);
  295.     }// </editor-fold>//GEN-END:initComponents
  296.  
  297.     private void instrumentsPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_instrumentsPopupMenuWillBecomeInvisible
  298.         this.selectInstrument();
  299.     }//GEN-LAST:event_instrumentsPopupMenuWillBecomeInvisible
  300.  
  301.     private void stopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopActionPerformed
  302.         this.playNote.stopSound();
  303.     }//GEN-LAST:event_stopActionPerformed
  304.  
  305.     private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
  306.         try {
  307.             int ret = JOptionPane.showConfirmDialog(this, "Salvare la sequenza?");
  308.             if (ret == JOptionPane.OK_OPTION) {
  309.                 JFileChooser choose = new JFileChooser();
  310.                 int retval = choose.showSaveDialog(this);
  311.                 if (retval == JFileChooser.APPROVE_OPTION) {
  312.                     File selected=choose.getSelectedFile();
  313.                     if(!selected.getAbsolutePath().endsWith(".mid")){
  314.                         selected=new File(selected.getAbsolutePath()+".mid");
  315.                     }
  316.                     this.playNote.saveFile(selected);
  317.                 }
  318.             }
  319.         } catch (Exception ex) {
  320.         }
  321.     }//GEN-LAST:event_formWindowClosing
  322.  
  323.     /**
  324.      * @param args the command line arguments
  325.      */
  326.     public static void main(String args[]) {
  327.         java.awt.EventQueue.invokeLater(new Runnable() {
  328.  
  329.             public void run() {
  330.                 new JInstruments().setVisible(true);
  331.             }
  332.         });
  333.     }
  334.     // Variables declaration - do not modify//GEN-BEGIN:variables
  335.     private javax.swing.JLabel helpLabel;
  336.     private javax.swing.JComboBox instruments;
  337.     private javax.swing.JPanel notePanel;
  338.     private javax.swing.JPanel panel;
  339.     private javax.swing.JCheckBox reverber;
  340.     private javax.swing.JLabel selectLabel;
  341.     private javax.swing.JToggleButton soundOnPass;
  342.     private javax.swing.JButton stop;
  343.     // End of variables declaration//GEN-END:variables
  344. }