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 JMenuItem menuItem;
22 protected static int viewLength = 0;
25 public MooTrackView (Track track) {
28 //setPreferredSize(new Dimension(200, 9000));
29 setLayout(new BorderLayout());
30 this.setBorder(BorderFactory.createLineBorder(Color.black));
32 //title = new MooTrackTitle(track);
33 //title.setBorder(BorderFactory.createLineBorder(Color.black));
34 //add(title, BorderLayout.NORTH);
36 notes = new NoteArea(track);
37 notes.setBackground(Color.white);
38 notes.setBorder(BorderFactory.createLineBorder(Color.black));
40 popup = new JPopupMenu();
41 menuItem = new JMenuItem("Add...");
42 menuItem.addActionListener(this);
44 menuItem = new JMenuItem("Preferences...");
45 menuItem.addActionListener(this);
48 notes.addMouseListener(new PopupListener());
49 add(notes, BorderLayout.CENTER);
52 public void actionPerformed(ActionEvent e) {}
54 public Track getTrack() {
60 * Updates the track view.
62 public void update() {
66 class NoteArea extends JPanel {
67 public static final int NOTE_HEIGHT = 10;
68 public static final int NOTE_WIDTH = 40;
69 private int trackLength;
70 private ArrayList rects;
72 public NoteArea(Track track) {
77 setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
79 // Creating temporary variables
83 rects = new ArrayList(track.size() / 2);
85 // Placing note elements
86 Insets insets = getInsets();
87 for (int i = 0; i < track.size(); i++) {
89 if (note instanceof MooNote) {
90 // Adds the note element to the note area.
91 MooNote mn = (MooNote)note;
92 elem = new MooNoteElement(mn);
95 // Moves the note element to the appropriate place.
97 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
98 height = (mn.getDuration() / 24) * NOTE_HEIGHT;
99 if (height == 0) height = NOTE_HEIGHT;
100 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
101 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
104 if (viewLength < (y + height)) viewLength = y + height;
106 // while(findComponentAt(x, y) instanceof MooNoteElement ||
107 // findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
109 setPreferredSize(new Dimension(200, viewLength));
114 private boolean isOccupied(Rectangle r) {
115 Iterator it = rects.iterator();
116 while (it.hasNext()) {
117 if(r.intersects((Rectangle)it.next())) return true;
122 public void paintComponent(Graphics g) {
123 super.paintComponent(g);
124 Graphics2D g2 = (Graphics2D)g;
125 for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
126 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
127 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
128 g2.setColor(Color.gray);
135 class PopupListener extends MouseAdapter {
136 public void mousePressed(MouseEvent e) {
140 public void mouseReleased(MouseEvent e) {
144 private void maybeShowPopup(MouseEvent e) {
145 if (e.isPopupTrigger()) {
146 popup.show(e.getComponent(), e.getX(), e.getY());