/** * NoteChoice.java * * This is a user interface for selecting notes. It presents choices for the (human) * concepts of letter-name and octave but has methods to set and get this in terms * of the (MIDI) concept of a note number. * * The two choice-boxes can be layed out vertically or horizontally in the constructor. * * Andy Murray 18/12/02 * */ import java.awt.*; public class NoteChoice extends Panel { static final int LAYOUT_HORIZ=0; static final int LAYOUT_VERT=1; private Label lblNote, lblOct; private Choice chNote, chOct; NoteChoice(int LAYOUT) { lblNote=new Label("Note:", Label.RIGHT); lblOct=new Label("Oct:", Label.RIGHT); chNote=new Choice(); chNote.add("B"); chNote.add("A#"); chNote.add("A"); chNote.add("G#"); chNote.add("G"); chNote.add("F#"); chNote.add("F"); chNote.add("E"); chNote.add("D#"); chNote.add("D"); chNote.add("C#"); chNote.add("C"); chNote.select("C"); chOct=new Choice(); chOct.add("+3");//96 chOct.add("+2");//84 chOct.add("+1");//72 chOct.add("0");//60 chOct.add("-1");//48 chOct.add("-2");//36 chOct.add("-3");//24 chOct.select("0"); switch(LAYOUT) { case LAYOUT_HORIZ: this.setLayout(new GridLayout(1,4)); break; case LAYOUT_VERT: this.setLayout(new GridLayout(2,2)); } this.add(lblNote); this.add(chNote); this.add(lblOct); this.add(chOct); this.setBackground(Color.lightGray); } public int getNoteNum() { int noteVal=11-chNote.getSelectedIndex(); int octVal=(3-chOct.getSelectedIndex()+5)*12; return noteVal+octVal; } public void setNoteNum(int val) { if ( (val>23)&&(val<108) ) { int octIndex = 6-((val/12)-2); chOct.select(octIndex); int letterIndex = 11-(val % 12); chNote.select(letterIndex); } } }