]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
speed improvements.. I think
[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         private String notePitch;
20         private String noteVelocity;
21
22         /** 
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
26          */
27         public MooNoteElement (MooNote mn) {
28                 note = mn;
29                 calculateString();
30                 columns = mn.getDuration() / (Moosique.getSequence().getResolution() / 4);
31                 setBorder(BorderFactory.createLineBorder(Color.black));
32                 setBackground(bgColor);
33                 addMouseListener(new MAdapter());
34
35                 pitchRect = new Rectangle(0, 0, 15, 10);
36                 veloRect = new Rectangle(20, 0, 40, 10);
37         }
38
39         /** 
40          * Returns true if the current NoteElement is selected, otherwise false.
41          * @return if the element is selected
42          */
43         public boolean isSelected() {
44                 return selected;
45         }
46
47         /** 
48          * Selects the current NoteElement.
49          * @param state if the element should be selected
50          */
51         public void setSelected(boolean state) {
52                 selected = state;
53         }
54
55         /**
56          *
57          */
58         public void paintComponent(Graphics g)
59         {
60                 super.paintComponent(g);
61                 if (!(g instanceof Graphics2D)) return;
62                 Graphics2D g2 = (Graphics2D)g;
63                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
64         /*
65                 switch(columns) {
66                         case 0:
67                         case 1:
68                         ...
69                 }
70         */
71                 
72                 g2.drawString(notePitch, 1, 9);
73                 g2.drawString("" + noteVelocity, 21, 9);
74         }
75
76         protected void calculateString(){
77
78                 noteVelocity = ""; 
79                 notePitch = "";
80                 if(note == null) return;
81
82                 int pitch = note.getPitch();
83                 switch (pitch % 12) {
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;
96                 }
97                 notePitch += pitch / 12;
98                 noteVelocity = ""+note.getVelocity();
99         }
100
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);
108                                 }
109                                 calculateString();
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);
115                                 }
116                                 calculateString();
117                         }
118                         e.getComponent().repaint();
119                 }
120
121                 public void mousePressed(MouseEvent e) {}
122
123                 public void mouseReleased(MouseEvent e) {}
124         }
125 }