From 3895c924f1ebe07d2def43568dce002d9589f821 Mon Sep 17 00:00:00 2001 From: Einar Pehrson Date: Tue, 13 May 2003 00:14:07 +0000 Subject: [PATCH] FIXED THE MULTICOLUMN BUG!!! How? findComponentAt(x,y) couldn't return Containers, i.e. ALL JComponents. Instead, stored coordinates of all placed panels, and checked for intersections. Added a progress indicator for loading files. Kinda nice. --- MooTrackView.java | 31 +++++++++++++++++++++++-------- MooView.java | 3 ++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/MooTrackView.java b/MooTrackView.java index 864ed07..5efb002 100644 --- a/MooTrackView.java +++ b/MooTrackView.java @@ -2,6 +2,7 @@ import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.sound.midi.*; +import java.util.*; /** * Graphical representation of a MIDI track. @@ -65,6 +66,7 @@ public class MooTrackView extends JPanel implements ActionListener { public static final int NOTE_HEIGHT = 10; public static final int NOTE_WIDTH = 40; private int trackLength; + private ArrayList rects; public NoteArea(Track track) { // Configuring panel @@ -72,10 +74,11 @@ public class MooTrackView extends JPanel implements ActionListener { trackLength = 140; setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT)); - // Temporary variables + // Creating temporary variables MidiEvent note; MooNoteElement elem; int x, y, height; + rects = new ArrayList(track.size() / 2); // Placing note elements Insets insets = getInsets(); @@ -90,17 +93,29 @@ public class MooTrackView extends JPanel implements ActionListener { // Moves the note element to the appropriate place. x = insets.left; y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT; - height = (mn.getDuration() / 24) * NOTE_HEIGHT; - if (height == 0) height = NOTE_HEIGHT; - while(findComponentAt(x, y) instanceof MooNoteElement || - findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH; - elem.setBounds(x, y, NOTE_WIDTH, height); + height = NOTE_HEIGHT; + // height = (mn.getDuration() / 24) * NOTE_HEIGHT; + Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height); + while(isOccupied(r)) r.translate(NOTE_WIDTH, 0); + elem.setBounds(r); + rects.add(r); if (viewLength < (y + height)) viewLength = y + height; + + // while(findComponentAt(x, y) instanceof MooNoteElement || + // findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH; } - setPreferredSize(new Dimension(200,viewLength)); + setPreferredSize(new Dimension(200, viewLength)); } validate(); } + + private boolean isOccupied(Rectangle r) { + Iterator it = rects.iterator(); + while (it.hasNext()) { + if(r.intersects((Rectangle)it.next())) return true; + } + return false; + } public void paintComponent(Graphics g) { super.paintComponent(g); @@ -130,4 +145,4 @@ public class MooTrackView extends JPanel implements ActionListener { } } } -} +} \ No newline at end of file diff --git a/MooView.java b/MooView.java index 1155b7a..9ee52b3 100644 --- a/MooView.java +++ b/MooView.java @@ -47,7 +47,7 @@ public class MooView extends JPanel { titlePanel.add(new MooTrackTitle(tracks[0])); } else { // Creates dialog for progress bar. - JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing MIDI file...", false); + JDialog progressDialog = new JDialog(Moosique.getGUI(), "Visualizing...", false); JProgressBar progressBar = new JProgressBar(0, tracks.length); progressBar.setValue(0); progressBar.setStringPainted(true); @@ -67,6 +67,7 @@ public class MooView extends JPanel { progressDialog.dispose(); } trackPanel.validate(); + trackViews.setViewportView(trackPanel); } /** -- 2.39.2