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 JPopupMenu notePopup;
22 private JMenuItem menuItem;
23 protected static int viewLength = 0;
24 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
26 public MooTrackView (Track track) {
29 //setPreferredSize(new Dimension(200, 9000));
30 setLayout(new BorderLayout());
31 // setBorder(BorderFactory.createLineBorder(Color.black));
33 //title = new MooTrackTitle(track);
34 //title.setBorder(BorderFactory.createLineBorder(Color.black));
35 //add(title, BorderLayout.NORTH);
37 notes = new NoteArea(track);
38 notes.setBackground(Color.white);
39 notes.setBorder(BorderFactory.createLineBorder(Color.black));
41 popup = new JPopupMenu();
42 menuItem = new JMenuItem("Add...");
43 menuItem.addActionListener(this);
47 notePopup = new JPopupMenu();
48 menuItem = new JMenuItem("Preferences...");
49 menuItem.addActionListener(this);
50 notePopup.add(menuItem);
52 notes.addMouseListener(new PopupListener());
53 add(notes, BorderLayout.CENTER);
56 public void actionPerformed(ActionEvent e) {}
58 public Track getTrack() {
64 * Updates the track view.
66 public void update(long tickPosition) {
70 class NoteArea extends JPanel {
71 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
72 private ArrayList rects;
74 public NoteArea(Track track) {
78 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
80 // Creating temporary variables
84 rects = new ArrayList(track.size() / 2);
86 // Placing note elements
87 Insets insets = getInsets();
88 for (int i = 0; i < track.size(); i++) {
90 if (note instanceof MooNote) {
91 // Adds the note element to the note area.
92 MooNote mn = (MooNote)note;
93 elem = new MooNoteElement(mn);
96 // Moves the note element to the appropriate place.
98 y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
99 height = (mn.getDuration() / 24) * NOTE_HEIGHT;
100 if (height == 0) height = NOTE_HEIGHT;
101 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
102 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
105 if (viewLength < (y + height)) viewLength = y + height;
107 setPreferredSize(new Dimension(VIEW_WIDTH, 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 < getHeight(); 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 if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
145 notePopup.show(e.getComponent(), e.getX(), e.getY());
147 popup.show(e.getComponent(), e.getX(), e.getY());