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;
18 <<<<<<< MooTrackView.java
19 //private Rectangle box2;
23 public MooTrackView () {
25 private JPopupMenu popup;
26 private JMenuItem menuItem;
27 private ArrayList rects;
28 protected static int viewLength = 0;
29 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
31 public MooTrackView (Track track) {
34 <<<<<<< MooTrackView.java
36 setLayout(new BorderLayout());
37 <<<<<<< MooTrackView.java
38 this.setBorder(BorderFactory.createLineBorder(Color.black));
39 add(trackTitle(), BorderLayout.NORTH);
40 add(noteView(), BorderLayout.CENTER);
43 private JPanel trackTitle () {
44 title = new MooTrackTitle();
45 title.setPreferredSize(new Dimension(PANEL_WIDTH, TITLE_HEIGHT));
46 title.setBorder(BorderFactory.createLineBorder(Color.black));
50 private JPanel noteView () {
51 notes = new NoteArea();
52 notes.setBackground(Color.white);
53 notes.setBorder(BorderFactory.createLineBorder(Color.black));
57 notes = new NoteArea(track);
58 notes.setBackground(Color.white);
59 notes.setBorder(BorderFactory.createLineBorder(Color.black));
63 setBackground(Color.white);
64 setBorder(BorderFactory.createLineBorder(Color.black));
66 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
68 // Creates temporary variables
71 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
73 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
74 rects = new ArrayList(track.size() / 2);
76 // Places note elements
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(this, mn);
86 // Moves the note element to the appropriate place.
88 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
89 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
90 if (height == 0) height = NOTE_HEIGHT;
91 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
92 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
95 if (viewLength < (y + height)) viewLength = y + height;
97 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
102 // Creates pop-up menu.
103 popup = new JPopupMenu();
104 menuItem = new JMenuItem("Add...");
105 // menuItem.addActionListener();
108 addMouseListener(new PopupListener());
111 public Track getTrack() {
117 * Updates the track view.
119 public void update(long tickPosition) {
124 <<<<<<< MooTrackView.java
125 class NoteArea extends JPanel {
126 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
127 private ArrayList rects;
129 public NoteArea(Track track) {
133 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
135 // Creating temporary variables
139 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
140 rects = new ArrayList(track.size() / 2);
142 // Placing note elements
143 Insets insets = getInsets();
144 for (int i = 0; i < track.size(); i++) {
146 if (note instanceof MooNote) {
147 // Adds the note element to the note area.
148 MooNote mn = (MooNote)note;
149 elem = new MooNoteElement(mn);
152 // Moves the note element to the appropriate place.
154 y = insets.top + (int)(mn.getTick() / beatsPerSixteenth) * NOTE_HEIGHT;
155 height = (mn.getDuration() / beatsPerSixteenth) * NOTE_HEIGHT;
156 if (height == 0) height = NOTE_HEIGHT;
157 Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
158 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
161 if (viewLength < (y + height)) viewLength = y + height;
163 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength));
168 private boolean isOccupied(Rectangle r) {
169 Iterator it = rects.iterator();
170 while (it.hasNext()) {
171 if(r.intersects((Rectangle)it.next())) return true;
176 public void paintComponent(Graphics g) {
177 super.paintComponent(g);
178 Graphics2D g2 = (Graphics2D)g;
179 <<<<<<< MooTrackView.java
180 for (int c=0;c<1000;c=c+20) {
182 for (r=0;r<200;r=r+20) {
183 box = new Rectangle(r,c,20,20);
184 g2.setColor(Color.gray);
191 private boolean isOccupied(Rectangle r) {
192 Iterator it = rects.iterator();
193 while (it.hasNext()) {
194 if(r.intersects((Rectangle)it.next())) return true;
199 public void remove(MooNoteElement elem) {
200 remove((Component)elem);
204 public void paintComponent(Graphics g) {
205 super.paintComponent(g);
206 Graphics2D g2 = (Graphics2D)g;
207 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
208 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
209 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
210 g2.setColor(Color.gray);
217 private static final int PANEL_WIDTH = 65;
218 private static final int TITLE_HEIGHT = 40;
219 private static final int NOTEVIEW_HEIGHT = 200;
222 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
223 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
224 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
225 g2.setColor(Color.gray);
232 class PopupListener extends MouseAdapter {
233 public void mousePressed(MouseEvent e) {
234 if (e.isPopupTrigger()) {
235 popup.show(e.getComponent(), e.getX(), e.getY());
239 <<<<<<< MooTrackView.java