Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Thread problem!

Status
Not open for further replies.

Oxymoron

Technical User
Dec 17, 2000
168
GB
Hello,

I'm using a recursive algorithm to solve a problem, which periodically calls upon a sprite to be moved around the swing GUI using the Observer and Observable mechanism.
However, being recursive, the calls to update the GUI via Observer's update() method are only executed after all the recursive method calls have been popped off the method call stack. (Basically the sprite jumps to it's finishing location, rather than moving in visible steps.)

I have solved this problem with another Thread class, but was wondering if anyone knew of a simpler way.

Also, just out of interest, does anyone know how to make an Icon appear as normal when appearing on a JButton that has been disabled (i.e. colour, not greyed out as disable icons normally look).

Any suggestions to either of these questions would be much appreciated!!

Oxy

we are all of us living in the gutter.
But some of us are looking at the stars.
 
Swing (and AWT) has one thread running to handle all GUI events. As long as your event handle does not return it cannot do anything else (like draw your sprite).

If you don't want to start a new thread every time, you can signal another thread to do the work, That way, your event handler can return immediately and be available to draw your sprite. You can create a special thread for this, or just use your main thread.

Here's a rough idea of what I mean:

public void update(Observable o, Object arg) {
MyClass.setNeedsUpdate(true);
}


public static void main( String args[] ) {
...
myFrame.setVisible(true);
while(true) {
while(!needsUpdate()) {
// draw sprite
setNeedsUpdate(false);
}
}
}

Sean McEligot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top