]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
Fixed some bugs
[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 boolean selected;
16
17         /** 
18          * Creates a new note element.
19          * @param mn The note that will be graphically represented
20          */
21         public MooNoteElement (MooNote mn) {
22                 note = mn;
23                 setPreferredSize(new Dimension(20,20));
24                 setFont(new Font("Helvetica", Font.PLAIN, 10));
25
26                 class MouseList implements MouseListener{
27                         public void mouseClicked(MouseEvent event){
28                                 //Bring upp dialog to edit note.
29                         }
30
31                         public void mouseEntered(MouseEvent event){
32                                 //Show note props in statusbar?
33                         }
34
35                         public void mouseExited(MouseEvent event){
36                                 //Reset statusbar?
37                         }
38
39                         public void mousePressed(MouseEvent event){     }
40
41                         public void mouseReleased(MouseEvent event){}
42                 };
43                 MouseListener listener = new MouseList();
44                 addMouseListener(listener);
45         }
46
47         /** 
48          * Returns true if the current NoteElement is selected, otherwise false.
49          * @return if the element is selected
50          */
51         public boolean isSelected() {
52                 return selected;
53         }
54
55         /** 
56          * Selects the current NoteElement.
57          * @param state if the element should be selected
58          */
59         public void setSelected(boolean state) {
60                 selected = state;
61         }
62
63         /**
64          *
65          */
66         public void paintComponent(Graphics g)
67         {
68                 super.paintComponent(g);
69
70                 if (note == null)
71                         return;
72
73                 if (!(g instanceof Graphics2D))
74                         return;
75                 Graphics2D g2 = (Graphics2D)g;
76
77                 String n = ""; 
78                 int pitch = note.getPitch();
79                 switch( pitch % 12)
80                 {
81                         case 0: n = "C"; break;
82                         case 1: n = "C#"; break;
83                         case 2: n = "D"; break;
84                         case 3: n = "D#"; break;
85                         case 4: n = "E"; break;
86                         case 5: n = "F"; break;
87                         case 6: n = "F#"; break;
88                         case 7: n = "G"; break;
89                         case 8: n ="G#"; break;
90                         case 9: n = "A"; break;
91                         case 10: n = "A#"; break;
92                         case 11: n = "B"; break;
93                 }
94
95                 g2.drawString(n +" "+(pitch/12), 2, 2);
96
97         }
98
99 }