Java :Painting Random Shapes on JFrame against Music Played

坚强是说给别人听的谎言 提交于 2019-12-11 08:15:06

问题


I am trying to paint JFrame when music is played , these shapes are created on random no calculation and when a there is some music played there pops up a JFrame and these shapes are painted there, problem is that when i run this code there is no musical sound and shapes drawn but just a frame pops up and nothing else , Plase check this code and help me to correct it ..

public class MusicBeatsDrawing {

    static JFrame frame; 
    DrawPanel dp =new DrawPanel();
    public static void main(String[] args)
    {
        MusicBeatsDrawing mbd= new MusicBeatsDrawing();
        mbd.go();

    }
    public void SetGui(){

        frame= new JFrame("Simple frame ; ");
        frame.setBounds(100, 100, 200, 200); 
        frame.setContentPane(dp);
        frame.setVisible(true);

    }

    public void go()
    {
        SetGui();
        try
        {
            Sequencer sequencer =  MidiSystem.getSequencer();
            sequencer.open(); 

            Sequence seq= new Sequence(Sequence.PPQ, 4);
            sequencer.addControllerEventListener(dp,new int [] {127});
            Track track =  seq.createTrack(); 
            int r  =  0; 
            for  (int  i  =  0; i  <  60 ; i+=  4) {

                r =  (int) Math.random()*50; 
                track.add(MakeEvent(144,1,r,100,i)); 
                track.add(MakeEvent(176,1,127,0,i)); 
                track.add(MakeEvent(128,1,r,100,i ));
            }
            sequencer.setSequence(seq);
            sequencer.start();
        }

        catch(Exception e)
        {e.printStackTrace(); }
    }

    public  MidiEvent  MakeEvent(int one , int two , int three , int four , int tick)
    {   
        MidiEvent event= null;
        try 
        {    
            ShortMessage sm= new ShortMessage();
            sm.setMessage(one,two, three, four);
            event= new MidiEvent(sm , tick );


        } catch (InvalidMidiDataException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return  event;   
    }
    class DrawPanel extends  JPanel implements ControllerEventListener
    {       
        boolean msg= false ; 


        public void paintComponent(Graphics g)
        {
            if(msg){ Graphics2D g2d= (Graphics2D) g; 

            int red= (int) (Math.random()*255); 
            int green= (int) (Math.random()*255) ;
            int blue= (int) (Math.random()*255); 

            g.setColor(new Color(red, green , blue));

            int height= (int)Math.random()*255;
            int width= (int)Math.random()*255;
            int x= (int)Math.random()*255;
            int y= (int)Math.random()*255;
            g.fillRect(height, width, x, y);        
            msg = false; 
            }
        }
        @Override
        public void controlChange(ShortMessage event) {
            msg= true; 
            repaint();          
        }}


}

回答1:


Swing GUI objects should be constructed and manipulated only on the event dispatch thread. With this change, your program works. A telltale symptom is that the program runs when you resize the frame, which causes the system to invoke repaint() repeatedly.

Addendum: Some additional issues merit attention,

  • Make setVisible() last.

  • The sequencer startup latency may be worth moving to the background.

  • Use named constants, e.g. ShortMessage.NOTE_ON, rather than magic numbers.

  • Instantiate Random for later use.

  • Follow Java naming conventions.

Revised SSCCE:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.sound.midi.ControllerEventListener;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * @see http://stackoverflow.com/a/17767350/230513
 */
public class MidiDrawing {

    private static final Random R = new Random();

    public static void main(String[] args) {
        EventQueue.invokeLater(new MidiDrawing()::display);
    }

    public void display() {
        JFrame frame = new JFrame("Midi Drawing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        DrawPanel dp = new DrawPanel();
        frame.add(dp);
        Sequencer sequencer = initSequencer(dp);
        JPanel p = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        p.add(new JButton(new AbstractAction("Start") {
            @Override
            public void actionPerformed(ActionEvent e) {
                sequencer.setTickPosition(0);
                sequencer.start();
            }
        }));
        frame.add(p, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private Sequencer initSequencer(DrawPanel dp) {
        try {
            Sequencer sequencer = MidiSystem.getSequencer();
            Sequence seq = new Sequence(Sequence.PPQ, 3);
            Track track = seq.createTrack();
            int n = 60; // middle C
            for (int i = 0; i < 3 * 12; i += 3) {
                track.add(new MidiEvent(new ShortMessage(ShortMessage.CONTROL_CHANGE, 0, 0, n), i));
                track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_ON, 0, n, 127), i));
                track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_ON, 0, n + 3, 127), i));
                track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_OFF, 0, n, 127), i + 3));
                track.add(new MidiEvent(new ShortMessage(ShortMessage.NOTE_OFF, 0, n + 3, 127), i + 3));
                n++;
            }
            sequencer.open();
            sequencer.setSequence(seq);
            sequencer.addControllerEventListener(dp, new int[]{0});
            return sequencer;
        } catch (InvalidMidiDataException | MidiUnavailableException e) {
            e.printStackTrace(System.err);
        }
        return null;
    }

    private static class DrawPanel extends JPanel implements ControllerEventListener {

        private final Font font = this.getFont().deriveFont(24f);
        private int data;

        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.getHSBColor(R.nextFloat(), 1, 1));
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setFont(font);
            g.setColor(Color.black);
            g.drawString(String.valueOf(data), 8, g.getFontMetrics().getHeight());
        }

        @Override
        public void controlChange(ShortMessage event) {
            data = event.getData2();
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(256, 128);
        }
    }
}


来源:https://stackoverflow.com/questions/17765604/java-painting-random-shapes-on-jframe-against-music-played

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!