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 * Selects the current NoteElement.
64 * @param state if the element should be selected
66 public void setSelected(boolean state) {
71 * Draws the string that shows the note's properties.
72 * @param g The Graphics object used to draw the strings.
74 public void paintComponent(Graphics g)
76 super.paintComponent(g);
77 if (!(g instanceof Graphics2D)) return;
78 Graphics2D g2 = (Graphics2D)g;
79 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
88 g2.drawString(notePitch, 1, 9);
89 g2.drawString("" + noteVelocity, 21, 9);
93 * Calculate what the string that shows the note properties should look like.
95 protected void calculateString(){
99 if(note == null) return;
101 int pitch = note.getPitch();
102 switch (pitch % 12) {
103 case 0: notePitch = "C"; break;
104 case 1: notePitch = "C#"; break;
105 case 2: notePitch = "D"; break;
106 case 3: notePitch = "D#"; break;
107 case 4: notePitch = "E"; break;
108 case 5: notePitch = "F"; break;
109 case 6: notePitch = "F#"; break;
110 case 7: notePitch = "G"; break;
111 case 8: notePitch = "G#"; break;
112 case 9: notePitch = "A"; break;
113 case 10: notePitch = "A#"; break;
114 case 11: notePitch = "B"; break;
116 notePitch += pitch / 12;
117 noteVelocity = ""+note.getVelocity();
121 * Gets the note that is element represents
122 * @return the MooNote object.
124 public MooNote getNote(){
129 * Listener that checks the mouse actions on this element.
131 class MAdapter extends MouseAdapter {
133 * Checks if the mouse is pressed.
134 * Pops up the menu if right mousebutton is used.
135 * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down.
136 * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down.
137 * @param e The events recieved.
139 public void mousePressed(MouseEvent e) {
140 if (e.isControlDown()) {
141 if (pitchRect.contains(e.getPoint())) {
142 if (SwingUtilities.isRightMouseButton(e)) {
143 note.setPitch(note.getPitch() + 1);
144 } else if (SwingUtilities.isLeftMouseButton(e)) {
145 note.setPitch(note.getPitch() - 1);
148 } else if (veloRect.contains(e.getPoint())) {
149 if (SwingUtilities.isRightMouseButton(e)) {
150 note.setVelocity(note.getVelocity() + 1);
151 } else if (SwingUtilities.isLeftMouseButton(e)) {
152 note.setVelocity(note.getVelocity() - 1);
156 e.getComponent().repaint();
157 } else if (e.isPopupTrigger()) {
158 popup.show(e.getComponent(), e.getX(), e.getY());
164 * Listens on the actions made to the popupmenu.
166 class PopupListener implements ActionListener {
167 public void actionPerformed(ActionEvent e) {
168 Object source = e.getSource();
169 if (source == popupProp) {
171 } else if (source == popupRemove) {
178 * Asks the MooTrackView that it's painted on to remove this element and the note.
180 protected void remove(){
181 mtv.removeNote(this, note);