Originally Posted by
willemjav
(Or how to get into one and, most importantly, how to get out of a one.)
Really simple:
class Music implements List of Some Kind{
List notes;
Position position;
Boolean playing;
Music(){
// ....? maybe set music type or something
}
public void nextNote()
{
++position;
}
}
//
//
class Controller extends GUI / AWT / Graphical {
Button goButton;
Button stopButton;
Boolean ExitProgram; // Actually, you do not need this
//
MasterClock mc;
//
Controller(MasterClock mc){
this.mc = mc;
}
public void ButtonHandler(ButtonPush bp)
{
if(bp == goButtonPush)
{
mc.startPlay();
}
if(bp == stopButtonPush)
{
mc.stopPlay();
}
}
public void stopPlay()
{
mc.exit();
}
}
class MasterClock implements Runnable
{
//
private Ticker;//
private List waits;
//
Boolean running = new Boolean(true);
//
public void run
{
while(running.BooleanValue())
{
//
if(++Ticker > NextTimeToMoveOnList)
{
Music.nextNote();
}
// Looks funny, but is closer to correct practice.
sleep(0);
}
}
public void startPlay()
{
running = new Boolean(true);
}
public void stopPlay()
{
running = new Boolean(false);
}
public void exit()// Not use yet....
}
Originally Posted by
willemjav
I haven’t seen any better alternative to my previous loop constructions!
You have now, though this has considerable detail left out.
Originally Posted by
willemjav
Maybe because loops form the very hearth of computers and there functioning.
Basically, this is close to technically correct.
Originally Posted by
willemjav
Almost everything, concerning a computer, is loop cycle.
Yes.
Originally Posted by
willemjav
A program is one the processor cycle is one, events are loops the poor thing has actually noting better to do than spinning its loops.
Correct.
Originally Posted by
willemjav
(I know, now that nick is reading carefully - he thinks that I am joking, but I am not, this looping bUssiness is very serious to me so he will surprise me with another mysterious code which should put my thinking into a loop.
I realize you are not joking, and yes it is funny to me but try not to loop you thinking.
Originally Posted by
willemjav
(because he is actually the joker of our forum).
Did you see my post about UFO's in Oklahoma?
Originally Posted by
willemjav
This new loop waits for the sound tracks to start and is note at the GUI class where buttons etc. are pushed. It sits at the class where all the sounblocks are. The loop happens, again, inside a thread (I start to like threads)
Just replace the System.out.prints with Music.play(note)
Originally Posted by
willemjav
The loop will keep the thread busy spinning until the method call of the GUI class turns true. Than the flag gets reset and the important mainSoundblock(1); method starts WITHIN THE THREAD.
Method call of GUI class is a method call of an instance of MusicPlayer. You have two runs, one is runs music - which runs along a List - iow Plays Music. The other is the system thread with is the gui and so on. You do not program that thread, the window controls take care of that for you. If you want to be able to start and stop your music, you have to have a Thread trapped in a while, which perforce must do a sleep or yield. Everyone codes it like Norm has it. Wait and NotifiyAll have performance issuse that are not important for low data-rate such as midi on one Mac. If that approach makes more sense to you, do it.
Originally Posted by
willemjav
Consider the working of this method similar to a pin-ball machine. The little ball starts at the very top but eventually will reach the bottom. The if’s, when turn false, will push the ball hard (the esc key was pressed). Each soundblock takes from 20 sec. to several minutes (depending on midi pedals, remember). When reaching the end the thread is over and the cycle/ thread repeats by pushing the start button.
There are two ways to flip the pinball. One is with a Thread.new() the other is to release a boolean gate on button push, un-gatting a Thread trapped in a do / while. The problem with a do / while is that if the thread is not daemon, we get sluggish exits so what I have decided to do is construct all objects in main() setting daemon == true and calling start on the clock class. GUI class has a reference to the Clock, which does a check on boolean continue running each time around.
private void mainSoundblock(int trck)
{
if((trck > minValue)&&(trck < maxValue))this.settrackNumb(trck);
else this.settrackNumb(defalutTrackNumber);
}
Giant switches can usually be reduced to a simple variable, check for continue running is moved to MasterClock.
Originally Posted by
willemjav
By the way PK, when coming out of the loop and NOT coming out of the thread, all the buttons, besides the escape, are turned off (xxx.setEnabled(false) so that no other thread, besides the midipedal and escape, can disturb the pin ball.
Correct.
Originally Posted by
willemjav
this funny head appeared by itself?
Those smileys are actually certain character combinations such as semicolon/right round brace.
Originally Posted by
willemjav
Location: Spain
I thought you were in Netherlands.....
Originally Posted by
willemjav
Yes norm, we might get to the center of this problem (maybe a lack of java imagination of mine). Java does not support multiple inheritance, right. I read some stuff about that, and the point I got out of that all, is that they do not want to turn java into the spaggetti-code mess of C++.
I would place a joke here but those folks watch too many spaggetti-westerns.
Originally Posted by
willemjav
I started out with java and do not know any better, good! (I choose for java, because I do not feel any sympathy for the micro-soft (Chicago)software-boys
Ditto.
Originally Posted by
willemjav
if you want to know more about that, read Naomi Klein’s latest, The Shock Doctrine (yes, nick I believe we should show our code-biting-juniors about the many hidden background stories of this world).
Meek, and media-compliant service to normalacy - nothing more. Chicago packs Iron, Naomi doesn't.
Originally Posted by
willemjav
Okay let’s follow the thread, when the Main class on top of the other classes creates an instance of the second Controler class, that has all the GUI stuff, THERE IS A ONE WAY DIRECTION OF INFORMATION, right.
GUI -> Controller, clock just slips out of loop if trap variable is false.
Originally Posted by
willemjav
When the instance of the GUI-class, called count, created at the Main-class (where all the soundblocks are, remember) wants to get data from the GUI-class it gets it by cont.whatever(); If I create a thread at one of the event-notifying methods (of the GUI-class) I WOULD BE GOING INTO THE OPPOSITE DIRECTION.
Move all driver code to the controller class. Main just construct a few objects and call start on them. That is why Swing has it's own launcher, called javaw.
Originally Posted by
willemjav
Where is the instance of the Waitloop class created?
You do not have a class named
Originally Posted by
willemjav
The return When I Can Execute method would loop/wait until the event occured when you want the mainSoundblock method to be called. This would simplify the design by not requiring the run method to know about setting and clearing flags in the cont class.
Events, ala AWT / Swing, set variables that are the boolean that Trap the Threads. Wait() and Notifiy are the same thing, except they are written by Engineers for people who think that way.
Originally Posted by
willemjav
Finally, I have the the program working. I have tested the application many times and everything works just fine. I have to take care of some æsthetical things.... and done. Probably the underlaying mistake of the program is that I did not put the GUI stuff into the top class. I have two waitloops taking care of the GUI management, and one in a new created thread.
Timer / TimerTask if(nextClock)doSomething();yield();// runs at 1khz
do{music}while(run);// wait for start button
No wait states, just yield or sleep if no work to do....machine is vastly faster than what you think it is.
Originally Posted by
willemjav
Sure next programming project I should design differently and I will try to get rid of those un-elegant cpu consuming loops.
Prove they consume cpu cycles needlessly, do not go nuts trying to do that.
Originally Posted by
willemjav
1) when appending text into a JTextArea, how could I keep the new text on sight at the screen (the new appended text scrolls down and disappears)?
JTextArea.setText(" ...... ");
Originally Posted by
willemjav
2) Before going out of the application, at the very last moment, I would like to clean up some things (clossing drivers etc.). I am using the standard
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
put all that in finally of try / catch block, which is entered when do / while is released by setting run boolean to false or better yet per Norm "Use a Window Listener to catch window closing and put a call to your cleanup code there."
Originally Posted by
willemjav
There is something I am not getting here!
Vestigages of earlier work, you are moving along.
Originally Posted by
willemjav
There happens an event in class B (at any time!!!) how this can be noted in A???
public static void main(String[] args)
{
A a = new A();//
B b = new B(a);//
// Or ....
B b = new B(new A());//
b.start();
}
Originally Posted by
willemjav
At class A I could ask for b.getMidiped() and it will return the correct true or false of the var. But I do not know when that will happen from inside the code of class a so that is why I need a loop to wait this to happen......... Or not???
One way data-flow, as discussed earlier.
public static void main(String[] args)
{
do
{
;//
}while(boolean run);
}
Norm's code is a classic intro to threads, Wait is not a class, has do{}while(); stuffed in somewhere other than where the wait needs to happen.