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;
19 private JPopupMenu popup;
20 private JMenuItem menuItem;
21 private ArrayList rects;
22 protected static int viewLength = 0;
23 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
25 public MooTrackView (Track track) {
30 setBackground(Color.white);
31 setBorder(BorderFactory.createLineBorder(Color.black));
33 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
35 // Creates temporary variables
38 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
40 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
41 rects = new ArrayList(track.size() / 2);
43 // Places note elements
44 Insets insets = getInsets();
45 for (int i = 0; i < track.size(); i++) {
47 if (note instanceof MooNote) {
48 // Adds the note element to the note area.
49 MooNote mn = (MooNote)note;
50 elem = new MooNoteElement(this, mn);
53 // Moves the note element to the appropriate place.
55 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
56 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
57 if (height == 0) height = NOTE_HEIGHT;
58 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
59 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
62 if (viewLength < (y + height)) viewLength = y + height;
64 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
68 // Creates pop-up menu.
69 popup = new JPopupMenu();
70 menuItem = new JMenuItem("Add...");
71 // menuItem.addActionListener();
74 addMouseListener(new PopupListener());
77 public Track getTrack() {
83 * Updates the track view.
85 public void update(long tickPosition) {
89 private boolean isOccupied(Rectangle r) {
90 Iterator it = rects.iterator();
91 while (it.hasNext()) {
92 if(r.intersects((Rectangle)it.next())) return true;
97 public void remove(MooNoteElement elem) {
98 remove((Component)elem);
99 elem.getNote().removeFrom(track);
104 public void paintComponent(Graphics g) {
105 super.paintComponent(g);
106 Graphics2D g2 = (Graphics2D)g;
107 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
108 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
109 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
110 g2.setColor(Color.gray);
116 class PopupListener extends MouseAdapter {
117 public void mousePressed(MouseEvent e) {
118 if (e.isPopupTrigger()) {
119 popup.show(e.getComponent(), e.getX(), e.getY());