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