]> ruin.nu Git - moosique.git/blob - MooViewCounter.java
no message
[moosique.git] / MooViewCounter.java
1 import javax.sound.midi.*;
2 import javax.swing.*;
3 import java.awt.*;
4
5 /**
6  * A graphical representation of the time signature of the current sequence.
7  * 
8  * @author  Andersson, Andreen, Lanneskog, Pehrson
9  * @version 1
10  */
11  
12 public class MooViewCounter extends JPanel {
13
14         private int measure, halfBeat, beat, halfNote;
15         private static final int CELL_HEIGHT = 10;
16         
17         /** 
18          * Creates an musical ruler depending on the timesignature
19          */
20         public MooViewCounter (MetaMessage[] timeSigs) {
21                 // ...for now
22                 int timeSig1 = 4, timeSig2 = 4;
23                 calculateLineSpecs(timeSig1, timeSig2);
24                 setBackground(Moosique.getGUI().bgColor);
25                 setPreferredSize(new Dimension(35, 200 * CELL_HEIGHT));
26         }
27
28         private void calculateLineSpecs(int ts1, int ts2) {
29                 switch (ts2) {
30                         case 16: measure = ts1;                 // 1/16
31                                  break;
32                         case  8: measure = ts1 * 2;     // 1/16
33                                  halfBeat = measure / ts1;      // 1/8
34                                  break;
35                         case  2: measure = ts1 * 8;     // 1/16
36                                  halfBeat = beat / 2;           // 1/8
37                                  beat = halfNote / 2;           // 1/4
38                                  halfNote = measure / ts1;      // 1/2
39                                  break;
40                         case  1: measure = ts1 * 16;    // 1/16
41                                  halfBeat = beat / 2;           // 1/8
42                                  beat = halfNote / 2;           // 1/4
43                                  halfNote = measure / 2;        // 1/2
44                                  break;
45                         default: measure = ts1 * 4;     // 1/16
46                                  halfBeat = beat / 2;           // 1/8          
47                                  beat = measure / ts1;  // 1/4  
48                                  break;
49                 }
50         }
51
52         /**
53          * Draws the ruler-like fields.
54          * @param g The Graphics object it operates on.
55          */
56         public void paintComponent(Graphics g) {
57                 super.paintComponent(g);
58                 if (!(g instanceof Graphics2D)) return;
59                 Graphics2D g2 = (Graphics2D)g;
60                 g2.setColor(Color.black);
61
62
63                 /* Using time signature...
64                 for (int i = 0, tick = getTicksForPosition(i, 0, 0); tick < Moosique.getSequence().getTickLength(); i++) {
65                         int[] ts = getTimeSig(tick);
66                         calculateLineSpecs(ts[0], ts[1]);
67                         g2.drawLine(0, c * CELL_HEIGHT, 5, c * CELL_HEIGHT);                                    // 1/16
68                         g2.drawLine(0, c * CELL_HEIGHT * halfBeat, 10, c * CELL_HEIGHT * halfBeat);             // 1/8
69                         g2.drawLine(0, c * CELL_HEIGHT * beat, 15, c * CELL_HEIGHT * beat);                     // 1/4
70                         g2.drawLine(0, c * CELL_HEIGHT * halfNote, 20, c * CELL_HEIGHT * halfNote);             // 1/2
71                         g2.drawLine(0, c * CELL_HEIGHT * measure, 30, c * CELL_HEIGHT * measure);               // 1/1
72                 }
73                 */
74
75                 for (int c = 0; c < 200; c++) {
76                         g2.drawLine(0, c * CELL_HEIGHT, 5, c * CELL_HEIGHT);                                    // 1/16
77                         g2.drawLine(0, c * CELL_HEIGHT * halfBeat, 10, c * CELL_HEIGHT * halfBeat);             // 1/8
78                         g2.drawLine(0, c * CELL_HEIGHT * beat, 15, c * CELL_HEIGHT * beat);                     // 1/4
79                         g2.drawLine(0, c * CELL_HEIGHT * halfNote, 20, c * CELL_HEIGHT * halfNote);             // 1/2
80                         g2.drawLine(0, c * CELL_HEIGHT * measure, 30, c * CELL_HEIGHT * measure);               // 1/1
81                 }
82         }
83 }