HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Cascading JFrames"
Volume: 4 Issue: 5, p. 56

	

Listing 1.
 
// Cascade.java  Copyright 1998 David J. Anderson 

import java.io.Serializable; 

/** A Cascade implements a stepable position in a one-dimensional offset 
  wrapping-space. 
  The instance computes a new int location and increments it on demand. 
  The four int parameters which control the instance are 
  <UL> 
  <LI>the <I>offset</I> from the origin, 
  <LI>the current <I>position</I> relative to the offset, 
  <LI>the <I>step</I> which is added to the position during an increment, and 
  <LI>the <I>wrap</I> which bounds the position relative to the offset. 
  </UL> 
**/ 

public class Cascade 
implements Cloneable, Serializable 
{ 
  private int offset; 
  private int position; 
  private int step; 
  private int wrap; 

  /** Creates a new Cascade with default parameters. 
  **/ 
  public Cascade () 
  { 
    this(10,0,25,300); 
  } 

  /** Creates a new Cascade with the given parameters. 
    @param offset the offset from 0 
    @param position the position from the offset 
    @param step the distance the position is incremented 
    @param wrap the basis of the position modulus 
    **/ 
  public Cascade (int offset, int position, int step, int wrap) 
  { 
    setOffset(offset); 
    setPosition(position); 
    setStep(step); 
    setWrap(wrap); 
  } 

  /** Sets the instance's offset. 
    @param offset the offset from 0 
    **/ 
  public void setOffset (int offset) 
  { 
    this.offset = offset; 
  } 

  /** Gets the instance's offset. 
    @return the offset from 0 
    **/ 
  public int getOffset () 
  { 
    return offset; 
  } 

  /** Sets the instance's position. 
    @param position the position from the offset 
    **/ 
  public void setPosition (int position) 
  { 
    this.position = position; 
  } 

  /** Gets the instance's position. 
    @return the position from the offset 
    **/ 
  public int getPosition () 
  { 
    return position; 
  } 

  /** Sets the instance's step. 
    @param step the distance the position is incremented 
    **/ 
  public void setStep (int step) 
  { 
    this.step = step; 
  } 

  /** Gets the instance's step. 
    @return the distance the position is incremented 
    **/ 
  public int getStep () 
  { 
    return step; 
  } 

  /** Sets the instance's wrap. 
    @param wrap the basis of the position modulus 
    **/ 
  public void setWrap (int wrap) 
  { 
    this.wrap = wrap; 
  } 

  /** Gets the instance's wrap. 
    @return the basis of the position modulus 
    **/ 
  public int getWrap () 
  { 
    return wrap; 
  } 

  /** Computes the instance's current location. 
    @return the location 
    **/ 
  public int location () 
  { 
    return offset+position; 
  } 

  /** Increments the instance's location, keeping it within the parameters. 
  **/ 
  public void increment () 
  { 
    position += step; 
    while (position < 0) 
      position += wrap; 
    while (position > wrap) 
      position -= wrap; 
    if (position < 0) 
      position = 0; 
  } 

  /** Compares the contents of the instance with the given Cascade's contents. 
    @return true if the contents of the instance and the given Cascade are 
    the same 
  **/ 
  public boolean equals (Cascade cascade) 
  { 
    return 
      ((cascade != null)&& 
       (getOffset() == cascade.getOffset())&& 
       (getPosition() == cascade.getPosition())&& 
       (getStep() == cascade.getStep())&& 
       (getWrap() == cascade.getWrap())); 
  } 

  /** Clones the instance. 
    @return the cloned instance 
    **/ 
  public Object clone () 
  { 
    Cascade clone = null; 
    try 
      { 
 clone = (Cascade)(super.clone()); 
      } 
    catch (CloneNotSupportedException exception) 
      { 
 throw new InternalError(exception.toString()); 
      } 
    return clone(); 
  } 
} 

Listing 2.
 
// PointCascade.java  Copyright 1998 David J. Anderson 

import java.awt.Point; 
import java.io.Serializable; 

/** A PointCascade implements a stepable position in an two-dimensional offset 
  wrapping-space. 
  The instance computes a new Point location and increments it on demand. 
  The four logical Point parameters which control the instance are 
  <UL> 
  <LI>the <I>offset</I> from the origin, 
  <LI>the current <I>position</I> relative to the offset, 
  <LI>the <I>step</I> which is added to the position during an increment, and 
  <LI>the <I>wrap</I> which bounds the position relative to the offset. 
  </UL> 
  The instance wraps access to two Cascade objects, one for the x-dimension 
  and the other for the y. 
**/ 

