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);
19 private String notePitch;
20 private String noteVelocity;
23 * Creates a new note element.
24 * @param mn the note that will be graphically represented
25 * @param rows the number of rows that the note will occupy
27 public MooNoteElement (MooNote mn) {
30 columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
31 setBorder(BorderFactory.createLineBorder(Color.black));
32 setBackground(bgColor);
33 addMouseListener(new MAdapter());
35 pitchRect = new Rectangle(0, 0, 15, 10);
36 veloRect = new Rectangle(20, 0, 40, 10);
40 * Returns true if the current NoteElement is selected, otherwise false.
41 * @return if the element is selected
43 public boolean isSelected() {
48 * Selects the current NoteElement.
49 * @param state if the element should be selected
51 public void setSelected(boolean state) {
58 public void paintComponent(Graphics g)
60 super.paintComponent(g);
61 if (!(g instanceof Graphics2D)) return;
62 Graphics2D g2 = (Graphics2D)g;
63 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
72 g2.drawString(notePitch, 1, 9);
73 g2.drawString("" + noteVelocity, 21, 9);
76 protected void calculateString(){
80 if(note == null) return;
82 int pitch = note.getPitch();
84 case 0: notePitch = "C"; break;
85 case 1: notePitch = "C#"; break;
86 case 2: notePitch = "D"; break;
87 case 3: notePitch = "D#"; break;
88 case 4: notePitch = "E"; break;
89 case 5: notePitch = "F"; break;
90 case 6: notePitch = "F#"; break;
91 case 7: notePitch = "G"; break;
92 case 8: notePitch = "G#"; break;
93 case 9: notePitch = "A"; break;
94 case 10: notePitch = "A#"; break;
95 case 11: notePitch = "B"; break;
97 notePitch += pitch / 12;
98 noteVelocity = ""+note.getVelocity();
101 class MAdapter extends MouseAdapter {
102 public void mouseClicked(MouseEvent e) {
103 if (pitchRect.contains(e.getPoint())) {
104 if (SwingUtilities.isRightMouseButton(e)) {
105 note.setPitch(note.getPitch() + 1);
106 } else if (SwingUtilities.isLeftMouseButton(e)) {
107 note.setPitch(note.getPitch() - 1);
110 } else if (veloRect.contains(e.getPoint())) {
111 if (SwingUtilities.isRightMouseButton(e)) {
112 note.setVelocity(note.getVelocity() + 1);
113 } else if (SwingUtilities.isLeftMouseButton(e)) {
114 note.setVelocity(note.getVelocity() - 1);
118 e.getComponent().repaint();
121 public void mousePressed(MouseEvent e) {}
123 public void mouseReleased(MouseEvent e) {}