Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 7
The API also includes constants DEFAULT_DELAY, MIN_DELAY, MAX_DELAY, and STEP, which can be conveniently used with a GUI component (such as a slider) to let the user determine animation speed. An IllegalArgumentException is thrown for any value passed to setDelay() that lies outside the MIN_DELAY/MAX_DELAY range.
| Caution! |
|---|
For convenience, I initialize depth to -1 at the beginning of animThd's run() method. Although depth will not normally contain -1 when the component is repainted, it could happen. You should therefore treat any depth value less than or equal to 0 as the fractal recursion's stopping condition.
|
Although it is possible to place the FractalGenerator interface and FractalAnimator class into their own package (they would need to be marked public), I have not done so -- consider this an exercise. Instead, I've embedded them into fractal applets. Each applet's public void createGUI() method creates the fractal generator and fractal animator, and integrates the fractal animator into the applet's GUI, as
Listing 3 shows.
Listing 3. public void createGUI() creates the fractal infrastructure
private void createGUI ()
{
getContentPane ().setLayout (new GridBagLayout ());
final FractalAnimator fa;
fa = new FractalAnimator (new FractalGenerator ());
fa.setBorder (BorderFactory.createEtchedBorder ());
int size = Math.min (getWidth ()/2, getHeight ()/2);
fa.setPreferredSize (new Dimension ((int) (size*1.15), size));
GridBagConstraints gbc = new GridBagConstraints ();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
getContentPane ().add (fa, gbc);
JLabel lbl = new JLabel ("Delay (milliseconds)");
gbc = new GridBagConstraints ();
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.insets = new Insets (10, 10, 10, 10);
getContentPane ().add (lbl, gbc);
sliderMS = new JSlider (JSlider.HORIZONTAL,
FractalAnimator.MIN_DELAY,
FractalAnimator.MAX_DELAY,
FractalAnimator.DEFAULT_DELAY);
sliderMS.setMinorTickSpacing (FractalAnimator.STEP/2);
sliderMS.setMajorTickSpacing (FractalAnimator.STEP);
sliderMS.setPaintTicks (true);
sliderMS.setPaintLabels (true);
sliderMS.setLabelTable (sliderMS.createStandardLabels (FractalAnimator.STEP));
Dimension prefSize = sliderMS.getPreferredSize ();
prefSize.width += prefSize.width/2;
sliderMS.setPreferredSize (prefSize);
ChangeListener cl;
cl = new ChangeListener ()
{
public void stateChanged (ChangeEvent e)
{
fa.setDelay (sliderMS.getValue ());
}
};
sliderMS.addChangeListener (cl);
gbc = new GridBagConstraints ();
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
gbc.insets = new Insets (10, 10, 10, 10);
getContentPane ().add (sliderMS, gbc);
JPanel pnl = new JPanel ();
btnAnimate = new JButton ("Animate");
ActionListener al;
al = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
fa.start ();
btnAnimate.setEnabled (false);
btnStop.setEnabled (true);
sliderMS.setEnabled (false);
}
};
btnAnimate.addActionListener (al);
pnl.add (btnAnimate);
btnStop = new JButton ("Stop");
btnStop.setEnabled (false);
al = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
fa.stop ();
btnAnimate.setEnabled (true);
btnStop.setEnabled (false);
sliderMS.setEnabled (true);
}
};
btnStop.addActionListener (al);
pnl.add (btnStop);
gbc = new GridBagConstraints ();
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.gridheight = 1;
getContentPane ().add (pnl, gbc);
}
The createGUI() method uses the java.awt.GridBagLayout class and its java.awt.GridBagConstraints support class to lay out the GUI. After installing this layout manager, createGUI() creates a FractalAnimator, passing an object whose class implements FractalGenerator to the constructor. Figure 1 presents the GUI.
Archived Discussions (Read only)