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;
16 private boolean selected;
17 private Rectangle pitchRect, veloRect;
18 public static final Color bgColor = new Color(160, 218, 255);
19 private String notePitch;
20 private String noteVelocity;
21 private JPopupMenu popup;
22 private JMenuItem popupRemove, popupProp;
25 * Creates a new note element.
26 * @param mn the note that will be graphically represented
27 * @param rows the number of rows that the note will occupy
29 public MooNoteElement (MooTrackView parent, MooNote mn) {
33 setBorder(BorderFactory.createLineBorder(Color.black));
34 setBackground(bgColor);
35 addMouseListener(new MAdapter());
37 // Defines coordinates.
38 pitchRect = new Rectangle(0, 0, 15, 10);
39 veloRect = new Rectangle(20, 0, 40, 10);
41 // Creates pop-up menu.
42 popup = new JPopupMenu();
43 PopupListener pList = new PopupListener();
44 popupProp = new JMenuItem("Preferences...");
45 popupProp.addActionListener(pList);
48 popupRemove = new JMenuItem("Remove");
49 popupRemove.addActionListener(pList);
50 popup.add(popupRemove);
55 * Returns true if the current NoteElement is selected, otherwise false.
56 * @return if the element is selected
58 public boolean isSelected() {
63 * Selects the current NoteElement.
64 * @param state if the element should be selected
66 public void setSelected(boolean state) {
73 public void paintComponent(Graphics g)
75 super.paintComponent(g);
76 if (!(g instanceof Graphics2D)) return;
77 Graphics2D g2 = (Graphics2D)g;
78 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
87 g2.drawString(notePitch, 1, 9);
88 g2.drawString("" + noteVelocity, 21, 9);
91 protected void calculateString(){
95 if(note == null) return;
97 int pitch = note.getPitch();
99 case 0: notePitch = "C"; break;
100 case 1: notePitch = "C#"; break;
101 case 2: notePitch = "D"; break;
102 case 3: notePitch = "D#"; break;
103 case 4: notePitch = "E"; break;
104 case 5: notePitch = "F"; break;
105 case 6: notePitch = "F#"; break;
106 case 7: notePitch = "G"; break;
107 case 8: notePitch = "G#"; break;
108 case 9: notePitch = "A"; break;
109 case 10: notePitch = "A#"; break;
110 case 11: notePitch = "B"; break;
112 notePitch += pitch / 12;
113 noteVelocity = ""+note.getVelocity();
116 public MooNote getNote(){
120 class MAdapter extends MouseAdapter {
121 public void mousePressed(MouseEvent e) {
122 if (e.isControlDown()) {
123 if (pitchRect.contains(e.getPoint())) {
124 if (SwingUtilities.isRightMouseButton(e)) {
125 note.setPitch(note.getPitch() + 1);
126 } else if (SwingUtilities.isLeftMouseButton(e)) {
127 note.setPitch(note.getPitch() - 1);
130 } else if (veloRect.contains(e.getPoint())) {
131 if (SwingUtilities.isRightMouseButton(e)) {
132 note.setVelocity(note.getVelocity() + 1);
133 } else if (SwingUtilities.isLeftMouseButton(e)) {
134 note.setVelocity(note.getVelocity() - 1);
138 e.getComponent().repaint();
139 } else if (e.isPopupTrigger()) {
140 popup.show(e.getComponent(), e.getX(), e.getY());
145 class PopupListener implements ActionListener {
146 public void actionPerformed(ActionEvent e) {
147 Object source = e.getSource();
148 if (source == popupProp) {
150 } else if (source == popupRemove) {
156 protected void remove(){