我一直在使用 Midi Sequencer 并进行一项练习,该练习涉及使用 ControlEventListener 根据音乐节拍在面板上以随机颜色、形状和位置重新绘制正方形。当我在笔记本电脑上执行此操作时,一切正常。但是,当我在我的 PC 上执行此操作时,我收到此错误:
Aug 07, 2013 1:10:11 PM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
BUILD SUCCESSFUL (total time: 27 seconds)
该程序运行良好。编译并完成它应该做的事情,而且,正如我之前所说,我对笔记本电脑上的这个确切代码没有任何问题。
此外,大部分代码都来自一本关于 Java 的书,我只对面板进行了一些更改以调整代码以稍微不同地执行相同的操作。有谁知道这段代码是什么意思?我已经用谷歌搜索了它,但什么也没找到。这本书没有说明这种代码。
任何帮助将不胜感激。预先感谢您花时间阅读本文以及您花时间帮助解决这个问题。
This is the code in its entirity:
import javax.swing.*;
import java.awt.*;
import javax.sound.midi.*;
public class Check implements ControllerEventListener{
JFrame frame;
DrawPanel dp;
public void controlChange(ShortMessage a) {
frame.repaint();
}
public static void main(String[] args) {
new Check().buildGui();
}
private void buildGui() {
frame = new JFrame("Woot");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dp = new DrawPanel();
frame.getContentPane().add(dp);
frame.setVisible(true);
frame.setSize(500, 500);
frame.setResizable(false);
frame.setLocation(375, 50);
playMusic();
}
private void playMusic() {
try {
Sequencer sequencer = MidiSystem.getSequencer();
sequencer.open();
int[] trackedInt = {127};
sequencer.addControllerEventListener(this, trackedInt);
Sequence seq = new Sequence(Sequence.PPQ,4);
Track track = seq.createTrack();
for(int i = 0; i < 50; i++) {
int rI = (int)(Math.random()*50)+30;
track.add(makeEvent(144,9,rI,100,i*10));
track.add(makeEvent(176,1,127,0,i*10));
track.add(makeEvent(128,9,rI,0,i*2+2));
}
sequencer.setSequence(seq);
sequencer.setTempoInBPM(160);
sequencer.start();
} catch(Exception exc){}
}
private 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 exc){}
return event;
}
}
class DrawPanel extends JPanel {
public void paintComponent(Graphics g) {
int r = (int)(Math.random()*256);
int gr = (int)(Math.random()*256);
int b = (int)(Math.random()*256);
g.setColor(new Color(r,gr,b));
int x = (int)(Math.random()*200)+20;
int y = (int)(Math.random()*200)+20;
int h = (int)(Math.random()*500)+20;
int w = (int)(Math.random()*500)+20;
g.fillRect(x, y, w, h);
}
}