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 parent The MooTrackView that this element will be painted on.
27 * @param mn the note that will be graphically represented
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 * Returns the note of this element.
66 public MooNote getNote() {
71 * Selects the current NoteElement.
72 * @param state if the element should be selected
74 public void setSelected(boolean state) {
79 * Draws the string that shows the note's properties.
80 * @param g The Graphics object used to draw the strings.
82 public void paintComponent(Graphics g)
84 super.paintComponent(g);
85 if (!(g instanceof Graphics2D)) return;
86 Graphics2D g2 = (Graphics2D)g;
87 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
96 g2.drawString(notePitch, 1, 8);
97 g2.drawString("" + noteVelocity, 21, 8);
101 * Calculate what the string that shows the note properties should look like.
103 protected void calculateString(){
107 if(note == null) return;
109 int pitch = note.getPitch();
110 switch (pitch % 12) {
111 case 0: notePitch = "C"; break;
112 case 1: notePitch = "C#"; break;
113 case 2: notePitch = "D"; break;
114 case 3: notePitch = "D#"; break;
115 case 4: notePitch = "E"; break;
116 case 5: notePitch = "F"; break;
117 case 6: notePitch = "F#"; break;
118 case 7: notePitch = "G"; break;
119 case 8: notePitch = "G#"; break;
120 case 9: notePitch = "A"; break;
121 case 10: notePitch = "A#"; break;
122 case 11: notePitch = "B"; break;
124 notePitch += pitch / 12;
125 noteVelocity = ""+note.getVelocity();
129 * Listener that checks the mouse actions on this element.
131 class MAdapter extends MouseAdapter {
133 public void mouseClicked(MouseEvent e) {
138 * Checks if the mouse is pressed.
139 * Pops up the menu if right mousebutton is used.
140 * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down.
141 * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down.
142 * @param e The events recieved.
144 public void mousePressed(MouseEvent e) {
145 if (e.isControlDown()) {
146 if (pitchRect.contains(e.getPoint())) {
147 if (SwingUtilities.isRightMouseButton(e)) {
148 note.setPitch(note.getPitch() + 1);
149 } else if (SwingUtilities.isLeftMouseButton(e)) {
150 note.setPitch(note.getPitch() - 1);
153 } else if (veloRect.contains(e.getPoint())) {
154 if (SwingUtilities.isRightMouseButton(e)) {
155 note.setVelocity(note.getVelocity() + 1);
156 } else if (SwingUtilities.isLeftMouseButton(e)) {
157 note.setVelocity(note.getVelocity() - 1);
161 e.getComponent().repaint();
162 } else if (e.isPopupTrigger()) {
163 popup.show(e.getComponent(), e.getX(), e.getY());
169 * Listens on the actions made to the popupmenu.
171 class PopupListener implements ActionListener {
172 public void actionPerformed(ActionEvent e) {
173 Object source = e.getSource();
174 if (source == popupProp) {
176 } else if (source == popupRemove) {
183 * Asks the MooTrackView that it's painted on to remove this element and the note.
185 protected void remove(){
186 mtv.removeNote(this);