At the forefront of Artificial Intelligence
  Home Articles Reviews Interviews JDK Glossary Features Discussion Search
Home » Articles » Programming » Java

Using the Generation5 JDK's VisStepApplet

VisStepApplet (org.generation5.swing) is designed to help prototype, visualize and animate classes in applets. To use VisStepApplet, derive your applet from it and use setSteppable and setVisualizable methods and the class will take care of the rest.

Simple Example

Let us look at how easy it is to create a VisStepApplet. For this example, we will create an applet that animates a circle with a gradient of colours.

import java.awt.*;

import org.generation5.*;
import org.generation5.util.*;
import org.generation5.swing.*;

public class StepDemoApplet extends VisStepApplet {
    
    public StepDemoApplet() {
    }
    
    public void init() {
        super.init();  // Always call super.init()
        
        setSteppable(stepDemo);
        setVisualizable(stepDemo);
    }
    
    public StepDemo stepDemo = new StepDemo();

Here we declare our applet to extend VisStepApplet. All VisStepApplets should override the init function to set the steppable and visualizable content. Firstly though, the function should always call the super's own init function, since this initializes a range of other variables as well as loading common applet parameters.

If your data object already implemented the Visualizable and Steppable interfaces, then this was all that would be required. For our demonstration though, we will write a small inner class:

    public static class StepDemo implements Visualizable, Steppable {
        private int gradientPosition = 0;
        
        public StepDemo() {
            gradient.addPoint(new Color(64,0,128));
            gradient.addPoint(new Color(255,0,128));
            gradient.addPoint(new Color(255,255,128));
            gradient.addPoint(Color.WHITE);
            gradient.addPoint(new Color(64,0,128));
            gradient.createGradient();
        }
        
        public void doStep() {
            gradientPosition = (gradientPosition + 1) % 256;
        }
        
        public void init() {
            gradientPosition = 0;
        }
        
        public void render(java.awt.Graphics g, int width, int height) {
            g.setColor(gradient.getColour(gradientPosition));
            g.fillOval(10, 10, width-20, height-20);
            g.setColor(Color.BLACK);
            g.drawOval(10, 10, width - 21, height - 21);
        }
        
        public void reset() {
            init();
        }
        
        public void writeImage(String s, int width, int height) {
        }
        
        Gradient gradient = new Gradient();
    }
}

The StepDemo inner class is fairly self-explanatory: the class renders a circle using the gradient colour specified by gradientPosition, which in turn is incremented by doStep. Take a look at the demonstration applet built from this code.

Further Tips

Separating Visualization and Stepping

Remember that although many times your calls to setSteppable and setVisualizable will use the same object, it needn't. For example, the Kohonen applet uses VisStepApplet, but assigns the kohonen network object to the steppable portion, and an instance of Plot2D as the visualization, so that the network weights can be plotted.

Customized Listeners

VisStepApplet also has a powerful feature that allows you to customize the event handlers for the start button, step button, reset button, timer and mouse clicks. All this event code is handled by an instance of VisStepListener called actions. For example, the Kohonen applet overrides the default behaviour slightly, so that the applet automatically stops when the network has finished training:

class KohonenActions extends DefaultActions {
    public void timer(java.awt.event.ActionEvent evt) {
        if (steppable != null) steppable.doStep();

        if (kohonenNet.hasFinished()) setStartStop(false);
            
        visualizationPanel.repaint();
    }        
}

// in the init() function of the applet
actions = new KohonenActions();

Note how the class derives from DefaultActions not VisStepListener so all the other event handlers remain their defaults.

Submitted: 03/10/2004

Article content copyright © James Matthews, 2004.
 Article Toolbar
Print
BibTeX entry

Search

Latest News
- Generation5 10-year Anniversary (03/09/2008)
- New Generation5 Design! (09/04/2007)
- Happy New Year 2007 (02/01/2007)
- Where has Generation5 Gone?! (04/11/2005)
- NeuroEvolving Robotic Operatives (NERO) (25/06/2005)

What's New?
- Back-propagation using the Generation5 JDK (07/04/2008)
- Hough Transforms (02/01/2008)
- Kohonen-based Image Analysis using the Generation5 JDK (11/12/2007)
- Modelling Bacterium using the JDK (19/03/2007)
- Modelling Bacterium using the JDK (19/03/2007)


All content copyright © 1998-2007, Generation5 unless otherwise noted.
- Privacy Policy - Legal - Terms of Use -