This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes Semi-Urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Semi-Urgent " Watch "Semi-Urgent " New topic
Author

Semi-Urgent

Reed Spool
Greenhorn

Joined: Jan 11, 2006
Posts: 14
The following code is a project for school i'm doing, it's due ina week and a half and i really wanna finish it so that's why this is semi urgent. I just learned how to make and use an applet today specifically for this program. The purpose of the program is to have an arrow start at the left side and move towards the right as a balloon moves from the bottom and moves towards the top. my problem is that when i try to make the animation slow down so one may be able to see it, that Java loading thing displays at the beginning of my program instead of showing the program happening, then when all of the code is done, which takes a while since i put the pause in, it shows the results. I thought this might have something to do with putting the pause code in the init() method so i moved all of it to the start() method, but still the same thing happens. I'm going to post just the urgent parts of the code because it's a bit lengthy


this uses the Thread.sleep with try and catch blocks, but i dont pretend to know how that works. i'm not at the point of threads yet
the next block is the Pause class which my teacher wrote, it works fine and dandy usually but when Thread.sleep in the above is subsituted for Pause.pause(x) the same thing happens as when Thread.sleep is there,



THANKS for reading this already, i know it's really lengthy. Hope you have some good suggestions


Splinterspool
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Thread.sleep() merely waits, without doing anything. Other threads in the program -- in particular, the one responsible for painting the screen -- can do its work in the meantime.

Your teacher's lovely "pause" method, on the other hand, pegs the CPU usage to 100% while it's running, wasting energy, generating heat, contributing to global warming, and worst of all, preventing that painting thread from doing its thing. If the CPU is running at 100%, there's no time for making pretty pictures on the screen! If this was actually provided by a person from whom you are supposed to be learning... well, let's say you ought to rely on the book as much as possible.

Here's another pause method which doesn't have this problem, and is drop-in compatible with the one you've shown:



[Jess in Action][AskingGoodQuestions]
Reed Spool
Greenhorn

Joined: Jan 11, 2006
Posts: 14
Thanks for the lesson but that's not my problem
The java sun microsystems loading screen thingy goes on when i use either of the slowing methods, and it doesn't display what it should be displaying <What i'm painting on the applet> until all code is executed. how to do the waiting is the least of my worries, what it's doing to the applet is my problem
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Indeed, sorry, I couldn't see the forest for the trees. There is another problem: to do animation, you must start a separate thread to do it in. The start() method is called from that same painting thread I was talking about before, and until start() returns, the screen can't be painted. start() has to do its business quickly and get out of the way so that the screen can be updated when you need to to be.

Although "start a new thread" sounds very scary, it's not, really. Here's a recipe for you:

1) define a method

public void run() { ... }

and move everything in start() into it.

2) Declare that your applet class "extends Applet implements Runnable" (i.e., add that "implements Runnable" part.)

3) In start(), just do this:

public void start() { new Thread(this).start(); }

Your run() method will then be called from a separate Thread, leaving the GUI thread free to update the screen when you call repaint().
Reed Spool
Greenhorn

Joined: Jan 11, 2006
Posts: 14
Thanks very much, this is exactly what i needed. The runnable works great
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Semi-Urgent
 
Similar Threads
URGENT
Urgent....
Threads Synchronization (Urgent)
Urgent
how to let 10 thread do the same thing simultaneously ?