3 import java.awt.event.*;
4 import javax.sound.midi.*;
8 * Graphical representation of a MIDI track.
10 * @author Andersson , Andreen, Lanneskog, Pehrson
14 public class MooTrackView extends JPanel {
17 private Rectangle box;
18 private JPopupMenu popup;
19 private JMenuItem menuItem;
20 private ArrayList rects;
21 protected static int viewLength = 0;
22 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
24 public MooTrackView (Track track) {
29 setBackground(Color.white);
30 setBorder(BorderFactory.createLineBorder(Color.black));
32 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
34 // Creates temporary variables
37 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
39 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
40 rects = new ArrayList(track.size() / 2);
42 // Places note elements
43 Insets insets = getInsets();
44 for (int i = 0; i < track.size(); i++) {
46 if (note instanceof MooNote) {
47 // Adds the note element to the note area.
48 MooNote mn = (MooNote)note;
49 elem = new MooNoteElement(this, mn);
52 // Moves the note element to the appropriate place.
54 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
55 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
56 if (height == 0) height = NOTE_HEIGHT;
57 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
58 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
61 if (viewLength < (y + height)) viewLength = y + height;
63 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
67 // Creates pop-up menu.
68 popup = new JPopupMenu();
69 menuItem = new JMenuItem("Add...");
70 // menuItem.addActionListener();
73 addMouseListener(new PopupListener());
76 public Track getTrack() {
80 private boolean isOccupied(Rectangle r) {
81 Iterator it = rects.iterator();
82 while (it.hasNext()) {
83 if(r.intersects((Rectangle)it.next())) return true;
88 public void remove(MooNoteElement elem) {
89 remove((Component)elem);
93 public void paintComponent(Graphics g) {
94 super.paintComponent(g);
95 Graphics2D g2 = (Graphics2D)g;
96 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
97 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
98 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
99 g2.setColor(Color.gray);
106 * Updates the track view.
108 public void update(long tickPosition) {
112 class PopupListener extends MouseAdapter {
113 public void mousePressed(MouseEvent e) {
114 if (e.isPopupTrigger()) {
115 popup.show(e.getComponent(), e.getX(), e.getY());