Kevin Simonson

Ranch Hand
+ Follow
since Oct 22, 2011
Cows and Likes
Cows
Total received
2
In last 30 days
0
Total given
0
Likes
Total received
4
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Kevin Simonson

I just wrote a little program to see what happens when I enter keys using class {KeyListener}. The program is:

This works just fine for every key I enter except the tab key. Nothing happens when I press the tab key. Does anyone know what I have to do to get my code to process the tab key?
1 year ago
I implemented a Red Black Tree for a program I'm writing like so:

and then I wrote a driver to test it:

which works just fine. But I had to cast the result of method {insert()} to get an object of type
{Frwrd}, so I thought I'd use Java generics, and pass {Frwrd} in as a parameterized type. So
rewritten versions of the same two files are:

and driver:

When I compile this I get:

D:\Langs\Java\Src\Alan\Alt>\Langs\Java\8u40-windows-x64\bin\javac -Xlint:unchecked RedBlack.java
RedBlack.java:47: warning: [unchecked] unchecked cast
   newRoot.high.set( inq, (TR) this);
                               ^
 required: TR
 found:    RedBlack<TR>
 where TR is a type-variable:
   TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:59: warning: [unchecked] unchecked cast
     return (TR) this;
                 ^
 required: TR
 found:    RedBlack<TR>
 where TR is a type-variable:
   TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:63: warning: [unchecked] unchecked cast
   { stRoot.merge( (TR) this);
                        ^
 required: TR
 found:    RedBlack<TR>
 where TR is a type-variable:
   TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:72: error: rotate(int) has private access in RedBlack
       { stRoot = stRoot.rotate( 0);
                        ^
 where TR is a type-variable:
   TR extends RedBlack<TR> declared in class RedBlack
RedBlack.java:75: error: rotate(int) has private access in RedBlack
       { stRoot = stRoot.rotate( 1);
                        ^
 where TR is a type-variable:
   TR extends RedBlack<TR> declared in class RedBlack
2 errors
3 warnings

D:\Langs\Java\Src\Alan\Alt>

The announced errors on lines 72 and 79 can both be fixed by replacing the {private} qualifier for
method {rotate()} with {protected}, and then the new version of {Frwrd} works just fine. But why
does it need to be {protected}? Method {stInsert()} is declared to be {private}, and that works just
fine when called by method {insert()}; why can't method {insert()} be happy when it calls {private}
method {rotate()}?

The other things the compiler complains about don't keep the program from compiling, but they're
still of some concern to me. They entirely have to do with the {(TR)} cast on {this}. That being the
case, I don't know why the compiler is complaining; {TR} is declared to be a subtype of {RedBlack},
so clearly a cast from {RedBlack< TR>} to {TR} is legal. So why the compiler complaint?

Is there some other way to implement a Red Black Tree that wouldn't involve code that would generate
these types of complaints? Or should I just be happy with the fact that the code compiles?
1 year ago

Paul Clapham wrote:Anyway I would suggest that



might work better. That's what I did in this sort of situation, at least I think it's this sort of situation.


Thanks! That resolved the problem.
1 year ago
An extraneous line got into my listing; it should actually say:

Sorry for the discrepancy!
1 year ago
I wrote this piece of code and stored it in file "Bug.java":

Then I tried compiling it and got error messages:

D:\Langs\Java\Src\Alan\Bug>D:\Langs\Java\8u40-windows-x64\bin\javac Bug.java
Bug.java:21: error: incompatible types: Object cannot be converted to BG
   high.set( opp, hmm.high.get( inq));
                              ^
 where BG is a type-variable:
   BG extends Bug declared in class Bug
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

D:\Langs\Java\Src\Alan\Bug>

This seems to be saying that {hmm.high.get( inq)} will return an {Object}, which "cannot be converted to BG". But {hmm.high} is actually a {List< BG>}, so why would that objects {get()} method be returning an {Object}? Shouldn't it return an object of type {BG}? Which then gets assigned to the element of {this.high} that corresponds to index {opp}? {this.high} is once again of type {List< BG>} so it looks like this should work. Does anybody have any idea why my code isn't working? Thanks in advance for any pointers anyone can give me.
1 year ago

Stephan van Hulst wrote:Actually, you shouldn't be using Thread.sleep() at all. If you want to block the current thread until the track has finished playing, you should use locks and conditions and add a meta event listener to the sequencer.


Stephan, can you tell me where I can go to find out how to "use locks and conditions" and adding "a meta event listener to the sequencer"? Are those things new to Java 8 or Java 9? I'm mostly familiar with Java 6 and 7, and I don't remember anything about locks, conditions, or meta event listeners.
1 year ago

Kevin Simonson wrote:

Carey Brown wrote:What is an example of command line arguments that you're using?
Edit: sorry I missed your comment at the end.


java Synth 96 6 0 127 6500


Although I think what I'm going to go with for my game is probably:

java Synth 96 7 0 127 5500
1 year ago

Carey Brown wrote:What is "rsltn" ?


Resolution. I guess I should have spelled it out.
1 year ago

Carey Brown wrote:What is an example of command line arguments that you're using?
Edit: sorry I missed your comment at the end.


java Synth 96 6 0 127 6500
1 year ago
After I got Stephan's code working, I decided I'd try to simplify it somewhat, and, in case you're interested, here's what I came up with:

That last comment line is just a suggested list of arguments to call it with. Let me know what you think of this Java class.
1 year ago

Stephan van Hulst wrote:Actually, everything is working fine. The sequencer is now open, and it's sequencing the music. The message you get is just a warning, you can disregard it.

The final problem you're facing now is that music is sequenced and played asynchronously, and the program terminates before it has a chance to finish playing the music. You need to write some code that blocks until the music has stopped playing. For now, you can use Thread.sleep() after you've started the sequencer to listen if any music is being played. After you've made sure that it does, you can add a MetaEventListener to your sequencer to detect the end of the melody, and use Lock and Condition to block until you've detected the end.

Note that you don't have to call sequencer.close(). Because you're using try-with-resources, the sequencer is automatically closes as soon as you leave the try statement.


Wonderful! I got it working the way everybody suggested I do it, but found it was executing too slowly, so I decreased {half} from 1/2 to 1/16, and that gave me:

That sounded great. But I noticed that the first note seems to last twice as long as any of the other notes. Does anybody know why that might be the case? I'd like to get the melody to play with each note having the same brief duration.
1 year ago

Stephan van Hulst wrote:You're almost there!


I'm glad to hear that!

Stephan van Hulst wrote:The exception tells you exactly what's wrong. Your sequencer hasn't been opened. So you should try to open it.

Please note that Sequencer is AutoCloseable, so you should use it in a try-with-resources statement.

addAsTrackToSequence() also doesn't need the timeSignature parameter, so remove it.


Okay, so now I have:

When I ran this I got messages:

D:\Langs\Java\Src\Comtutor\Stephan>j8 Stephan
Jul 03, 2017 10:26:47 AM 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.

D:\Langs\Java\Src\Comtutor\Stephan>

So even explicitly opening my {Sequencer} object appears to fail to open that object. Any idea what I'm doing wrong?
1 year ago

Stephan van Hulst wrote:

Kevin Simonson wrote:Well, the only thing that creates {MidiEvent}s is {addAsTrackToSequence()}. Are you saying that after my program calls {melody.playNote()} five times, I can call {melody.addAsTrackToSequence()} and use that to make my melody sound?


Yes, that method converts the notes to Midi events, and adds those Midi events to a track that it creates itself. The Track is already part of a Sequence.

There are two constructors that create {Sequence} objects, namely {Sequence ( float divisionType, int resolution)} and {Sequence ( float divisionType, int resolution, int numTracks)}. I'm guessing I want the first. What exactly do the {divisionType} and {resolution} arguments do, and can anybody give me any idea what values I'd want to set them to in order to generate my little melody?


Division type determines the units that the resolution is given in. The two main kinds are "pulses per quarter", which is used for tempo-based Midi, and "frame rate", which is mostly used when playing Midi for a video. Indeed, we're only interested in tempo-based Midi, so for divisionType you should use Sequence.PPQ.

The resolution is exactly that: how precise you can time your Midi events. If you only have 2 pulses per quarter, you will only be able to time midi events at two different moments within a quarter. I believe 96 pulses per quarter is a fairly standard amount to use.

I know how to create a {Track} object. How do I add that {Track} object to my {Sequence} object? I don't see a {Sequence} method for doing that.


You don't have to. The addAsTrackToSequence() already does that. When it calls sequence.createTrack(), the track that is returned is already part of the sequence.


Okay, I tried incorporating this and got:

but when I ran it I got messages:

D:\Langs\Java\Src\Comtutor\Stephan>j8 Stephan
Jul 02, 2017 12:04:38 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.
Exception in thread "main" java.lang.IllegalStateException: sequencer not open
       at com.sun.media.sound.RealTimeSequencer.start(RealTimeSequencer.java:243)
       at Stephan.main(Stephan.java:29)

D:\Langs\Java\Src\Comtutor\Stephan>

It looks like I'm having trouble opening object {sequencer}. Anybody have any idea what I'm doing wrong?
1 year ago

Stephan van Hulst wrote:Create a new Sequence, add your Melody to it as a Track, and use Sequencer.setSequence() and Sequencer.start() to sequence your Melody.


I know how to create a {Track} object. How do I add that {Track} object to my {Sequence} object? I don't see a {Sequence} method for doing that.
1 year ago

Stephan van Hulst wrote:Yes, all this code does is convert notes to Midi events. The events still have to be sequenced and sent to a playback device.

MidiSystem.getSequencer() returns the system's default sequencer, hooked up to a default playback device. Create a new Sequence, add your Melody to it as a Track, and use Sequencer.setSequence() and Sequencer.start() to sequence your Melody.


There are two constructors that create {Sequence} objects, namely {Sequence ( float divisionType, int resolution)} and {Sequence ( float divisionType, int resolution, int numTracks)}. I'm guessing I want the first. What exactly do the {divisionType} and {resolution} arguments do, and can anybody give me any idea what values I'd want to set them to in order to generate my little melody?
1 year ago