public class PointCascade 
implements Cloneable, Serializable 
{ 
  private Cascade xCascade; 
  private Cascade yCascade; 

  /** Creates a new PointCascade with default logical parameters. 
  **/ 
  public PointCascade () 
  { 
    setXCascade(new Cascade(10,0,25,375)); 
    setYCascade(new Cascade(10,0,25,300)); 
  } 

  /** Creates a new PointCascade with the given Cascades. 
    @param xCascade the Cascade for the x dimension 
    @param yCascade the Cascade for the y dimension 
    **/ 
  public PointCascade (Cascade xCascade, Cascade yCascade) 
  { 
    setXCascade(xCascade); 
    setYCascade(yCascade); 
  } 

  /** Creates a new Cascade with the given logical parameters. 
    @param offset the offset from 0,0 
    @param position the position from the offset 
    @param step the distance the position is incremented 
    @param wrap the basis of the position modulus 
    **/ 
  public PointCascade (Point offset, Point position, Point step, Point wrap) 
  { 
    this(); 
    setOffset(offset); 
    setPosition(position); 
    setStep(step); 
    setWrap(wrap); 
  } 

  /** Sets the instance's x dimension Cascade 
    @param xCascade the Cascade for the x dimension 
    **/ 
  public void setXCascade (Cascade xCascade) 
  { 
    this.xCascade = xCascade; 
  } 

  /** Gets the instance's x dimension Cascade 
    @return the Cascade for the x dimension 
    **/ 
  public Cascade getXCascade () 
  { 
    return xCascade; 
  } 

  /** Sets the instance's y dimension Cascade 
    @param xCascade the Cascade for the y dimension 
    **/ 
  public void setYCascade (Cascade yCascade) 
  { 
    this.yCascade = yCascade; 
  } 

  /** Gets the instance's y dimension Cascade 
    @return the Cascade for the y dimension 
    **/ 
  public Cascade getYCascade () 
  { 
    return yCascade; 
  } 

  /** Sets the instance's logical offset 
    @param offset the offset from 0,0 
    **/ 
  public void setOffset (Point offset) 
  { 
    xCascade.setOffset(offset.x); 
    yCascade.setOffset(offset.y); 
  } 

  /** Gets the instance's logical offset 
    @return the offset from 0,0 
    **/ 
  public Point getOffset () 
  { 
    return new Point(xCascade.getOffset(),yCascade.getOffset()); 
  } 

  /** Sets the instance's logical position 
    @param position the position from the offset 
    **/ 
  public void setPosition (Point position) 
  { 
    xCascade.setPosition(position.x); 
    yCascade.setPosition(position.y); 
  } 

  /** Gets the instance's logical position 
    @return the position from the offset 
    **/ 
  public Point getPosition () 
  { 
    return new Point(xCascade.getPosition(),yCascade.getPosition()); 
  } 

  /** Sets the instance's logical step 
    @param step the distance the position is incremented 
    **/ 
  public void setStep (Point step) 
  { 
    xCascade.setStep(step.x); 
    yCascade.setStep(step.y); 
  } 

  /** Gets the instance's logical step 
    @return the distance the position is incremented 
    **/ 
  public Point getStep () 
  { 
    return new Point(xCascade.getStep(),yCascade.getStep()); 
  } 

  /** Sets the instance's logical wrap 
    @param wrap the basis of the position modulus 
    **/ 
  public void setWrap (Point wrap) 
  { 
    xCascade.setWrap(wrap.x); 
    yCascade.setWrap(wrap.y); 
  } 

  /** Gets the instance's logical wrap 
    @return the basis of the position modulus 
    **/ 
  public Point getWrap () 
  { 
    return new Point(xCascade.getWrap(),yCascade.getWrap()); 
  } 

  /** Computes the instance's current location. 
    @return the location 
    **/ 
  public Point location () 
  { 
    return new Point(xCascade.location(),yCascade.location()); 
  } 

  /** Increments the instance's location, keeping it within the parameters. 
  **/ 
  public void increment () 
  { 
    xCascade.increment(); 
    yCascade.increment(); 
  } 

  /** Compares the contents of the instance with the given PointCascade's 
    contents. 
    @return true if the contents of the instance and the given pointCascade 
    are the same 
  **/ 
  public boolean equals (PointCascade pointCascade) 
  { 
    return 
      ((pointCascade != null) && 
       (getOffset().equals(pointCascade.getOffset())) && 
       (getPosition().equals(pointCascade.getPosition())) && 
       (getStep().equals(pointCascade.getStep())) && 
       (getWrap().equals(pointCascade.getWrap()))); 
  } 

  /** Clones the instance. 
    @return the cloned instance 
    **/ 
  public Object clone () 
  { 
    PointCascade clone = null; 
    try 
      { 
 clone = (PointCascade)(super.clone()); 
      } 
    catch (CloneNotSupportedException exception) 
      { 
 throw new InternalError(exception.toString()); 
      } 
    clone.xCascade = (Cascade)(xCascade.clone()); 
    clone.yCascade = (Cascade)(yCascade.clone()); 
    return clone(); 
  } 
} 

Listing 3.
 
// CascadingJFrame.java  Copyright 1998 David J. Anderson 

import com.sun.java.swing.JFrame; 
import java.awt.Point; 
import java.awt.Rectangle; 
import java.io.Serializable; 
import java.util.Hashtable; 

