1 import javax.sound.midi.*;
6 * A graphical representation of the time signature of the current sequence.
8 * @author Andersson, Andreen, Lanneskog, Pehrson
12 public class MooViewCounter extends JPanel {
14 private int measure, halfBeat, beat, halfNote;
15 private static final int CELL_HEIGHT = 10;
18 * Creates an musical ruler depending on the timesignature
20 public MooViewCounter (MetaMessage[] timeSigs) {
22 int timeSig1 = 4, timeSig2 = 4;
23 calculateLineSpecs(timeSig1, timeSig2);
24 setBackground(Moosique.getGUI().bgColor);
25 setPreferredSize(new Dimension(35, 200 * CELL_HEIGHT));
28 private void calculateLineSpecs(int ts1, int ts2) {
30 case 16: measure = ts1; // 1/16
32 case 8: measure = ts1 * 2; // 1/16
33 halfBeat = measure / ts1; // 1/8
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
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
45 default: measure = ts1 * 4; // 1/16
46 halfBeat = beat / 2; // 1/8
47 beat = measure / ts1; // 1/4
53 * Draws the ruler-like fields.
54 * @param g The Graphics object it operates on.
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);
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
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