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...
Message search is now enhanced, find messages faster. Take it for a spin.

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
Multi-Threaded Wonders   Message List  
Reply | Forward Message #111 of 119 |
Hi Bill,

With the changes you suggested to create a separate drawing thread, my
students found that if they changed the number of iterations in the
ColorWheel code from 360 to something like 3600 which goes around 10
times and pressed the Blue button several times in succession, they
could get several drawing threads all updating the screen at the same
time which is entertaining. They also discovered that if hitting the
STOP button in Jurtle appears to kill off one of the threads while
leaving the remaining ones running. If you've got a lot of drawing
threads, then it takes a lot of clicks to terminate the program. It also
appears that the CPU stays pegged even though control has returned to
Jurtle. Is this the intended behavior or should the STOP button stop all
active user threads?

- John

p.s. Now my students want to have separate threads drawing in different
areas of the display. We've created a monster!

Here's the code:

/**
* 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 GUIDisplayReloaded 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 < 36000; 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 entire wheel.

if ( ( i % 10 ) == 0 )
updateDisplay();
}
updateDisplay();
}
}
}




Sat Sep 10, 2005 9:28 pm

johnkirkilis
Offline Offline
Send Email Send Email

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

Hi Bill, With the changes you suggested to create a separate drawing thread, my students found that if they changed the number of iterations in the ColorWheel...
John Kirkilis
johnkirkilis
Offline Send Email
Sep 10, 2005
9:24 pm

The Stop button will only stop the Turtle thread. Any other threads that you may start are unknown to Jurtle and must terminate on their own (the normal case)...
Bill Tschumy
btschumy
Offline Send Email
Sep 10, 2005
9:40 pm

Bill, Well, this isn't the behavior we're seeing. If we press the blue button 4 times, it takes 5 presses of the STOP button ( 1 for each spawned thread and 1...
John Kirkilis
johnkirkilis
Offline Send Email
Sep 13, 2005
12:09 am

John, You guys are definitely treading in uncharted waters. Jurtle wasn't really designed with the idea of non-Turtle threads using the Turtle API calls. I...
Bill Tschumy
btschumy
Offline Send Email
Sep 13, 2005
1:09 am
Advanced

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