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;
23 public MooTrackView (Track track) {
25 //setPreferredSize(new Dimension(200, 9000));
26 setLayout(new BorderLayout());
27 this.setBorder(BorderFactory.createLineBorder(Color.black));
29 title = new MooTrackTitle(track);
30 title.setBorder(BorderFactory.createLineBorder(Color.black));
31 add(title, BorderLayout.NORTH);
33 notes = new NoteArea(track);
34 notes.setBackground(Color.white);
35 notes.setBorder(BorderFactory.createLineBorder(Color.black));
37 popup = new JPopupMenu();
38 menuItem = new JMenuItem("Add...");
39 menuItem.addActionListener(this);
41 menuItem = new JMenuItem("Preferences...");
42 menuItem.addActionListener(this);
45 notes.addMouseListener(new PopupListener());
46 add(notes, BorderLayout.CENTER);
49 public void actionPerformed(ActionEvent e) {}
51 public Track getTrack() {
57 * Updates the track view.
59 public void update() {
63 class NoteArea extends JPanel {
64 public static final int NOTE_HEIGHT = 10;
65 public static final int NOTE_WIDTH = 40;
66 private int trackLength;
68 public NoteArea(Track track) {
69 System.out.println("Creating track view...");
72 setPreferredSize(new Dimension(200,140*NOTE_HEIGHT));
77 Insets insets = getInsets();
78 for (int i = 0; i < track.size(); i++) {
80 if (note instanceof MooNote) {
81 // Adds the note element to the note area.
82 MooNote mn = (MooNote)note;
83 elem = new MooNoteElement(mn);
86 // Places the note element in the appropriate place.
88 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
90 // height = (mn.getDuration() / 24) * NOTE_HEIGHT;
91 // System.out.println("Comp at: " + x + ", " + y + " is: " + findComponentAt(x + 10, y + 10));
92 while(findComponentAt(x, y) instanceof MooNoteElement ||
93 findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
94 elem.setBounds(x, y, NOTE_WIDTH, height);
100 public void paintComponent(Graphics g) {
101 super.paintComponent(g);
102 Graphics2D g2 = (Graphics2D)g;
103 for (int c = 0; c < (trackLength*NOTE_HEIGHT); c += NOTE_HEIGHT) {
104 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
105 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
106 g2.setColor(Color.gray);
113 class PopupListener extends MouseAdapter {
114 public void mousePressed(MouseEvent e) {
118 public void mouseReleased(MouseEvent e) {
122 private void maybeShowPopup(MouseEvent e) {
123 if (e.isPopupTrigger()) {
124 popup.show(e.getComponent(), e.getX(), e.getY());