/** A CascadingJFrame implements an initially autopositioning JFrame using a 
  named PointCascade to assign the instance's location. 
  A Hashtable of PointCascades, keyed by name, is maintained in the class. 
  It is accessed when the CascadingJFrame is constructed to get the initial 
  location. 
  Afterwards, on the first call to setBounds, the JFrame's position is 
  adjusted to the cascaded location. 
  The autopositioning may be turned off or adjusted on an instance prior to 
  the setBounds call. 
  **/ 

public class CascadingJFrame 
extends JFrame 
implements Serializable 
{ 
  private static Hashtable pointCascadeHashtable = new Hashtable (); 
  /** The name of the default PointCascade **/ 
  public static final String defaultCascadeName = "_default_"; 

  private Point location; 
  private boolean autoposition; 

  /** Creates a new CascadingJFrame with the given title using the default 
    PointCascade. 
    @param title the text to be placed on the title bar 
  **/ 
  public CascadingJFrame (String title) 
  { 
    this(title,defaultCascadeName,true); 
  } 

  /** Creates a new CascadingJFrame with the given title using the named 
    PointCascade. 
    @param title the text to be placed on the title bar 
    @param cascadeName the name of the PointCascade to retrieve the location 
    from 
    **/ 
  public CascadingJFrame (String title, String cascadeName) 
  { 
    this(title,cascadeName,true); 
  } 

  /** Creates a new CascadingJFrame with the given title using the named 
    PointCascade. 
    @param title the text to be placed on the title bar 
    @param cascadeName the name of the PointCascade to retrieve the location 
    from 
    @param autoposition if false, then no autopositioning will be done for the 
    instance 
    **/ 
  public CascadingJFrame 
  (String title, String cascadeName, boolean autoposition) 
  { 
    super(title); 
    if (autoposition) 
      setLocation(cascadeName); 
    setAutoposition(autoposition); 
  } 

  /** Sets the default PointCascade to the given PointCascade. 
    @param pointCascade the new default PointCascade 
    **/ 

  public static void setPointCascade (PointCascade pointCascade) 
  { 
    setPointCascade(defaultCascadeName,pointCascade); 
  } 

  /** Sets the named PointCascade to the given PointCascade. 
    @param cascadeName the name of the PointCascade 
    @param pointCascade the new PointCascade 
    **/ 
  public static void setPointCascade 
  (String cascadeName, PointCascade pointCascade) 
  { 
    pointCascadeHashtable.put(cascadeName,pointCascade); 
  } 

  /** Gets the default PointCascade. 
    @return the default PointCascade 
    **/ 
  public static PointCascade getPointCascade () 
  { 
    return CascadingJFrame.getPointCascade(defaultCascadeName); 
  } 

  /** Gets the named PointCascade. 
    If a point cascade by that name does not exist, a new one is created. 
    @param cascadeName the name of the PointCascade 
    @return the named PointCascade 
    **/ 
  public static PointCascade getPointCascade (String cascadeName) 
  { 
    if (cascadeName == null) 
      cascadeName = defaultCascadeName; 
    PointCascade pointCascade = 
      (PointCascade)(pointCascadeHashtable.get(cascadeName)); 
    if (pointCascade == null) 
      { 
 pointCascade = new PointCascade(); 
 CascadingJFrame.setPointCascade(cascadeName,pointCascade); 
      } 
    return pointCascade; 
  } 

  /** Sets whether the instance will be autopositioned. 
    @param autoposition true if autopositioning is desired 
    **/ 
  public void setAutoposition (boolean autoposition) 
  { 
    this.autoposition = autoposition; 
  } 

  /** Gets whether the instance will be autopositioned. 
    @return true if autopositioning will occur 
    **/ 
  public boolean getAutoposition () 
  { 
    return autoposition; 
  } 

  /** Sets the instance's location by retrieving it from the named 
    PointCascade. 
    @param cascadeName the name of the PointCascade 
    **/ 
  public void setLocation (String cascadeName) 
  { 
    PointCascade pointCascade = getPointCascade(cascadeName); 
    setLocation(pointCascade.location()); 
    pointCascade.increment(); 
  } 

  /** Sets the instance's location to the specified Point. 
    @param location the new location 
    **/ 
  public void setLocation (Point location) 
  { 
    this.location = location; 
  } 

  /** Gets the instance's location. 
    @return the location 
    **/ 
  public Point getLocation () 
  { 
    return location; 
  } 

  /** Sets the instance's bounds, autopositioning if intended. 
    @param x the new x coordinate of the upper left position 
    @param y the new y coordinate of the upper left position 
    @param width the new width of the instance 
    @param height the new height of the instance 
    **/ 
  public void setBounds (int x, int y, int width, int height) 
  { 
    if (getAutoposition() == true) 
      { 
 Point frameLocation = getLocation(); 
 x = frameLocation.x; 
 y = frameLocation.y; 
 setAutoposition(false); 
      } 
    super.setBounds(x,y,width,height); 
  } 

  /** Sets the instance's bounds, autopositioning if intended. 
    @param r the new bounds of the instance 
    **/ 
  public void setBounds (Rectangle r) 
  { 
    setBounds(r.x,r.y,r.width,r.height); 
  } 
} 
  



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.