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
MidiSounder - MidiSounderGUI.java

MidiSounderGUI.java

Caricato da: Netarrow
Scarica il programma completo

  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import java.io.*;
  5.  
  6. import java.util.*;
  7.  
  8. import javax.swing.*;
  9.  
  10. /**
  11.  * Classe che rappresenta l'interfaccia grafica.
  12.  * <br>Implementa ActionListener per gestire gli eventi del click, KeyListener per la tastiera e<br>
  13.  * suonare tramite essa le note, e Noteable per permettere l'uso di Tastiera e le altre ciassi.
  14.  * @author netarrow
  15.  * @version 1.2.04
  16.  * @since 1.0.00
  17.  * @see Noteable
  18.  */
  19. public class MidiSounderGUI extends JFrame implements ActionListener, Noteable, KeyListener {
  20.  
  21. private static Properties p;
  22.  
  23. static {
  24. p = new Properties();
  25. FileInputStream fis = null;
  26. try {
  27. fis = new FileInputStream("key.conf");
  28. createDefaultConf(p);
  29. }
  30.  
  31. try {
  32. p.load(fis);
  33. fis.close();
  34. } catch(Exception e) {
  35.  
  36. }
  37.  
  38. }
  39.  
  40.         private JPanel jContentPane = null;
  41.       private JPanel composer = null;
  42.       private JPanel optionPane = null;
  43.         private JPanel jPanel = null;
  44.  
  45.       private JTextArea list = null;
  46.       private JComboBox addingList = null;
  47.       private JTextField during = null;
  48.       private JButton clear = null;
  49.       private JCheckBox canonic = null;
  50.         private JButton noteButton[] = new JButton[24];
  51.       private JButton aggiungiNota = null;
  52.       private JButton suonaSequenza = null;
  53.  
  54.       private String note[] = {"Do", "Do#", "Re", "Re#", "Mi", "Fa", "Fa#", "Sol", "Sol#", "La", "La#", "Si", "Do2", "Do2#", "Re2", "Re2#", "Mi2", "Fa2", "Fa2#", "Sol2", "Sol2#", "La2", "La2#", "Si2"};
  55.      
  56.       private Tastiera t;
  57.       private Sequenza seq;
  58.  
  59. /**
  60.  * Questo metodo crea la configurazione standard in key.conf
  61.  * @param p Properties usata per salvare la configurazione
  62.  */
  63. private static void createDefaultConf(Properties p) {
  64. p.put("q", "DO");
  65. p.put("w", "DOd");
  66. p.put("e", "RE");
  67. p.put("r", "REd");
  68. p.put("t", "MI");
  69. p.put("y", "FA");
  70. p.put("u", "FAd");
  71. p.put("i", "SOL");
  72. p.put("o", "SOLd");
  73. p.put("p", "LA");
  74. p.put("è", "LAd");
  75. p.put("+", "SI");
  76. p.put("a", "DO2");
  77. p.put("s", "DO2d");
  78. p.put("d", "RE2");
  79. p.put("f", "RE2d");
  80. p.put("g", "MI2");
  81. p.put("h", "FA2");
  82. p.put("j", "FA2d");
  83. p.put("k", "SOL2");
  84. p.put("l", "SOL2d");
  85. p.put("ò", "LA2");
  86. p.put("à", "LA2d");
  87. p.put("ù", "SI2");
  88. try {
  89. FileOutputStream fos = new FileOutputStream("key.conf");
  90. p.store(fos, "Configurazione tastiera");
  91. fos.close();
  92. } catch(Exception e) {
  93. //
  94. }
  95. }
  96.  
  97. /**
  98.  * ritorna un pannello che rappresenta la GUI della tastiera
  99.  * @return JPanel
  100.  */
  101.         private JPanel getJPanel() {
  102.                 if (jPanel == null) {
  103.                         jPanel = new JPanel();
  104.                         jPanel.setLayout( new GridLayout(2,11));
  105. for(int i = 0; i < noteButton.length; i++) {
  106.                         jPanel.add(noteButton[i]);
  107.                 }
  108. }
  109.                
  110.                 return jPanel;
  111.         }
  112.  
  113.         /**
  114.        * Questo metodo inizializza i 24 bottoni
  115.        * Prima di questo metodo c'era un metodo per ogni bottone(24).<br>
  116.        * Decisamente scomodo prima.
  117.        * @since 1.2.03
  118.        */
  119.         private void initNoteButton() {
  120. for(int i = 0; i < noteButton.length; i++) {
  121.                         noteButton[i] = new JButton();
  122.                         noteButton[i].setText(note[i]);
  123.                         noteButton[i].setHorizontalAlignment(SwingConstants.CENTER);
  124.                         noteButton[i].addActionListener(this);
  125.                         noteButton[i].addKeyListener(this);
  126. }
  127.         }
  128.        
  129.         /**
  130.          * Naturalmente il metodo main per far partire l'applicazione
  131.        * @param args Non utilizzato, al momento il programma non prende argomenti da riga di comando
  132.          */
  133.                 public static void main(String[] args) {
  134.                         new MidiSounderGUI();
  135.         }
  136.  
  137.       /**
  138.        * Costruttore per creare la finestra, inizializza la Testiara e la Sequenza
  139.        * @see Tastiera
  140.        * @see Sequenza
  141.        */
  142.         public MidiSounderGUI() {
  143.                 super();
  144.                 t = new Tastiera(this);
  145.        try {
  146.             seq = new Sequenza(t);
  147. } catch(Exception ex) {
  148.  
  149. }
  150.             addingList = new JComboBox(note);
  151.                 initialize();//inizializza la GUI
  152.         }
  153.  
  154.         /**
  155.          * Metodo di inizializzazione, setta il titolo, mette i pannelli, crea i bottoni ecc...
  156.          *
  157.          */
  158.         private void initialize() {
  159.                 this.setTitle("MidiSounder - (C) Netarrow");
  160.             initNoteButton();
  161.                 this.setContentPane(getJContentPane());
  162.             addComposer();
  163.                 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  164.             pack();
  165.             this.center();
  166.                 this.setResizable(false);
  167.             this.setVisible(true);
  168.         }
  169.        
  170.         /**
  171.        * Crea e restituisce il pannello principale
  172.        * @return JPanel
  173.        */
  174.         private JPanel getJContentPane() {
  175.                 if(jContentPane == null) {
  176.                         jContentPane = new JPanel();
  177.                         jContentPane.setLayout(new BorderLayout());
  178.                         jContentPane.add(getJPanel(), BorderLayout.CENTER);
  179.                   jContentPane.add(getOptionPane(), BorderLayout.SOUTH);
  180.                 }
  181.                 return jContentPane;
  182.         }
  183.  
  184.       private JPanel getOptionPane() {
  185.             if(optionPane == null) {
  186.             optionPane = new JPanel();                
  187. }
  188.   return optionPane;                
  189.  
  190. }
  191.  
  192.        
  193. /**
  194.  * Serve per mettere al centro la finestra
  195.  *
  196.  */
  197. private void center() {
  198. Toolkit t = Toolkit.getDefaultToolkit();
  199. Dimension d=t.getScreenSize();
  200. Dimension win=getSize();
  201. setLocation(d.width/2-(win.width/2)-1,d.height/2-(win.height/2)-1);
  202.  
  203. }
  204.  
  205. /**
  206.  * Questo pannello crea il pannello e i suoi componenti per poter utilizzare le sequenze
  207.  * @since 1.2.03
  208.  * @return JPanel
  209.  * @see Sequenza
  210.  */
  211. private JPanel getComposer() {
  212. if(composer == null) {
  213. JPanel jp = new JPanel();
  214. jp.setLayout(new BorderLayout());
  215. composer = new JPanel();
  216. list = new JTextArea();
  217. list.setEditable(false);
  218. jp.add(list, BorderLayout.CENTER);
  219. JScrollPane jsp = new JScrollPane(jp, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  220. composer.setLayout(new BorderLayout());
  221. composer.add(jsp, BorderLayout.SOUTH);
  222. aggiungiNota = new JButton("Aggiungi Nota");
  223. suonaSequenza = new JButton("Play");
  224. aggiungiNota.addActionListener(this);
  225. suonaSequenza.addActionListener(this);
  226. composer.add(aggiungiNota, BorderLayout.WEST);
  227. composer.add(suonaSequenza, BorderLayout.EAST);
  228. }
  229. return composer;
  230. }
  231.  
  232. /**
  233.  * Aggiunge il pannello composer per le sequenze
  234.  * @see Sequenze
  235.  */
  236. private void addComposer() {
  237. optionPane.add(getComposer(), BorderLayout.SOUTH);
  238. optionPane.add(addingList, BorderLayout.WEST);
  239. optionPane.add(new JLabel("Durata"), BorderLayout.CENTER);
  240. JPanel jp = new JPanel();
  241. during = new JTextField(10);
  242. clear = new JButton("Clear");
  243. clear.addActionListener(this);
  244. canonic = new JCheckBox("Canonico");
  245. jp.setLayout(new BorderLayout());
  246. jp.add(during, BorderLayout.WEST);
  247. jp.add(clear, BorderLayout.CENTER);
  248. jp.add(canonic, BorderLayout.EAST);
  249. optionPane.add(jp, BorderLayout.EAST);
  250. center();
  251. }
  252.  
  253.  
  254. /**
  255.  * Gestisce gli eventi<br>
  256.  * Controlla se è stato premuto aggiungiNota, se si aggiunge la nota alla sequenza
  257.  * <bt> Se viene premuto suonaSequenze viene riprodotta
  258.  * <br> sennò è stato premuto un tasto-nota, viene convertito da formato nota standard(Do, Do#, Mi2, La2#) in <br>
  259.  * formato nota-variabile(DO, DOd, MI2, LA2d)<br>
  260.  * @param e l'ActionEvent per gestire l'oggetto sorgente che ha scatenato l'evento
  261.  */
  262. public void actionPerformed(ActionEvent e) {
  263. if(e.getSource().equals(clear)) {
  264. seq.clear();
  265. list.setText("");
  266. } else
  267. if(e.getSource().equals(aggiungiNota)) {
  268. String nota = addingList.getSelectedItem().toString();
  269. int idurata = 500;
  270. try {
  271. idurata = Integer.parseInt(during.getText());
  272. } catch(Exception ex) {
  273.  
  274. }
  275. seq.addNota(new Nota(nota, idurata));
  276. list.append(nota + " - " + idurata + "\n");
  277. } else if(e.getSource().equals(suonaSequenza)) {
  278. try {
  279. seq.suonaSeq(canonic.isSelected());
  280. } catch(Exception exc) {
  281. }
  282. }else {
  283.         t.suonaNota(e.getActionCommand().toUpperCase().replace('#', 'd'), false);
  284. }
  285. }
  286.  
  287. /**
  288.  * Not used
  289.  */
  290. public void keyTyped(KeyEvent e) {
  291.         //NON FA NIENTE
  292. }
  293.  
  294. /**
  295.  * Metodo per intercettare i tasti premuti e suonare i midi da testiera, viene chiamato getName<br>
  296.  * e se il ritorno non è null è stata trovata una nota associata a quel tasto
  297.  *
  298.  */
  299. public void keyPressed(KeyEvent e) {
  300. if(getName(e.getKeyChar()) == null) {
  301. e.consume();
  302. }
  303. else {
  304.         t.suonaNota(getName(e.getKeyChar()), false);
  305. }
  306. }
  307.  
  308. /**
  309.  * Not used
  310.  */
  311. public void keyReleased(KeyEvent e) {
  312.         //NON FA NIENTE
  313. }
  314.  
  315. /**
  316.  * Cerca nel file key.conf una nota associata a quel tasto, se viene trovata ritorna la nota, sennà null
  317.  * @param code Codice del tasto premuto
  318.  * @return String il nome della nota in stringa, Null se non si trova un'associazione
  319.  */
  320. private String getName(char code) {
  321.         String tmp = "" + code;
  322.         tmp.toLowerCase();
  323.       return p.getProperty(tmp);
  324.        
  325. }
  326. }