]> ruin.nu Git - moosique.git/blob - MooTrackView.java
*** empty log message ***
[moosique.git] / MooTrackView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5 import java.util.*;
6
7 /**
8  * Graphical representation of a MIDI track.
9  * 
10  * @author  Andersson , Andreen, Lanneskog, Pehrson
11  * @version 1
12  */
13
14 public class MooTrackView extends JPanel {
15
16         private Track track;
17         private Rectangle box;
18 <<<<<<< MooTrackView.java
19         //private Rectangle box2;
20         /** 
21          * Creates 
22          */
23         public MooTrackView () {
24 =======
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;
30
31         public MooTrackView (Track track) {
32                 super(true);
33                 this.track = track;
34 <<<<<<< MooTrackView.java
35 >>>>>>> 1.45
36                 setLayout(new BorderLayout());
37 <<<<<<< MooTrackView.java
38                 this.setBorder(BorderFactory.createLineBorder(Color.black));
39                 add(trackTitle(), BorderLayout.NORTH);
40                 add(noteView(), BorderLayout.CENTER);
41                 }
42         
43         private JPanel trackTitle () {
44                 title = new MooTrackTitle();
45                 title.setPreferredSize(new Dimension(PANEL_WIDTH, TITLE_HEIGHT));
46                 title.setBorder(BorderFactory.createLineBorder(Color.black));
47                 return title;
48         }
49
50         private JPanel noteView () {
51                 notes = new NoteArea(); 
52                 notes.setBackground(Color.white);
53                 notes.setBorder(BorderFactory.createLineBorder(Color.black));   
54                 return notes;           
55 =======
56
57                 notes = new NoteArea(track);    
58                 notes.setBackground(Color.white);
59                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
60 =======
61
62                 // Configures panel
63                 setBackground(Color.white);
64                 setBorder(BorderFactory.createLineBorder(Color.black));
65                 setLayout(null);
66                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
67
68                 // Creates temporary variables
69                 MidiEvent note;
70                 MooNoteElement elem;
71                 int extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
72                 int x, y, height;
73                 int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
74                 rects = new ArrayList(track.size() / 2);
75
76                 // Places note elements
77                 Insets insets = getInsets();
78                 for (int i = 0; i < track.size(); i++) {
79                         note = track.get(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);
84                                 add(elem);
85
86                                 // Moves the note element to the appropriate place.
87                                 x = insets.left;
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);
93                                 elem.setBounds(r);
94                                 rects.add(r);
95                                 if (viewLength < (y + height)) viewLength = y + height;
96                         }
97                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
98                 }
99                 validate();
100 >>>>>>> 1.46
101
102                 // Creates pop-up menu.
103                 popup = new JPopupMenu();
104                 menuItem = new JMenuItem("Add...");
105                 // menuItem.addActionListener();
106                 popup.add(menuItem);
107
108                 addMouseListener(new PopupListener());
109         }
110
111         public Track getTrack() {
112                 return track;
113         }
114
115
116         /** 
117          * Updates the track view.
118          */
119         public void update(long tickPosition) {
120                 repaint();
121 >>>>>>> 1.45
122         }
123
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;
128
129                 public NoteArea(Track track) {
130                         // Configuring panel
131                         super(true);
132                         setLayout(null);
133                         setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
134
135                         // Creating temporary variables
136                         MidiEvent note;
137                         MooNoteElement elem;
138                         int x, y, height;
139                         int beatsPerSixteenth = Moosique.getSequence().getResolution() / 4;
140                         rects = new ArrayList(track.size() / 2);
141
142                         // Placing note elements
143                         Insets insets = getInsets();
144                         for (int i = 0; i < track.size(); i++) {
145                                 note = track.get(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);
150                                         add(elem);
151
152                                         // Moves the note element to the appropriate place.
153                                         x = insets.left;
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);
159                                         elem.setBounds(r);
160                                         rects.add(r);
161                                         if (viewLength < (y + height)) viewLength = y + height;
162                                 }
163                                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength));
164                         }
165                         validate();
166                 }
167                 
168                 private boolean isOccupied(Rectangle r) {
169                         Iterator it = rects.iterator();
170                         while (it.hasNext()) {
171                                 if(r.intersects((Rectangle)it.next())) return true;
172                         }
173                         return false;
174                 }
175
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) {
181                                 int r=0;
182                                 for (r=0;r<200;r=r+20) {
183                                         box = new Rectangle(r,c,20,20);
184                                         g2.setColor(Color.gray);
185                                         g2.draw(box);
186                                 }
187                         }
188                 }
189         }
190 =======
191         private boolean isOccupied(Rectangle r) {
192                 Iterator it = rects.iterator();
193                 while (it.hasNext()) {
194                         if(r.intersects((Rectangle)it.next())) return true;
195                 }
196                 return false;
197         }
198         
199         public void remove(MooNoteElement elem) {
200                 remove((Component)elem);
201                 validate();
202         }
203
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);
211                                 g2.draw(box);
212                         }
213                 }
214         }
215 >>>>>>> 1.46
216
217         private static final int PANEL_WIDTH = 65;
218         private static final int TITLE_HEIGHT = 40;
219         private static final int NOTEVIEW_HEIGHT = 200;
220 }
221 =======
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);
226                                         g2.draw(box);
227                                 }
228                         }
229                 }
230         }
231
232         class PopupListener extends MouseAdapter {
233                 public void mousePressed(MouseEvent e) {
234                         if (e.isPopupTrigger()) {
235                                 popup.show(e.getComponent(), e.getX(), e.getY());
236                         }
237                 }
238         }
239 <<<<<<< MooTrackView.java
240 }
241 >>>>>>> 1.45
242 =======
243 }>>>>>>> 1.46