3 import java.awt.event.*;
6 * Graphical representation of a MIDI note.
8 * @author Andersson, Andreen, Lanneskog, Pehrson
12 public class MooNoteElement extends JPanel {
16 private boolean selected;
17 private Rectangle pitchRect, veloRect;
18 public static final Color bgColor = new Color(160, 218, 255);
21 * Creates a new note element.
22 * @param mn the note that will be graphically represented
23 * @param rows the number of rows that the note will occupy
25 public MooNoteElement (MooNote mn) {
27 columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
28 setBorder(BorderFactory.createLineBorder(Color.black));
29 setBackground(bgColor);
30 addMouseListener(new MAdapter());
32 pitchRect = new Rectangle(0, 0, 15, 10);
33 veloRect = new Rectangle(20, 0, 40, 10);
37 * Returns true if the current NoteElement is selected, otherwise false.
38 * @return if the element is selected
40 public boolean isSelected() {
45 * Selects the current NoteElement.
46 * @param state if the element should be selected
48 public void setSelected(boolean state) {
55 public void paintComponent(Graphics g)
57 super.paintComponent(g);
58 if (note == null || !(g instanceof Graphics2D)) return;
59 Graphics2D g2 = (Graphics2D)g;
60 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
63 int pitch = note.getPitch();
65 case 0: n = "C"; break;
66 case 1: n = "C#"; break;
67 case 2: n = "D"; break;
68 case 3: n = "D#"; break;
69 case 4: n = "E"; break;
70 case 5: n = "F"; break;
71 case 6: n = "F#"; break;
72 case 7: n = "G"; break;
73 case 8: n = "G#"; break;
74 case 9: n = "A"; break;
75 case 10: n = "A#"; break;
76 case 11: n = "B"; break;
88 g2.drawString(n, 1, 9);
89 g2.drawString("" + note.getVelocity(), 21, 9);
92 class MAdapter extends MouseAdapter {
93 public void mouseClicked(MouseEvent e) {
94 if (pitchRect.contains(e.getPoint())) {
95 if (SwingUtilities.isRightMouseButton(e)) {
96 note.setPitch(note.getPitch() + 1);
97 } else if (SwingUtilities.isLeftMouseButton(e)) {
98 note.setPitch(note.getPitch() - 1);
100 } else if (veloRect.contains(e.getPoint())) {
101 if (SwingUtilities.isRightMouseButton(e)) {
102 note.setVelocity(note.getVelocity() + 1);
103 } else if (SwingUtilities.isLeftMouseButton(e)) {
104 note.setVelocity(note.getVelocity() - 1);
107 e.getComponent().repaint();
110 public void mousePressed(MouseEvent e) {}
112 public void mouseReleased(MouseEvent e) {}