From c83e74facf762222fe4578f175408cc50d360518 Mon Sep 17 00:00:00 2001 From: Einar Pehrson Date: Sun, 11 May 2003 23:41:47 +0000 Subject: [PATCH] Fixed some bugs --- MooGUI.java | 22 +++++++------ MooMenu.java | 15 ++------- MooNoteElement.java | 2 +- MooToolbar.java | 77 +++++++++++++++++++++++++-------------------- MooTrackView.java | 19 +++++++---- MooView.java | 45 +++++++++++++------------- MooViewCounter.java | 16 +++++++++- Moosique.java | 26 ++++----------- To Do.txt | 17 ++++++++-- 9 files changed, 130 insertions(+), 109 deletions(-) diff --git a/MooGUI.java b/MooGUI.java index 146ca6d..32285ac 100644 --- a/MooGUI.java +++ b/MooGUI.java @@ -39,7 +39,7 @@ public class MooGUI extends JFrame { pane.add(toolbar, BorderLayout.NORTH); // Adds main view. - view = new MooView(seq); + view = new MooView(seq.getTracks()); pane.add(view, BorderLayout.CENTER); // Adds status bar. @@ -55,18 +55,22 @@ public class MooGUI extends JFrame { statusBar.setBackground(bgColor); view.setBackground(bgColor); + // Sets up global key listener ActionMap am = getRootPane().getActionMap(); Action playAction = new AbstractAction() { public void actionPerformed(ActionEvent ae) { - Moosique.resumepause(); + if (!Moosique.getSequencer().isRunning()) { + Moosique.play(); + } else { + Moosique.stop(); + } }}; - am.put("play", playAction); + am.put("Play", playAction); InputMap im = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); KeyStroke playKey = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0); - im.put(playKey, "play"); - + im.put(playKey, "Play"); // Configures window. addWindowListener(new MooGUICloser()); @@ -94,7 +98,7 @@ public class MooGUI extends JFrame { */ public void setSequence(Sequence sequence) { seq = sequence; - view.setSequence(seq); + view.setTracks(seq.getTracks()); } /** @@ -106,12 +110,12 @@ public class MooGUI extends JFrame { } /** - * Update the view. + * Calls on the main view to update the track views, + * and on the toolbar to update the progress indicator. */ public void update(){ view.update(); - // Calls on the toolbar to update the progress indicator. - //toolbar.updateProgInd(); + toolbar.updateProgInd(); } class MooGUICloser extends WindowAdapter { diff --git a/MooMenu.java b/MooMenu.java index 2403a37..141fd93 100644 --- a/MooMenu.java +++ b/MooMenu.java @@ -141,7 +141,6 @@ public class MooMenu extends JMenuBar implements ActionListener { if(returnVal == JFileChooser.APPROVE_OPTION && isMidiFile(chooser.getSelectedFile())) { Moosique.saveAs(chooser.getSelectedFile().getAbsolutePath()); } - } else if (command == "Exit") { Moosique.quit(); } else if (command == "Copy") { @@ -157,21 +156,13 @@ public class MooMenu extends JMenuBar implements ActionListener { } else if (command == "Preferences...") { } else if (command == "Play") { - if (Moosique.getSequencer().isRunning()) { - Moosique.pause(); - } else { - Moosique.play(); - } + if (!Moosique.getSequencer().isRunning()) Moosique.play(); } else if (command == "Pause") { - if (Moosique.getSequencer().isRunning()) { - Moosique.resume(); - } else { - Moosique.pause(); - } + if (Moosique.getSequencer().isRunning()) Moosique.pause(); } else if (command == "Stop") { Moosique.stop(); } else if (command == "Jump...") { - + } else if (command == "Add track...") { Moosique.getSequence().createTrack(); } else if (command == "Delete track...") { diff --git a/MooNoteElement.java b/MooNoteElement.java index c39d2b5..e854a44 100644 --- a/MooNoteElement.java +++ b/MooNoteElement.java @@ -9,7 +9,7 @@ import java.awt.event.*; * @version 1 */ -public class MooNoteElement extends JPanel{ +public class MooNoteElement extends JPanel { private MooNote note; private boolean selected; diff --git a/MooToolbar.java b/MooToolbar.java index a2f5571..c1244f5 100644 --- a/MooToolbar.java +++ b/MooToolbar.java @@ -11,7 +11,7 @@ import javax.sound.midi.*; public class MooToolbar extends JToolBar { private JButton rewind, playpause, stop, fastforward; - private JLabel measure, beats, ticks, measurevalue, beatsvalue, ticksvalue; + private JLabel measure, beats, ticks, measureValue, beatsValue, ticksValue; private ImageIcon playIcon, pauseIcon; private MooMouseAdapter mouseAdapter; public static final Color bgColor = new Color(192, 224, 255); @@ -21,50 +21,64 @@ public class MooToolbar extends JToolBar { */ public MooToolbar() { + // setAlignmentX(LEFT_ALIGNMENT); setFloatable(false); mouseAdapter = new MooMouseAdapter(); // Creates playback buttons rewind = createButton("images/rewind.gif", "Rewind"); - add(rewind); playpause = createButton("images/play.gif", "Play"); - add(playpause); + stop = createButton("images/stop.gif", "Stop"); + fastforward = createButton("images/forward.gif", "Fast forward"); playIcon = new ImageIcon("images/play.gif"); pauseIcon = new ImageIcon("images/pause.gif"); - stop = createButton("images/stop.gif", "Stop"); + + // Adds playback buttons + add(rewind); + add(playpause); add(stop); - fastforward = createButton("images/forward.gif", "Fast forward"); add(fastforward); // Creates progress indicator + measure = createLabel("Msr", 10); + beats = createLabel("Beat", 10); + ticks = createLabel("Tick", 10); + measureValue = createLabel("1", 16); + beatsValue = createLabel("1", 16); + ticksValue = createLabel("1", 16); + JPanel spacenorth = new JPanel(); + spacenorth.setBackground(bgColor); + JPanel spacesouth = new JPanel(); + spacesouth.setBackground(bgColor); + + // Creates progress indicator panel and adds components JPanel progIndPanel = new JPanel(); progIndPanel.setMaximumSize(new Dimension(120,27)); progIndPanel.setLayout(new GridLayout(2,4)); - measure = createLabel("Mrs",10); - beats = createLabel("Beat",10); - ticks = createLabel("Tick",10); - measurevalue = createLabel("1", 16); - beatsvalue = createLabel("1",16); - ticksvalue = createLabel("1",16); - - JPanel spacenorth = new JPanel(); - spacenorth.setBackground(bgColor); progIndPanel.add(spacenorth); - progIndPanel.add(measure); progIndPanel.add(beats); progIndPanel.add(ticks); - - JPanel spacesouth = new JPanel(); - spacesouth.setBackground(bgColor); progIndPanel.add(spacesouth); - - progIndPanel.add(measurevalue); - progIndPanel.add(beatsvalue); - progIndPanel.add(ticksvalue); + progIndPanel.add(measureValue); + progIndPanel.add(beatsValue); + progIndPanel.add(ticksValue); add(progIndPanel); + } - + + /** + * Updates the progress indicator. + */ + public void updateProgInd() { + int pos = Moosique.getSequencer().getTickPosition(); + int ticksPerBeat = Moosique.getSequencer().getResolution(); + int beatsPerMeasure = 4; + measureValue.setText(pos / (beatsPerMeasure * ticksPerBeat)); + beatsValue.setText((pos - measures * beatsPerMeasure * ticksPerBeat) / ticksPerBeat); + ticksValue.setText(pos - measures * beatsPerMeasure * ticksPerBeat - beats * ticksPerBeat); + } + /** * Creates a button with the specified image and tooltip. * @param imagelocation the imagelacation of the the image displayed in the button @@ -84,17 +98,12 @@ public class MooToolbar extends JToolBar { * @param fontsize the fontzise of the text displayed on the label */ private JLabel createLabel(String title, int fontSize){ - JLabel value = new JLabel(title,JLabel.CENTER); - value.setFont(new Font("Times New Roman", Font.PLAIN, fontSize)); - value.setBorder(BorderFactory.createLineBorder(Color.black)); - return value; + JLabel label = new JLabel(title,JLabel.CENTER); + label.setFont(new Font("Times New Roman", Font.PLAIN, fontSize)); + label.setBorder(BorderFactory.createLineBorder(Color.black)); + return label; } - - // int pos = Moosique.getSequencer().getTickPosition(); - // int measures = pos / (4 * Moosique.getSequence.get) - // int beats = (pos - measures * 4 * 96) / 96; - // int ticks = (pos - measures*4*96 - beats*96); - + class MooMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e) { if (((JButton)e.getSource()).getToolTipText() == "Play") { @@ -126,4 +135,4 @@ public class MooToolbar extends JToolBar { public void mouseReleased(MouseEvent e) {} } -} +} \ No newline at end of file diff --git a/MooTrackView.java b/MooTrackView.java index bf6e31f..4e4f002 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -29,7 +29,7 @@ public class MooTrackView extends JPanel implements ActionListener { title.setBorder(BorderFactory.createLineBorder(Color.black)); add(title, BorderLayout.NORTH); - notes = new NoteArea(); + notes = new NoteArea(track); notes.setBackground(Color.white); notes.setBorder(BorderFactory.createLineBorder(Color.black)); @@ -52,16 +52,23 @@ public class MooTrackView extends JPanel implements ActionListener { } class NoteArea extends JPanel { - public void RectanglePanel() { - setPreferredSize(new Dimension(20, 20)); + + public NoteArea(Track track) { + MidiEvent note; + for (int i = 0; i < track.size(); i++) { + note = track.get(i); + if (note instanceof MooNote) { + add(new MooNoteElement((MooNote)note)); + } + } + validate(); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (int c = 0; c < 1000; c += 20) { - int r=0; - for (r = 0; r < 200; r += 20) { + for (int r = 0; r < 200; r += 20) { box = new Rectangle(r, c, 20, 20); g2.setColor(Color.gray); g2.draw(box); @@ -85,4 +92,4 @@ public class MooTrackView extends JPanel implements ActionListener { } } } -} +} \ No newline at end of file diff --git a/MooView.java b/MooView.java index 4f36de3..76391f9 100644 --- a/MooView.java +++ b/MooView.java @@ -11,61 +11,58 @@ import java.awt.event.*; public class MooView extends JScrollPane { - private Track[] tracks; - private MooTrackView[] trackViews; private JPanel trackPanel; /** * Creates the main view */ - public MooView(Sequence seq) { + public MooView(Track[] tracks) { super(VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_AS_NEEDED); - tracks = seq.getTracks(); - - trackPanel = new JPanel(new GridLayout(1,3), true); - createTrackViews(); + setTracks(tracks); setViewportView(trackPanel); } /** * Fills the track panel with track views for all tracks in the current sequence. + * @param tracks the tracks for which to add views */ - private void createTrackViews() { + public void setTracks(Track[] tracks) { trackPanel.removeAll(); ((GridLayout)trackPanel.getLayout()).setColumns(tracks.length); - trackViews = new MooTrackView[tracks.length]; for (int i = 0; i < tracks.length; i++) { - trackViews[i] = new MooTrackView(tracks[i]); trackPanel.add(new MooTrackView(tracks[i])); } trackPanel.validate(); + validate(); } - public void setSequence(Sequence seq) { - tracks = seq.getTracks(); - createTrackViews(); - } - + /** + * Calls on each track view to update itself. + */ public void update() { - // Calls on each track view to update itself. - for (int i = 0; i < trackViews.length; i++) { - //trackViews[i].update(); + Component[] comps = c.getComponents(); + for (int i = 0; i < comps.length; i++) { + ((MooTrackView)comps[i]).update(); } } + /** * Creates a view for the given track and adds it to the main view. * @param track the track for which to add a view + * @param index the index at which to insert the view */ - public void addTrackView(Track track) { - + public void addTrackView(Track track, int index) { + add(new MooTrackView(track), index); + validate(); } /** * Removes the view for the given track. - * @param track the track for which to remove the view + * @param index the index of the track for which to remove the view */ - public void removeTrackView(Track track) { - + public void removeTrackView(int index) { + remove(index); + validate(); } -} +} \ No newline at end of file diff --git a/MooViewCounter.java b/MooViewCounter.java index aea7944..ce69af2 100644 --- a/MooViewCounter.java +++ b/MooViewCounter.java @@ -1,4 +1,5 @@ import javax.swing.*; +import java.awt.*; /** * @@ -7,12 +8,25 @@ import javax.swing.*; * @version 1 */ -public class MooViewCounter { +public class MooViewCounter extends JPanel { /** * Creates */ public MooViewCounter () { + + } + + public void paintComponent(Graphics g) { + super.paintComponent(g); + if (!(g instanceof Graphics2D)) return; + Graphics2D g2 = (Graphics2D)g; + + for (int i = 0; i < 10; i++) { + for (int j = 0; j < 10; j++) { + // g2.drawLine(0,i*20, + } + } } } diff --git a/Moosique.java b/Moosique.java index 5ee3bcc..5d6ed97 100644 --- a/Moosique.java +++ b/Moosique.java @@ -1,6 +1,6 @@ import javax.sound.midi.*; -import javax.swing.*; import java.io.*; +import javax.swing.*; /** * Moosique - The MIDI Tracker @@ -22,7 +22,6 @@ public class Moosique { private static String filename, fileArg; private static long position; private static boolean makeGUI = true; - private static boolean playing = false; /** * Starts the application. @@ -68,9 +67,12 @@ public class Moosique { clearSequence(); } - // If n-flag is set, plays song and then exits. Otherwise builds GUI. + // Builds GUI, unless n-flag is set. if (makeGUI) { System.out.print("Building GUI..."); + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) {} gui = new MooGUI(seq); System.out.println("Done"); } else { @@ -147,7 +149,6 @@ public class Moosique { * Starts playback of the current sequence. */ public static void play() { - playing = true; sequencer.setTickPosition(position); sequencer.start(); } @@ -156,7 +157,6 @@ public class Moosique { * Pauses playback of the current sequence. */ public static void pause() { - playing = false; sequencer.stop(); } @@ -164,7 +164,6 @@ public class Moosique { * Resumes playback of the current sequence. */ public static void resume() { - playing = true; sequencer.start(); } @@ -172,22 +171,10 @@ public class Moosique { * Stops playback of the current sequence. */ public static void stop() { - playing = false; sequencer.stop(); sequencer.setTickPosition(position); } - /** - * Pauses if playing and resumes if stopped. - */ - public static void resumepause() - { - if (playing) - pause(); - else - resume(); - } - /** * Rewinds the current sequence the given number of measures. * @param measures the number of measures to rewind @@ -232,7 +219,6 @@ public class Moosique { } catch (InvalidMidiDataException e) { return false; } catch (IOException e) { - JOptionPane.showMessageDialog(null, "Error 404", "File Not Found", JOptionPane.ERROR_MESSAGE); return false; } @@ -304,4 +290,4 @@ public class Moosique { if (synthesizer.isOpen()) synthesizer.close(); System.exit(0); } -} +} \ No newline at end of file diff --git a/To Do.txt b/To Do.txt index b5f64bb..8d026dc 100644 --- a/To Do.txt +++ b/To Do.txt @@ -56,8 +56,7 @@ Track MooMenu - * Inställningar - - MIDI-enhet Öppna en dialogruta med innehållet i getMidiDeviceInfo() och låt användaren välja. +x Kom ihåg sökväg vid Open Musikrelaterade menyer i Midisoft Recording Session: @@ -147,6 +146,20 @@ Moosique * getPosition och setPosition - kvar? + +Skräp + + // Prints the number of notes in each track + int count; + Track[] tracks = seq.getTracks(); + for (int k = 0; k < tracks.length; k++) { + count = 0; + for (int j = 0; j < tracks[k].size(); j++) { + if (tracks[k].get(j) instanceof MooNote) count++; + } + System.out.println("Track " + k + ": " + count + " notes."); + } + MooNoteProp * textfält som gör att man bara kan skriva in siffror? -- 2.39.2