Results 1 to 2 of 2
Thread: paintComponent is not working
- 12-18-2009, 04:38 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 1
- Rep Power
- 0
paintComponent is not working
When I want my program to repaint, it's not displaying the visualization. What's wrong with it? I'm also using midi files so I guess you can grab any midi files. I can't link you to where I got them because I'm not allowed to post links..
Java Code:import java.applet.Applet; import java.applet.AudioClip; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.util.*; import java.net.*; import java.io.*; import javax.sound.midi.*; import javax.swing.*; import javax.swing.Timer; import javax.swing.border.LineBorder; public class Music extends Applet { static Music music; Vector notes = new Vector(); static Sequence seq; Sequencer sequencer; Timer timer; int WIDTH = 800; int HEIGHT = 600; double max = 0; double min = HEIGHT; boolean[] visible; VolatileImage buffer; Image background; Graphics g; double time; Vector v; Rectangle r; static double zoom = 10.0; Color[] colors = { Color.red, Color.yellow, Color.green, Color.blue, Color.cyan, Color.magenta, Color.orange, Color.pink }; private Button start = new Button("Start"); private Button pause = new Button("Pause"); private Button stop = new Button("Stop"); private JComboBox midiFiles = new JComboBox(new Object[] { "Moonlight Sonata", "Cantina", "Brahms" }); private AudioClip currentAudioClip; private AudioClip[] audioClips = new AudioClip[3]; private int current = 0; /** * Main method: takes midi file parameter from commandline, starts applet * * @param args */ public static void main(String[] args) { try { seq = MidiSystem.getSequence(new File(args[0])); if (args.length > 1) zoom = Double.parseDouble(args[1]); }// try catch (Exception e) { e.printStackTrace(); System.exit(0); }// catch music = new Music(); final JFrame frame = new JFrame(); frame.getContentPane().setLayout(new BorderLayout()); frame.add(music, BorderLayout.CENTER); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); music.go(); }// main public Music() { add(new Panel()); add(start); add(pause); add(stop); add(midiFiles); } public void init() { audioClips[0] = Applet.newAudioClip(getClass().getResource( "MoonlightSonata3.mid")); audioClips[1] = Applet.newAudioClip(getClass().getResource( "cantina.mid")); audioClips[2] = Applet.newAudioClip(getClass() .getResource("brahms.mid")); start.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { if (currentAudioClip == audioClips[0]) { seq = MidiSystem.getSequence(new File( "MoonlightSonata3.mid")); } else if (currentAudioClip == audioClips[1]) { seq = MidiSystem.getSequence(new File("cantina.mid")); } else if (currentAudioClip == audioClips[2]) { seq = MidiSystem.getSequence(new File("brahms.mid")); } // currentAudioClip.play(); music = new Music(); music.go(); music.everythingStart(); } catch (InvalidMidiDataException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); pause.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { music.everythingStop(); } }); stop.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { music.everythingStop(); } }); midiFiles.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { stop(); current = midiFiles.getSelectedIndex(); presentMidi(current); } }); currentAudioClip = audioClips[0]; } private void presentMidi(int index) { midiFiles.setSelectedIndex(index); currentAudioClip = audioClips[index]; } public void start() { } public void stop() { } /** * Starts playback of created midi sequence */ public void go() { for (int j = 0; j < seq.getTracks().length; j++) { Vector v = new Vector(); Track t = seq.getTracks()[j]; System.out.println(t.ticks() + " " + t.size()); RenderListener rt = new RenderListener(this); timer = new Timer(100, rt); MidiMessage m; ShortMessage s; MidiEvent e; Rectangle r; Point[] p = new Point[255]; for (int i = 0; i < p.length; i++) { p[i] = new Point(-1, -1); }// for for (int i = 0; i < t.size(); i++) { e = t.get(i); m = e.getMessage(); if (m instanceof ShortMessage) { s = (ShortMessage) m; if (s.getCommand() == 144 || s.getCommand() == 128) { if (p[s.getData1()].x == -1) { p[s.getData1()].x = (int) (e.getTick() / zoom); }// if else { r = new Rectangle( p[s.getData1()].x, s.getData1() * 5, (int) (e.getTick() / zoom - p[s.getData1()].x), 10); v.add(r); if (r.y < min) min = r.y; if (r.y > max) max = r.y; // System.out.println("added"); p[s.getData1()].x = -1; }// else }// if }// if }// for if (v.size() > 0) { notes.add(v); }// if }// for visible = new boolean[notes.size()]; Arrays.fill(visible, true); try { // starts actual playback. Note that timer must be created and // started *before* music starts playing sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(seq); sequencer.setTempoFactor(1.0f); timer.start(); sequencer.start(); }// try catch (Exception e) { e.printStackTrace(); System.exit(0); }// catch }// start public void everythingStop() { timer.stop(); sequencer.stop(); } public void everythingStart() { timer.start(); sequencer.start(); } class RenderListener implements ActionListener { Music parent; RenderListener(Music m) { parent = m; } public void actionPerformed(ActionEvent ev) { //repaint(); if (sequencer == null || !sequencer.isRunning()) return; time = sequencer.getTickPosition(); time /= zoom; //repaint(); parent.repaint(); //parent.render(); } } public class Panel extends JPanel { protected void paintComponent(Graphics g) { super.paintComponent(g); if (buffer == null) { buffer = createVolatileImage(WIDTH, HEIGHT); g = buffer.getGraphics(); }// if g.setColor(Color.black); g.fillRect(0, 0, WIDTH, HEIGHT); for (int i = 0; i < notes.size(); i++) { v = (Vector) (notes.elementAt(i)); for (int j = 0; j < v.size(); j++) { r = (Rectangle) (v.elementAt(j)); if ((r.x - time) > WIDTH / 2 + 200) { break; }// if if ((r.x - time + r.width) < -WIDTH / 2) { v.removeElementAt(j); }// if else if (visible[i]) { if (r.x - time <= 0 && r.x - time + r.width >= 0) { g.setColor(Color.white); }// if else { g.setColor(colors[i % colors.length]); }// else g.fillOval((int) ((r.x - time) + WIDTH / 2), (int) (HEIGHT - r.y), (int) (r.width), (int) r.height); }// else }// for }// for getGraphics().drawImage(buffer, 0, 0, this); } } public Dimension getPreferredSize() { return new Dimension(WIDTH, HEIGHT); } }// Music
- 12-18-2009, 04:48 AM #2
Member
- Join Date
- Sep 2009
- Location
- http://www.kouje.com
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
paint vs paintComponent for a JPanel
By lightstream in forum AWT / SwingReplies: 4Last Post: 01-29-2009, 03:26 AM -
Working around paintcomponent
By sahhhm in forum New To JavaReplies: 2Last Post: 05-16-2008, 03:43 AM -
Problem going outside paintComponent
By Thez in forum Java 2DReplies: 9Last Post: 12-08-2007, 05:59 PM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 11:36 PM -
paint() and paintComponent()
By goldhouse in forum Java 2DReplies: 1Last Post: 07-17-2007, 04:43 AM
Bookmarks