/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* JInstruments.java
*
* Created on 7-dic-2010, 19.16.35
*/
package jinstruments.graphics;
import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import javax.sound.midi.Instrument;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import jinstruments.InstrumentRoom;
import jinstruments.Notes;
/**
*
* @author Paolo
*/
public class JInstruments
extends javax.
swing.
JFrame {
private InstrumentRoom room;
private Notes playNote;
@Override
try {
switch (e.getKeyChar()) {
case 'a':
playNote.playNote(60, selectendInstrument, reverber.isSelected());
break;
case 'w':
playNote.playNote(61, selectendInstrument, reverber.isSelected());
break;
case 's':
playNote.playNote(62, selectendInstrument, reverber.isSelected());
break;
case 'e':
playNote.playNote(63, selectendInstrument, reverber.isSelected());
break;
case 'd':
playNote.playNote(64, selectendInstrument, reverber.isSelected());
break;
case 'f':
playNote.playNote(65, selectendInstrument, reverber.isSelected());
break;
case 't':
playNote.playNote(66, selectendInstrument, reverber.isSelected());
break;
case 'g':
playNote.playNote(67, selectendInstrument, reverber.isSelected());
break;
case 'y':
playNote.playNote(68, selectendInstrument, reverber.isSelected());
break;
case 'h':
playNote.playNote(69, selectendInstrument, reverber.isSelected());
break;
case 'j':
playNote.playNote(70, selectendInstrument, reverber.isSelected());
break;
case 'k':
playNote.playNote(71, selectendInstrument, reverber.isSelected());
break;
}
ex.printStackTrace();
}
}
}
private int id;
this.id = pId;
}
try {
playNote.playNote(60 + this.id, selectendInstrument, reverber.isSelected());
ex.printStackTrace();
}
}
}
private int id;
public Mouse(int pId) {
this.id = pId;
}
@Override
if (soundOnPass.isSelected()) {
try {
playNote.playNote(60 + this.id, selectendInstrument, reverber.isSelected());
ex.printStackTrace();
}
}
}
}
/** Creates new form JInstruments */
public JInstruments() {
try {
UIManager.
setLookAndFeel(new NimbusLookAndFeel
());
this.
setIconImage(Toolkit.
getDefaultToolkit().
getImage(getClass
().
getResource("/jinstruments/graphics/images/icon.png")));
initComponents();
this.setFocusable(true);
this.requestFocus();
this.
addKeyListener(new Key());
this.room = new InstrumentRoom();
this.playNote = new Notes();
this.initNotes();
this.initInstruments();
this.selectInstrument();
JOptionPane.
showMessageDialog(this,
"Errore nel caricamento degli strumenti.\nL'applicazione verrà chiusa.");
}
}
private void initNotes() {
for (int i = 0; i < this.notes.length; i++) {
this.
notes[i
] = new JButton(Notes.
NOTES[i
]);
this.
notes[i
].
addActionListener(new Action(i
));
this.notes[i].addMouseListener(new Mouse(i));
this.notes[i].setFocusable(false);
this.notePanel.add(this.notes[i]);
}
}
private void initInstruments() {
String[] instrumentsName
= this.
room.
getListOfInstruments();
for (String instrument
: instrumentsName
) {
this.instruments.addItem(instrument);
}
}
private void selectInstrument() {
this.selectendInstrument = this.room.getInstrument(this.instruments.getSelectedIndex());
}
/** 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
panel
= new javax.
swing.
JPanel();
selectLabel
= new javax.
swing.
JLabel();
notePanel
= new javax.
swing.
JPanel();
helpLabel
= new javax.
swing.
JLabel();
setTitle("JInstruments - by Paolo Ricciuti (C)");
setResizable(false);
public void windowClosing
(java.
awt.
event.
WindowEvent evt
) {
formWindowClosing(evt);
}
});
panel.setFocusable(false);
selectLabel.setText("Seleziona strumento:");
selectLabel.setFocusable(false);
instruments.setFocusable(false);
}
public void popupMenuWillBecomeInvisible
(javax.
swing.
event.
PopupMenuEvent evt
) {
instrumentsPopupMenuWillBecomeInvisible(evt);
}
public void popupMenuWillBecomeVisible
(javax.
swing.
event.
PopupMenuEvent evt
) {
}
});
notePanel.setFocusable(false);
notePanel.
setLayout(new java.
awt.
GridLayout(2, 6
));
soundOnPass.setText("Suona al passaggio");
soundOnPass.setFocusable(false);
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>");
reverber.setSelected(true);
reverber.setText("Riverbero");
reverber.setFocusable(false);
stop.
setIcon(new javax.
swing.
ImageIcon(getClass
().
getResource("/jinstruments/graphics/images/stop.png"))); // NOI18N
stop.setText("Stop");
stop.setFocusable(false);
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) {
stopActionPerformed(evt);
}
});
javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
panel.setLayout(panelLayout);
panelLayout.setHorizontalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(selectLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(instruments, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
.
addContainerGap(146,
Short.
MAX_VALUE))
.
addComponent(notePanel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, 400,
Short.
MAX_VALUE)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(soundOnPass)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(reverber)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED, 95,
Short.
MAX_VALUE)
.addComponent(stop)
.addContainerGap())
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.
addComponent(helpLabel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, 380,
Short.
MAX_VALUE)
.addContainerGap())
);
panelLayout.setVerticalGroup(
panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(selectLabel)
.addComponent(instruments, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(notePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(soundOnPass)
.addComponent(reverber)
.addComponent(stop))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.
addComponent(helpLabel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, 130,
Short.
MAX_VALUE)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(panel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addComponent(panel, javax.
swing.
GroupLayout.
DEFAULT_SIZE, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE)
);
java.
awt.
Dimension screenSize
= java.
awt.
Toolkit.
getDefaultToolkit().
getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-365)/2, 416, 365);
}// </editor-fold>//GEN-END:initComponents
private void instrumentsPopupMenuWillBecomeInvisible
(javax.
swing.
event.
PopupMenuEvent evt
) {//GEN-FIRST:event_instrumentsPopupMenuWillBecomeInvisible
this.selectInstrument();
}//GEN-LAST:event_instrumentsPopupMenuWillBecomeInvisible
private void stopActionPerformed
(java.
awt.
event.
ActionEvent evt
) {//GEN-FIRST:event_stopActionPerformed
this.playNote.stopSound();
}//GEN-LAST:event_stopActionPerformed
private void formWindowClosing
(java.
awt.
event.
WindowEvent evt
) {//GEN-FIRST:event_formWindowClosing
try {
int ret
= JOptionPane.
showConfirmDialog(this,
"Salvare la sequenza?");
int retval = choose.showSaveDialog(this);
File selected
=choose.
getSelectedFile();
if(!selected.getAbsolutePath().endsWith(".mid")){
selected
=new File(selected.
getAbsolutePath()+".mid");
}
this.playNote.saveFile(selected);
}
}
}
}//GEN-LAST:event_formWindowClosing
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) {
public void run() {
new JInstruments().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.
swing.
JLabel helpLabel
;
private javax.
swing.
JPanel notePanel
;
private javax.
swing.
JPanel panel
;
private javax.
swing.
JLabel selectLabel
;
// End of variables declaration//GEN-END:variables
}