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 implements ActionListener {
17 private NoteArea notes;
18 private Rectangle box;
19 private JPopupMenu popup;
20 private JPopupMenu notePopup;
21 private JMenuItem menuItem;
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) {
28 setLayout(new BorderLayout());
30 notes = new NoteArea(track);
31 notes.setBackground(Color.white);
32 notes.setBorder(BorderFactory.createLineBorder(Color.black));
34 popup = new JPopupMenu();
35 menuItem = new JMenuItem("Add...");
36 menuItem.addActionListener(this);
39 notePopup = new JPopupMenu();
40 menuItem = new JMenuItem("Preferences...");
41 menuItem.addActionListener(this);
42 notePopup.add(menuItem);
44 // notes.addMouseListener(new PopupListener());
45 add(notes, BorderLayout.CENTER);
48 public void actionPerformed(ActionEvent e) {}
50 public Track getTrack() {
56 * Updates the track view.
58 public void update(long tickPosition) {
62 class NoteArea extends JPanel {
63 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
64 private ArrayList rects;
66 public NoteArea(Track track) {
70 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
72 // Creating temporary variables
76 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
77 rects = new ArrayList(track.size() / 2);
79 // Placing note elements
80 Insets insets = getInsets();
81 for (int i = 0; i < track.size(); i++) {
83 if (note instanceof MooNote) {
84 // Adds the note element to the note area.
85 MooNote mn = (MooNote)note;
86 elem = new MooNoteElement(mn);
89 // Moves the note element to the appropriate place.
91 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
92 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
93 if (height == 0) height = NOTE_HEIGHT;
94 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
95 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
98 if (viewLength < (y + height)) viewLength = y + height;
100 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength));
105 private boolean isOccupied(Rectangle r) {
106 Iterator it = rects.iterator();
107 while (it.hasNext()) {
108 if(r.intersects((Rectangle)it.next())) return true;
113 public void paintComponent(Graphics g) {
114 super.paintComponent(g);
115 Graphics2D g2 = (Graphics2D)g;
116 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
117 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
118 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
119 g2.setColor(Color.gray);
126 class PopupListener extends MouseAdapter {
127 public void mousePressed(MouseEvent e) {
131 public void mouseReleased(MouseEvent e) {
135 private void maybeShowPopup(MouseEvent e) {
136 if (e.isPopupTrigger()) {
137 if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
138 notePopup.show(e.getComponent(), e.getX(), e.getY());
140 popup.show(e.getComponent(), e.getX(), e.getY());