
-----------------------------------
evogre3n
Thu Oct 13, 2005 9:20 pm

Threads
-----------------------------------
Hey everyone, ive started Java gr.11 course, and for MyCreation assignment i have to create a bunch of threads to animate a bunch of objects in my assignment.

the problem is, I am required to use the console class, with Ready to Program, and I have tried all kinds of parameter passes to the run() method in my thread to get it there including run (Console c) (with import hsa.Console; at the top) and all that, but it just doesnt work.

Would love if someone could help out.

thanks

-----------------------------------
Martin
Thu Oct 13, 2005 10:21 pm


-----------------------------------
Here are some code blocks from a simple threaded program.

class MyThreadClass extends Thread {
   ...
   public void run () {
      ...what happens when the thread is run...
   }
}

public static void main (String[] args) {
   MyThreadClass myThread = new MyThreadClass(...);
   ...
   myThread.start();//run the thread
}


-----------------------------------
evogre3n
Fri Oct 14, 2005 10:08 am


-----------------------------------
Okay, i got that part.

Now where in the thread where i need to "run" i have to use the console class, and have it write in the same window, called in the original program.

ei

class MyThreadClass extends Thread {
   
   public void run () {
        c.setColor (Color.red);
        c.drawRect (0,0,100,100); 
   }
}

how do I pass the console into this thread? even if i stick import hsa.Console and state static Console c; it still doesnt work.

Thanks a lot if you can help  :)

-----------------------------------
Hikaru79
Fri Oct 14, 2005 5:34 pm


-----------------------------------
You shouldn't be messing with threads yet if this is not something that you can easily answer yourself =/ But the short answer to your question is to pass the Console in to the constructor of the thread. So, for example,
public class MyThread{
     private Console c;

     public MyThread(Console c){
          this.c = c;
     }

... etc ...

}
And then when you create your thread, go:
MyThread testThread = new MyThread(c);


-----------------------------------
evogre3n
Fri Oct 14, 2005 8:51 pm


-----------------------------------
thats EXACTLY what i did!

and when i do that, i get a pointer null exeption error

-----------------------------------
wtd
Fri Oct 14, 2005 8:55 pm


-----------------------------------
Have you initialized "c" before using it?

-----------------------------------
evogre3n
Sat Oct 15, 2005 8:21 am


-----------------------------------
Yes, in my main program:

c = new Console ();

-----------------------------------
evogre3n
Sat Oct 15, 2005 8:33 am


-----------------------------------
yipee, it works, seems the private Console c; made it work in the threads, thanks a lot.

Now how bout creating flicker free graphics?

-----------------------------------
Finaltank
Thu Oct 20, 2005 6:48 pm


-----------------------------------
I use ready alot for my Gr 11 course, do you know many advanced things in ready to program like how to use an image or stuff?

The farthest we know is for loops :(

Yeah you need c= new Console ();

-----------------------------------
[Gandalf]
Fri Oct 21, 2005 1:11 am


-----------------------------------
I am fairly sure RTP can do normal Java just fine, look up some image tutorials for normal Java.  Check the Java site, the tutorials here, google, etc.
