3 import java.awt.event.*;
6 * Graphical representation of a MIDI note.
8 * @author Andersson, Andreen, Lanneskog, Pehrson
12 public class MooNoteElement extends JPanel {
14 private MooTrackView mtv;
17 private boolean selected;
18 private Rectangle pitchRect, veloRect;
19 public static final Color bgColor = new Color(160, 218, 255);
20 private String notePitch;
21 private String noteVelocity;
22 private JPopupMenu popup;
23 private JMenuItem popupRemove, popupProp;
26 * Creates a new note element.
27 * @param mn the note that will be graphically represented
28 * @param rows the number of rows that the note will occupy
30 public MooNoteElement (MooTrackView parent, MooNote mn) {
34 columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
35 setBorder(BorderFactory.createLineBorder(Color.black));
36 setBackground(bgColor);
37 addMouseListener(new MAdapter());
39 // Defines coordinates.
40 pitchRect = new Rectangle(0, 0, 15, 10);
41 veloRect = new Rectangle(20, 0, 40, 10);
43 // Creates pop-up menu.
44 popup = new JPopupMenu();
45 PopupListener pList = new PopupListener();
46 popupProp = new JMenuItem("Preferences...");
47 popupProp.addActionListener(pList);
50 popupRemove = new JMenuItem("Remove");
51 popupRemove.addActionListener(pList);
52 popup.add(popupRemove);
57 * Returns true if the current NoteElement is selected, otherwise false.
58 * @return if the element is selected
60 public boolean isSelected() {
65 * Selects the current NoteElement.
66 * @param state if the element should be selected
68 public void setSelected(boolean state) {
75 public void paintComponent(Graphics g)
77 super.paintComponent(g);
78 if (!(g instanceof Graphics2D)) return;
79 Graphics2D g2 = (Graphics2D)g;
80 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
89 g2.drawString(notePitch, 1, 9);
90 g2.drawString("" + noteVelocity, 21, 9);
93 protected void calculateString(){
97 if(note == null) return;
99 int pitch = note.getPitch();
100 switch (pitch % 12) {
101 case 0: notePitch = "C"; break;
102 case 1: notePitch = "C#"; break;
103 case 2: notePitch = "D"; break;
104 case 3: notePitch = "D#"; break;
105 case 4: notePitch = "E"; break;
106 case 5: notePitch = "F"; break;
107 case 6: notePitch = "F#"; break;
108 case 7: notePitch = "G"; break;
109 case 8: notePitch = "G#"; break;
110 case 9: notePitch = "A"; break;
111 case 10: notePitch = "A#"; break;
112 case 11: notePitch = "B"; break;
114 notePitch += pitch / 12;
115 noteVelocity = ""+note.getVelocity();
118 public MooNote getNote(){
122 class MAdapter extends MouseAdapter {
123 public void mousePressed(MouseEvent e) {
124 if (e.isControlDown()) {
125 if (pitchRect.contains(e.getPoint())) {
126 if (SwingUtilities.isRightMouseButton(e)) {
127 note.setPitch(note.getPitch() + 1);
128 } else if (SwingUtilities.isLeftMouseButton(e)) {
129 note.setPitch(note.getPitch() - 1);
132 } else if (veloRect.contains(e.getPoint())) {
133 if (SwingUtilities.isRightMouseButton(e)) {
134 note.setVelocity(note.getVelocity() + 1);
135 } else if (SwingUtilities.isLeftMouseButton(e)) {
136 note.setVelocity(note.getVelocity() - 1);
140 e.getComponent().repaint();
141 } else if (e.isPopupTrigger()) {
142 popup.show(e.getComponent(), e.getX(), e.getY());
147 class PopupListener implements ActionListener {
148 public void actionPerformed(ActionEvent e) {
149 Object source = e.getSource();
150 if (source == popupProp) {
152 } else if (source == popupRemove) {
158 protected void remove(){