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;
21 protected static int viewLength = 0;
24 public MooTrackView (Track track) {
26 //setPreferredSize(new Dimension(200, 9000));
27 setLayout(new BorderLayout());
28 this.setBorder(BorderFactory.createLineBorder(Color.black));
30 title = new MooTrackTitle(track);
31 title.setBorder(BorderFactory.createLineBorder(Color.black));
32 add(title, BorderLayout.NORTH);
34 notes = new NoteArea(track);
35 notes.setBackground(Color.white);
36 notes.setBorder(BorderFactory.createLineBorder(Color.black));
38 popup = new JPopupMenu();
39 menuItem = new JMenuItem("Add...");
40 menuItem.addActionListener(this);
42 menuItem = new JMenuItem("Preferences...");
43 menuItem.addActionListener(this);
46 notes.addMouseListener(new PopupListener());
47 add(notes, BorderLayout.CENTER);
50 public void actionPerformed(ActionEvent e) {}
52 public Track getTrack() {
58 * Updates the track view.
60 public void update() {
64 class NoteArea extends JPanel {
65 public static final int NOTE_HEIGHT = 10;
66 public static final int NOTE_WIDTH = 40;
67 private int trackLength;
69 public NoteArea(Track track) {
70 System.out.println("Creating track view...");
73 setPreferredSize(new Dimension(200,140*NOTE_HEIGHT));
78 Insets insets = getInsets();
79 for (int i = 0; i < track.size(); i++) {
81 if (note instanceof MooNote) {
82 // Adds the note element to the note area.
83 MooNote mn = (MooNote)note;
84 elem = new MooNoteElement(mn);
87 // Places the note element in the appropriate place.
89 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
91 // height = (mn.getDuration() / 24) * NOTE_HEIGHT;
92 // System.out.println("Comp at: " + x + ", " + y + " is: " + findComponentAt(x + 10, y + 10));
93 while(findComponentAt(x, y) instanceof MooNoteElement ||
94 findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
95 elem.setBounds(x, y, NOTE_WIDTH, height);
96 if (viewLength < (y+height)) viewLength = y+height;
98 setPreferredSize(new Dimension(200,viewLength));
103 public void paintComponent(Graphics g) {
104 super.paintComponent(g);
105 Graphics2D g2 = (Graphics2D)g;
106 //(trackLength*NOTE_HEIGHT)
107 for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
108 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
109 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
110 g2.setColor(Color.gray);
117 class PopupListener extends MouseAdapter {
118 public void mousePressed(MouseEvent e) {
122 public void mouseReleased(MouseEvent e) {
126 private void maybeShowPopup(MouseEvent e) {
127 if (e.isPopupTrigger()) {
128 popup.show(e.getComponent(), e.getX(), e.getY());