]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
Fixed mouse listener for note element
[moosique.git] / MooNoteElement.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 /**
6  * Graphical representation of a MIDI note.
7  * 
8  * @author  Andersson, Andreen, Lanneskog, Pehrson
9  * @version 1
10  */
11  
12 public class MooNoteElement extends JPanel {
13
14         private MooNote note;
15         private int columns;
16         private boolean selected;
17         private Rectangle pitchRect, veloRect;
18         public static final Color bgColor = new Color(160, 218, 255);
19
20         /** 
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
24          */
25         public MooNoteElement (MooNote mn) {
26                 note = mn;
27                 columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
28                 setBorder(BorderFactory.createLineBorder(Color.black));
29                 setBackground(bgColor);
30                 addMouseListener(new MAdapter());
31
32                 pitchRect = new Rectangle(0, 0, 15, 10);
33                 veloRect = new Rectangle(20, 0, 40, 10);
34         }
35
36         /** 
37          * Returns true if the current NoteElement is selected, otherwise false.
38          * @return if the element is selected
39          */
40         public boolean isSelected() {
41                 return selected;
42         }
43
44         /** 
45          * Selects the current NoteElement.
46          * @param state if the element should be selected
47          */
48         public void setSelected(boolean state) {
49                 selected = state;
50         }
51
52         /**
53          *
54          */
55         public void paintComponent(Graphics g)
56         {
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));
61
62                 String n = ""; 
63                 int pitch = note.getPitch();
64                 switch (pitch % 12) {
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;
77                 }
78                 n += pitch / 12;
79
80         /*
81                 switch(columns) {
82                         case 0:
83                         case 1:
84                         ...
85                 }
86         */
87                 
88                 g2.drawString(n, 1, 9);
89                 g2.drawString("" + note.getVelocity(), 21, 9);
90         }
91
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);
99                                 }
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);
105                                 }
106                         }
107                         e.getComponent().repaint();
108                 }
109
110                 public void mousePressed(MouseEvent e) {}
111
112                 public void mouseReleased(MouseEvent e) {}
113         }
114 }