Al Dunne

Greenhorn
+ Follow
since Apr 24, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
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 Al Dunne

Hi Rob,

Thanks for that. So does that mean all I'll need in my code is:

11 years ago
Thanks a lot for your help Ritchie. Much appreciated.
11 years ago

Campbell Ritchie wrote:That code doesn’t change the state of the List at all. What it does, however, is get a reference to every Check Box in the List, and the next statement changes the state of that check box. My version did it all in one line.



Am I right then in saying that "JCheckBox check = (JCheckBox) checkBoxState.get(i);" makes 'check' be a reference to checkBoxState[i] so that if you change the state of 'check' (as the bit after that code does) it will change the state of checkBoxState[i].

This is the part that I don't understand as in the most part all I've been working with is primitives where if you say int num = otherNum (another int) then the value of num will be the same as otherNum. Whereas if you applied the logic from above writing int num = otherNum would make the value in OtherNum the same as num.

I'm vaguely remebering something about object references??? If you make an object1 ref = object2, am I right in saying that the object 1 is just a reference to object2 rather than an object itself. Confusing myself now.

11 years ago
Yes, the saveBeatBox method is at Line 236 above. It just saves a boolean [] called savedCheckBoxState.

Still don't understand how this line changes the state of checkBoxState[i]. Is it not always that the thing on the left of the = will become what's on the right?



The code is from the second edition of HeadFirst which I think is the latest version.

Thanks for taking the time.
11 years ago
Hi all,

I've a problem with creating a jar file that's melting my brain. I've been at it for the last 3 days and cannot solve. I've tried pretty much everything. If anyone can help, please do.

So originally I had a File [] called soundFile that is going to store 9 files. I had a loop to import the 9 wav files. These wav files were called 0db.wav, 1db.wav etc etc and to get the code to work, I had to put these files inside my Version 4 folder (see image attached - Method 1 folder).

My original code for loading was as follows:

Code:
for (int i = 0; i < 9; i++){

soundFile [i] = new File (i + "db.wav");
}



This worked fine when I ran the project in NetBeans. However, a couple of days ago I tried to create the jar file and the section that played the sounds did not work i.e. did not play the sounds. From looking at it, when I told NetBeans to load the file it looked in the Version 4 folder (see image attached - Method 1 folder).

I then looked around everywhere to see what the problem was. I ended up getting the jar file working by hard-coding in a directory address but as you might guess, when I changed computer it stopped working.

So then after getting advice from other forums I tried numerous things. The most promising was to move the wav files into the rubberduckssoundproject folder which is inside the src folder. This folder rubberduckssoundproject contains all our other code in the project. The code for loading the wav files was then:

Code:
for (int i = 0; i < 9; i++){
try{
soundFile [i] = new File (this.getClass().getResource (i+"db.wav").toURI());
}
catch (Exception fileError){
System.out.println("Sorry but there's a problem loading your files into an array");
fileError.printStackTrace();
}

System.out.println("The soundfile located at soundFile "+i+" has the following path:"+soundFile[i].getAbsolutePath());
}


So this worked when the project was run in NetBeans. The path it returned for the files was ".........\Version 4\build\classes\rubberduckssoundproject\0db.wav"

