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 {
17 private Rectangle box;
19 private JPopupMenu popup;
20 private JMenuItem menuItem;
21 private ArrayList rects;
22 protected static int viewLength = 0;
23 protected static int extraHeight = 0;
24 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
26 public MooTrackView (Track track) {
30 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
32 setBackground(Color.white);
33 setBorder(BorderFactory.createLineBorder(Color.black));
35 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
37 // Creates temporary variables
40 rects = new ArrayList(track.size() / 2);
42 // Places note elements
43 for (int i = 0; i < track.size(); i++) {
45 if (note instanceof MooNote) {
46 // Adds the note element to the note area.
47 MooNote mn = (MooNote)note;
48 elem = new MooNoteElement(this, mn);
51 layoutElement(elem,false);
53 // Moves the note element to the appropriate place.
55 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
60 // Creates pop-up menu.
61 popup = new JPopupMenu();
62 menuItem = new JMenuItem("Add...");
63 // menuItem.addActionListener();
66 addMouseListener(new PopupListener());
69 public void layoutElement(MooNoteElement elem, boolean old){
70 Rectangle r = new Rectangle();
72 r = elem.getBounds(r);
73 for (Iterator i = rects.iterator(); i.hasNext();){
82 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
83 MooNote mn = elem.getNote();
84 Insets insets = getInsets();
87 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
88 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
89 if (height == 0) height = NOTE_HEIGHT;
90 r = new Rectangle(x, y, NOTE_WIDTH, height);
91 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
94 if (viewLength < (y + height)){
95 viewLength = y + height;
96 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
101 public Track getTrack() {
107 * Updates the track view.
109 public void update(long tickPosition) {
113 private boolean isOccupied(Rectangle r) {
114 Iterator it = rects.iterator();
115 while (it.hasNext()) {
116 if(r.intersects((Rectangle)it.next())) return true;
121 public void remove(MooNoteElement elem) {
122 remove((Component)elem);
123 elem.getNote().removeFrom(track);
128 public void paintComponent(Graphics g) {
129 super.paintComponent(g);
130 Graphics2D g2 = (Graphics2D)g;
131 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
132 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
133 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
134 g2.setColor(Color.gray);
140 class PopupListener extends MouseAdapter {
141 public void mousePressed(MouseEvent e) {
142 if (e.isPopupTrigger()) {
143 popup.show(e.getComponent(), e.getX(), e.getY());