]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
*** empty log message ***
[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
18         /** 
19          * Creates a new note element.
20          * @param mn    the note that will be graphically represented
21          * @param rows  the number of rows that the note will occupy
22          */
23         public MooNoteElement (MooNote mn) {
24                 note = mn;
25                 columns = mn.getDuration() / 24;
26                 setBorder(BorderFactory.createLineBorder(Color.black));
27         }
28
29         /** 
30          * Returns true if the current NoteElement is selected, otherwise false.
31          * @return if the element is selected
32          */
33         public boolean isSelected() {
34                 return selected;
35         }
36
37         /** 
38          * Selects the current NoteElement.
39          * @param state if the element should be selected
40          */
41         public void setSelected(boolean state) {
42                 selected = state;
43         }
44
45         /**
46          *
47          */
48         public void paintComponent(Graphics g)
49         {
50                 super.paintComponent(g);
51
52                 if (note == null || !(g instanceof Graphics2D)) return;
53                 Graphics2D g2 = (Graphics2D)g;
54
55                 String n = ""; 
56                 int pitch = note.getPitch();
57                 switch (pitch % 12) {
58                         case 0: n = "C"; break;
59                         case 1: n = "C#"; break;
60                         case 2: n = "D"; break;
61                         case 3: n = "D#"; break;
62                         case 4: n = "E"; break;
63                         case 5: n = "F"; break;
64                         case 6: n = "F#"; break;
65                         case 7: n = "G"; break;
66                         case 8: n ="G#"; break;
67                         case 9: n = "A"; break;
68                         case 10: n = "A#"; break;
69                         case 11: n = "B"; break;
70                 }
71
72         /*
73                 switch(columns) {
74                         case 0:
75                         case 1:
76                         ...
77                 }
78         */
79                 
80                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
81                 n = n +(pitch/12);
82                 g2.drawString(n + " "+ note.getVelocity(), 1, 9);
83         }
84 }