Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Threads
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
evogre3n




PostPosted: Thu Oct 13, 2005 9:20 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
Martin




PostPosted: Thu Oct 13, 2005 10:21 pm   Post subject: (No subject)

Here are some code blocks from a simple threaded program.

code:
class MyThreadClass extends Thread {
   ...
   public void run () {
      ...what happens when the thread is run...
   }
}


code:
public static void main (String[] args) {
   MyThreadClass myThread = new MyThreadClass(...);
   ...
   myThread.start();//run the thread
}
evogre3n




PostPosted: Fri Oct 14, 2005 10:08 am   Post subject: (No subject)

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

code:
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 Smile
Hikaru79




PostPosted: Fri Oct 14, 2005 5:34 pm   Post subject: (No subject)

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,
Java:
public class MyThread{
     private Console c;

     public MyThread(Console c){
          this.c = c;
     }

... etc ...

}

And then when you create your thread, go:
Java:
MyThread testThread = new MyThread(c);
evogre3n




PostPosted: Fri Oct 14, 2005 8:51 pm   Post subject: (No subject)

thats EXACTLY what i did!

and when i do that, i get a pointer null exeption error
wtd




PostPosted: Fri Oct 14, 2005 8:55 pm   Post subject: (No subject)

Have you initialized "c" before using it?
evogre3n




PostPosted: Sat Oct 15, 2005 8:21 am   Post subject: (No subject)

Yes, in my main program:

code:
c = new Console ();
evogre3n




PostPosted: Sat Oct 15, 2005 8:33 am   Post subject: (No subject)

yipee, it works, seems the private Console c; made it work in the threads, thanks a lot.

Now how bout creating flicker free graphics?
Sponsor
Sponsor
Sponsor
sponsor
Finaltank




PostPosted: Thu Oct 20, 2005 6:48 pm   Post subject: (No subject)

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 Sad

Yeah you need c= new Console ();
[Gandalf]




PostPosted: Fri Oct 21, 2005 1:11 am   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: