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 MooTrackTitle title;
18 private NoteArea notes;
19 private Rectangle box;
20 private JPopupMenu popup;
21 private JPopupMenu notePopup;
22 private JMenuItem menuItem;
23 protected static int viewLength = 0;
26 public MooTrackView (Track track) {
29 //setPreferredSize(new Dimension(200, 9000));
30 setLayout(new BorderLayout());
31 this.setBorder(BorderFactory.createLineBorder(Color.black));
33 //title = new MooTrackTitle(track);
34 //title.setBorder(BorderFactory.createLineBorder(Color.black));
35 //add(title, BorderLayout.NORTH);
37 notes = new NoteArea(track);
38 notes.setBackground(Color.white);
39 notes.setBorder(BorderFactory.createLineBorder(Color.black));
41 popup = new JPopupMenu();
42 menuItem = new JMenuItem("Add...");
43 menuItem.addActionListener(this);
47 notePopup = new JPopupMenu();
48 menuItem = new JMenuItem("Preferences...");
49 menuItem.addActionListener(this);
50 notePopup.add(menuItem);
52 notes.addMouseListener(new PopupListener());
53 add(notes, BorderLayout.CENTER);
56 public void actionPerformed(ActionEvent e) {}
58 public Track getTrack() {
64 * Updates the track view.
66 public void update(long tickPosition) {
70 class NoteArea extends JPanel {
71 public static final int NOTE_HEIGHT = 10;
72 public static final int NOTE_WIDTH = 40;
73 private int trackLength;
74 private ArrayList rects;
76 public NoteArea(Track track) {
81 setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
83 // Creating temporary variables
87 rects = new ArrayList(track.size() / 2);
89 // Placing note elements
90 Insets insets = getInsets();
91 for (int i = 0; i < track.size(); i++) {
93 if (note instanceof MooNote) {
94 // Adds the note element to the note area.
95 MooNote mn = (MooNote)note;
96 elem = new MooNoteElement(mn);
99 // Moves the note element to the appropriate place.
101 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
102 height = (mn.getDuration() / 24) * NOTE_HEIGHT;
103 if (height == 0) height = NOTE_HEIGHT;
104 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
105 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
108 if (viewLength < (y + height)) viewLength = y + height;
110 // while(findComponentAt(x, y) instanceof MooNoteElement ||
111 // findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
113 setPreferredSize(new Dimension(200, viewLength));
118 private boolean isOccupied(Rectangle r) {
119 Iterator it = rects.iterator();
120 while (it.hasNext()) {
121 if(r.intersects((Rectangle)it.next())) return true;
126 public void paintComponent(Graphics g) {
127 super.paintComponent(g);
128 Graphics2D g2 = (Graphics2D)g;
129 for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
130 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
131 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
132 g2.setColor(Color.gray);
139 class PopupListener extends MouseAdapter {
140 public void mousePressed(MouseEvent e) {
144 public void mouseReleased(MouseEvent e) {
148 private void maybeShowPopup(MouseEvent e) {
149 if (e.isPopupTrigger()) {
150 if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
151 notePopup.show(e.getComponent(), e.getX(), e.getY());
153 popup.show(e.getComponent(), e.getX(), e.getY());