java Jpanel 中遇到的了一些问题,新手求解 5C

demo是head first java中的例子,但是效果不对,我下载了源码运行出来也有一定的问题。运行后有些bug,我改了内部类MyDrawPanel中的刷新方式为mainPanel.repaint();但是效果和原先又不同了,希望大神解答下图片说明

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.sound.midi.ControllerEventListener;
import javax.sound.midi.MidiEvent;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Track;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class MiniMusic {

MyDrawPanel myPanel;
JFrame frame;
JPanel mainPanel;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    new MiniMusic().go();
}

private void go() {
    // TODO Auto-generated method stub
    frame = new JFrame();
    mainPanel = new JPanel();
    myPanel = new MyDrawPanel();
    JButton playItButton = new JButton("Play it");
    playItButton.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            try {
                Sequencer sequencer = MidiSystem.getSequencer();
                sequencer.open();

                sequencer.addControllerEventListener(myPanel, new int[] { 127 });
                Sequence seq = new Sequence(Sequence.PPQ, 4);
                Track track = seq.createTrack();

                for (int i = 0; i < 100; i += 4) {

                    int rNum = (int) ((Math.random() * 50) + 1);
                    if (rNum < 38) {

                        track.add(makeEvent(144, 1, rNum, 100, i));

                        track.add(makeEvent(176, 1, 127, 0, i));

                        track.add(makeEvent(128, 1, rNum, 100, i + 2));
                    }
                }
                sequencer.setSequence(seq);

                sequencer.start();
                sequencer.setTempoInBPM(220);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    mainPanel.add(myPanel);
    mainPanel.add(playItButton);
    frame.getContentPane().add(BorderLayout.NORTH, mainPanel);
    frame.setSize(500, 500);
    frame.setVisible(true);
}

public MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    MidiEvent event = null;
    try {
        ShortMessage a = new ShortMessage();
        a.setMessage(comd, chan, one, two);
        event = new MidiEvent(a, tick);

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

class MyDrawPanel extends JPanel implements ControllerEventListener {

    /**
    * 
    */
    private static final long serialVersionUID = 1L;
    // only if we got an event do we want to paint
    boolean msg = false;

    public void controlChange(ShortMessage event) {
        msg = true;
        myPanel.repaint();
    }

    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    public void paintComponent(Graphics g) {
        if (msg) {

            @SuppressWarnings("unused")
            Graphics2D g2 = (Graphics2D) g;

            int r = (int) (Math.random() * 250);
            int gr = (int) (Math.random() * 250);
            int b = (int) (Math.random() * 250);

            g.setColor(new Color(r, gr, b));

            int ht = (int) ((Math.random() * 120) + 10);
            int width = (int) ((Math.random() * 120) + 10);

            int x = (int) ((Math.random() * 40) + 10);
            int y = (int) ((Math.random() * 40) + 10);

            g.fillRect(x, y, ht, width);
            msg = false;

        } // close if
    } // close method
} // close inner class

} // close class


1个回答

原来里面的jar包有导入进去吗

qq_25255553
qq_25255553 该导入的全在了,不需要导入其他特殊的包
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问