Search the web
Sign In
New User? Sign Up
jurtle-users · Jurtle Users
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Want your group to be featured on the Yahoo! Groups website? Add a group photo to Flickr.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
GUIDisplay.java   Message List  
Reply | Forward Message #108 of 119 |
Re: [jurtle-users] GUIDisplay.java

John,

This is normal when executing code on the Event Thread. Because the
event thread is used for both events and drawing updates, it is
somewhat tricky to get intermediate drawing to happen during a method
that is running on this thread. The standard way would be to let the
event thread method start another thread that does the drawing,
calling updateDisplay as needed. Because this is not happening on
the event thread, it is now freed up to process the update events.

Here is some modified code that demonstrates this. Welcome to the
(somewhat confusing) world of multi-threaded programming.

/**
* The GUIDisplay turtle displays the same basic user interface as
GUI, but it does
* it within the Display. This shows how to get the display panel
and add graphical
* elements to it.
*
* Note the use of waitForStop() at the end of the runTurtle()
method. This causes
* the turtle to not finish until the Stop button has been clicked or
the stopTurtle()
* method has been called.
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.otherwise.jurtle.*;

public class GUIDisplay extends Turtle
{

JButton redBtn;
JButton greenBtn;
JButton blueBtn;
Container displayContainer;

/**
* Main entry point to the code. Creates the GUI within the
Display
* area calls waitForStop to wait until the Stop button is
clicked.
*/
public void runTurtle()
{
displayContainer = getDisplayContainer();
JPanel controlsPanel = new JPanel();
controlsPanel.setLayout( new FlowLayout() );

displayContainer.setLayout( new BorderLayout() );
displayContainer.add( controlsPanel, BorderLayout.NORTH );
displayContainer.add( getDisplay(), BorderLayout.CENTER );

redBtn = new JButton( "Red" );
redBtn.addActionListener( new BtnHandler() );
controlsPanel.add( redBtn );

greenBtn = new JButton( "Green" );
greenBtn.addActionListener( new BtnHandler() );
controlsPanel.add( greenBtn );

blueBtn = new JButton( "Blue" );
blueBtn.addActionListener( new BtnHandler() );
controlsPanel.add( blueBtn );

displayContainer.validate();
waitForStop();
}

/**
* The BtnHandler class is responsible for responding to mouse
* clicks on the buttons.
*/
private class BtnHandler implements ActionListener
{
public void actionPerformed( ActionEvent evt )
{
Object src = evt.getSource();
if ( src == redBtn )
{
setDisplayColor( Color.red );
Console.println( "Red button clicked" );
}
else if ( src == greenBtn )
{
setDisplayColor( Color.green );
Console.println( "Green button clicked" );
}
else if ( src == blueBtn )
{
(new HandleBlueBtn()).start();
}

}
}

private class HandleBlueBtn extends Thread
{
public void run()
{
setDisplayColor( Color.blue );
Console.println( "Blue button clicked" );

// insert code from ColorWheel class

setPenWidth( 5 );
setAutoUpdate( false );
hideTurtle();

for ( int i = 0; i < 360; i = i + 1 )
{
setPenColor( Color.getHSBColor( i / 360.0f, 1.0f,
1.0f ) );
center();
forward( 200 );
right( 1 );

// Only update the display every 10th line drawn.
This decreases
// time to draw the wheel
if ( ( i % 10 ) == 0 )
updateDisplay();
}
updateDisplay();
}
}


}


Bill

On Sep 7, 2005, at 11:43 PM, John Kirkilis wrote:

> Howdy Bill,
>
> For the past few classes, I've been going over GUI interface
> programming
> with my students. I started them off with the slider example
> (PolySpiral), but then retreated back to the "GUIDisplay" example as a
> better starting point. GUIDisplay uses a named inner class for the
> button handling while the PolySpiral uses an anonymous class to handle
> the events.
>
> My students started adding their own buttons and then invoking some of
> the drawing code from other Jurtle examples in the handler methods. In
> every case, the display area did not update until the execution of the
> handler completed even though some of the code explicitly asked for
> updates to occur as certain intervals. I'm suspicious that there's
> some
> event and/or thread management issue that is not allowing any of the
> interim updates, or perhaps the updateDisplay() method is not updating
> the drawing container for some other reason.
>
> In the attached file, I took the GUIDisplay example and then inserted
> the ColorWheel example's drawing code into the handler for the Blue
> Button and as my students experienced, only one display update
> occurred
> at the very end.
>
> What am I not seeing or understanding?
>
> Thanks,
>
> John
>
>
>
> ------------------------ Yahoo! Groups Sponsor --------------------
> ~-->
> Get Bzzzy! (real tools to help you find a job). Welcome to the
> Sweet Life.
> http://us.click.yahoo.com/A77XvD/vlQLAA/TtwFAA/5cFolB/TM
> --------------------------------------------------------------------
> ~->
>
>
> Yahoo! Groups Links
>
>
>
>

--
Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com





Thu Sep 8, 2005 1:19 pm

btschumy
Offline Offline
Send Email Send Email

Forward
Message #108 of 119 |
Expand Messages Author Sort by Date

Howdy Bill, For the past few classes, I've been going over GUI interface programming with my students. I started them off with the slider example (PolySpiral),...
John Kirkilis
johnkirkilis
Offline Send Email
Sep 8, 2005
4:39 am

John, This is normal when executing code on the Event Thread. Because the event thread is used for both events and drawing updates, it is somewhat tricky to...
Bill Tschumy
btschumy
Offline Send Email
Sep 8, 2005
1:19 pm

Bill, I was afraid you were going to say that, but since you've provided me with a working example, I'm glad. I've talked about multi-threading to my students...
John Kirkilis
johnkirkilis
Offline Send Email
Sep 9, 2005
12:34 am

... Yes, that's it exactly. If you don't need to see the intermediate drawing there is no need for a separate thread. Once you return from the slider's event...
Bill Tschumy
btschumy
Offline Send Email
Sep 9, 2005
1:19 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help