2

I have been playing with the Midi Sequencer and doing an exercise involving repainting squares on a panel in random colors, shapes, and locations based on the beat of the music using a ControlEventListener. When I do this on my laptop, everything works just fine. However, when I do this on my PC, I get this error:

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)

The program works just fine. Compiles and does exactly what it's supposed to do, and, as I stated earlier, I have no problems with this exact code on my laptop.
Also, much of this code was taken out of a book about Java, I only made a few changes to the panel to tweak the code to do the same thing a little differently. Does anyone know what this code means? I've googled for it and found nothing. The book states nothing about this kind of code.

Any help would be greatly appreciated. Thank you in advance for your time reading this and any time that you spend helping with this question.

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);
    }
}
Jeremy Johnson
  • 469
  • 1
  • 4
  • 17

1 Answers1

-2

Just let the warning log shut up:

PlatformLogger.getLogger("java.util.prefs")
        .setLevel(PlatformLogger.Level.SEVERE);
Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
  • I understand that doing that is possible, but that just prevents the log from manifesting the error. It does nothing to correct it. The link that was provided in comments actually FIXES this. – Jeremy Johnson Dec 26 '13 at 18:26
  • Thanks for your comment, I browsed the Java source for the given issue and it looked like the instanciation causing this error is unwanted for you midi playback and is possibly an ancient relict of bad code design in the Java platform. As it does no harm, I proposed to suppress the warning. If it would have been your code instead, I would have given you some other suggestions. Your current "fix" has to be applied by every single user of your application and it can only be applied with admin rights on their machine. – Jens Piegsa Dec 26 '13 at 18:47