This looked good to me as I think (I'm fairly new to Java) the jar files gets built from the build folder???

However, now when I clean and build, a jar file gets created but when I go to click this jar nothing happens. Then when I go back into the project folder it will not let me delete the jar file. It keeps saying "The action can't be completed because the file is open in Java (TM) Platform SE binary. This leads me to believe there's some sort of infinite loop or something being created?

I've thought about creating InputStreams and passing them to the
Code:
audio = AudioSystem.getAudioInputStream(soundFile[activeSound]);

The problem with this is I need an array of InputStreams and NetBeans will not let me do this. Can't remember exactly why, but it wouldn't. If anyone needs to know what it said I can try it again.

Please, please, please help if you can. I'm on the verge of giving up and I really don't want to.

Thanks a mil.

Al.
11 years ago

Campbell Ritchie wrote:
What you are doing is going through a list of check boxes, and setting their selected-ness (grammar!) to match the value in the parallel boolean[] array.



But can you tell me how this actually happens. I understand that line 265 makes the boolean [] loadCheckBoxState = the boolean [] read from the saved file (object cast back to a boolean []).

But whay I don't understand is how this then changes the state of the boolean [] "ArrayList <JCheckBox> checkBoxState" that gets run through the buildTrackAndStart method (Line 106).

So the line to adjust this has to be Lines 272 - 280


I think it must be Line 2 from the little snippet above that is changing the state of checkBoxState[i].
To me this reads, new JCheckBox check = whatever is stored in checkBoxState [i] but I'm probably wrong and this actually means make whatever is in checkBoxState = to whatever gets put into 'check'.
Can anyone clarify this?

This is code from the HeadFirst Java book (slightly modified by me). If there's bad coding, it's their fault.

Sorry about the delay getting back also. I have another problem in NetBeans that has been sucking up my time. Still haven't solved it. Relates to jar files. I might stick it up here now aswell.
11 years ago
Thanks Ritchie. Can anyone help with line 14 of original code posting / line 273 of full code posting?

How exactly is this changing the checkBoxState arraylist?

Al.
12 years ago
It's an ArrayList of type JCheckBox. Here's my full cose (sorry about all the comments, it was me trying to understand each step). I'd really like to understand how the file import is altering the state of the checkBoxState ArrayList. Again, any help much appreciated. Al.

12 years ago
Great that's the Line 6 casting answered. Thanks William. What you mean about Generics <>? Around my queries?
12 years ago
Hi all,

I hope I've put this in the correct forum.

I've completed the Head First Java Beat Box and everything worked fine. I then went on to the saving and restoring part and got that working (note I added a JFileChooser to my code). The problem I'm having is getting my head around the file restore part of the code as follows (note I made some variable/method name changes etc to the Head First code):-



From what I understand so far, the code above does the following:
Line 2 - Declare a boolean array called "loadCheckBoxState" and set it to null.
Lines 4/5 - Create a new object input stream to read from the saved file that gets passed to it.
Line 6 - Create the loadCheckBoxState boolean array. Then fill it with the contents of the objectInput stream, casted as a boolean array (boolean []). Q1. I'm not sure why it has to be casted as the file was saved as a boolean array?
Line 13 - Create a loop to iterate 256 times.

Now here's where my major problem is.
Line 14 - Declare a JCheckBox check and make it equal to the state of the actual checkbox in the class (note checkBoxState is the checkBox that was loaded into the ArrayList and the panel in the code not re-produced above) at the loop position. Q2. It's this line that's getting me. Why do we need to cast "(JCheckBox)"??? And how does this code actually change the checkBoxState checboxes in the code not re-produced (which are used to create the MIDI and build the track)??? To me this line reads make the JCheckBox check = the state of the JCheckBox checkBoxState
Lines 15 - Lines 19 - Iterate through the saved file and if the loaded checkbox is true/false, make the JCheckBox check true/false. My questions on this line will be answered (I think) if someone can answer my questions on Line 14.

Any help would be much appreciated. I hope I'm being clear in what I'm asking.

I can reproduce the full class if it would make it easier.

Thanks,

Al.
12 years ago
Hi Paul,

I'm fairly new to this so I was getting confused about where I should call the stop method. I had it in a few places but it wasn't working. I really don't understand about Clips etc yet.

I got the solution on another website yesterday afternoon. It was to put the following code before the try-catch in the playSound method.



Thanks for your reply though.

Al.
12 years ago
Hi all,

I'm fairly new to this so I hope I've posted this to the correct place.

I'm completing a project where a user gets to press a few buttons and they will see a waveform change and hear the amplitude of a sound increase or decrease. To get the sounds to play I'm using javax.sound.sampled.*

My code is as follows (the buttons the user presses are in a seperate GUI built class):-



The actual sounds are 10 seconds long so I want them to loop continuously until the user either increases or decreases the amplitude.

All my code is working fine. However, the problem I'm having is that everytime the user clicks the increase amplitude button and calls the playSound method, a new audio line gets opened and the sounds play at the same time.

Is there a way I can close all existing audio lines before a new one is opened? I've been looking for a couple of hours now but am getting more and more frustrated/confused.

Any help would be much appreciated.

Al.


12 years ago