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 and the event listener model, so we'll just get to dive into
in a few weeks earlier.
I'm still not quite sure why your PolySpiral example with the JSlider
works just fine without needing to spawn a drawing thread. Is it because
a complete spiral is drawn in response to a slider action with no
intermediate updates being requested?
Thanks again for all of your help.
- John
Bill Tschumy wrote:
> 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
>
>
>
> ------------------------------------------------------------------------
> YAHOO! GROUPS LINKS
>
> * Visit your group "jurtle-users
> <http://groups.yahoo.com/group/jurtle-users>" on the web.
>
> * To unsubscribe from this group, send an email to:
> jurtle-users-unsubscribe@yahoogroups.com
> <mailto:jurtle-users-unsubscribe@yahoogroups.com?subject=Unsubscribe>
>
> * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> Service <http://docs.yahoo.com/info/terms/>.
>
>
> ------------------------------------------------------------------------
>