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();
}
}
}