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) {
27 //setPreferredSize(new Dimension(200, 9000));
28 setLayout(new BorderLayout());
29 this.setBorder(BorderFactory.createLineBorder(Color.black));
31 //title = new MooTrackTitle(track);
32 //title.setBorder(BorderFactory.createLineBorder(Color.black));
33 //add(title, BorderLayout.NORTH);
35 notes = new NoteArea(track);
36 notes.setBackground(Color.white);
37 notes.setBorder(BorderFactory.createLineBorder(Color.black));
39 popup = new JPopupMenu();
40 menuItem = new JMenuItem("Add...");
41 menuItem.addActionListener(this);
43 menuItem = new JMenuItem("Preferences...");
44 menuItem.addActionListener(this);
47 notes.addMouseListener(new PopupListener());
48 add(notes, BorderLayout.CENTER);
51 public void actionPerformed(ActionEvent e) {}
53 public Track getTrack() {
59 * Updates the track view.
61 public void update() {
65 class NoteArea extends JPanel {
66 public static final int NOTE_HEIGHT = 10;
67 public static final int NOTE_WIDTH = 40;
68 private int trackLength;
69 private ArrayList rects;
71 public NoteArea(Track track) {
75 setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
77 // Creating temporary variables
81 rects = new ArrayList(track.size() / 2);
83 // Placing note elements
84 Insets insets = getInsets();
85 for (int i = 0; i < track.size(); i++) {
87 if (note instanceof MooNote) {
88 // Adds the note element to the note area.
89 MooNote mn = (MooNote)note;
90 elem = new MooNoteElement(mn);
93 // Moves the note element to the appropriate place.
95 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
97 // height = (mn.getDuration() / 24) * NOTE_HEIGHT;
98 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
99 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
102 if (viewLength < (y + height)) viewLength = y + height;
104 // while(findComponentAt(x, y) instanceof MooNoteElement ||
105 // findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
107 setPreferredSize(new Dimension(200, viewLength));
112 private boolean isOccupied(Rectangle r) {
113 Iterator it = rects.iterator();
114 while (it.hasNext()) {
115 if(r.intersects((Rectangle)it.next())) return true;
120 public void paintComponent(Graphics g) {
121 super.paintComponent(g);
122 Graphics2D g2 = (Graphics2D)g;
123 for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
124 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
125 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
126 g2.setColor(Color.gray);
133 class PopupListener extends MouseAdapter {
134 public void mousePressed(MouseEvent e) {
138 public void mouseReleased(MouseEvent e) {
142 private void maybeShowPopup(MouseEvent e) {
143 if (e.isPopupTrigger()) {
144 popup.show(e.getComponent(), e.getX(), e.getY());