3 import java.awt.event.*;
4 import javax.sound.midi.*;
7 * Graphical representation of a MIDI track.
9 * @author Andersson , Andreen, Lanneskog, Pehrson
13 public class MooTrackView extends JPanel implements ActionListener {
16 private MooTrackTitle title;
17 private NoteArea notes;
18 private Rectangle box;
19 private JPopupMenu popup;
20 private JMenuItem menuItem;
22 public MooTrackView (Track track) {
24 setPreferredSize(new Dimension(200, 200));
25 setLayout(new BorderLayout());
26 this.setBorder(BorderFactory.createLineBorder(Color.black));
28 title = new MooTrackTitle(track);
29 title.setBorder(BorderFactory.createLineBorder(Color.black));
30 add(title, BorderLayout.NORTH);
32 notes = new NoteArea(track);
33 notes.setBackground(Color.white);
34 notes.setBorder(BorderFactory.createLineBorder(Color.black));
36 popup = new JPopupMenu();
37 menuItem = new JMenuItem("Add...");
38 menuItem.addActionListener(this);
40 menuItem = new JMenuItem("Preferences...");
41 menuItem.addActionListener(this);
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() {
62 class NoteArea extends JPanel {
64 public static final int NOTE_SIZE = 20;
66 public NoteArea(Track track) {
72 Insets insets = getInsets();
73 for (int i = 0; i < track.size(); i++) {
75 if (note instanceof MooNote) {
76 // Adds the note element to the note area.
77 MooNote mn = (MooNote)note;
78 elem = new MooNoteElement(mn);
81 // Places the note element in the appropriate place.
83 y = insets.top + (int)(mn.getTick() / 24) * NOTE_SIZE;
84 height = (mn.getDuration() / 24) * NOTE_SIZE;
85 //System.out.println(findComponentAt(x, y) != this);
86 elem.setBounds(x, y, NOTE_SIZE, 20);
91 public void paintComponent(Graphics g) {
92 super.paintComponent(g);
93 Graphics2D g2 = (Graphics2D)g;
94 for (int c = 0; c < 1000; c += 20) {
95 for (int r = 0; r < 200; r += 20) {
96 box = new Rectangle(r, c, 20, 20);
97 g2.setColor(Color.gray);
104 class PopupListener extends MouseAdapter {
105 public void mousePressed(MouseEvent e) {
109 public void mouseReleased(MouseEvent e) {
113 private void maybeShowPopup(MouseEvent e) {
114 if (e.isPopupTrigger()) {
115 popup.show(e.getComponent(), e.getX(), e.getY());