com.ccg.swing
Class Corner

java.lang.Object
  extended by com.ccg.swing.Corner
All Implemented Interfaces:
Cloneable

public class Corner
extends Object
implements Cloneable

Class which encapsulates many of the necessary parts of determining coordinates when one wants to draw an object snug in a corner.

This class is designed to aid one in drawing things at various "corners" of a drawing area. In particular it provides enough infrastructure to build GUI components which allow a user to "place" a legend or other item at some corner on your drawing (or even disable).

The following example demonstrates how this might be used:

import com.ccg.swing.Corner;

import javax.swing.*;
import java.awt.*;

public class CornerTest extends JComponent {
  private Corner[] _Corners;
  private Color[] _Colors;

  CornerTest() {
    String[] cname = Corner.getNames();
    int n = cname.length;
    _Corners = new Corner[n];
    _Colors = new Color[] {
      Color.yellow,
      Color.red,
      Color.green,
      Color.blue,
      Color.gray
    };

                                // init array, then try to use each
                                // possible setting
    for (int i = 0; i < n; i++) _Corners[i] = new Corner();
    for (int i = 0; i < n; i++) try {
      _Corners[i].setCornerType(Corner.stringToCorner(cname[i]));
    } catch (Exception e) {
                                // should never happen - since we used
                                // good names!
      e.printStackTrace();
    }
  }


  public void paint(Graphics g) {
    FontMetrics fm = g.getFontMetrics();

    int gw = getWidth();        // size of component
    int gh = getHeight();
    g.setColor(Color.black);
    g.fillRect(0,0,gw,gh);

                                // maximum height of text
    int ascent = fm.getMaxAscent();
    int th = fm.getMaxDescent()+ascent;

    int n = _Corners.length;

    for (int i = 0; i < n; i++) {
      Corner c = _Corners[i];
      if (c.isDisabled()) continue;

      g.setColor(_Colors[i%_Colors.length]);
      String text = c.toString();
      int tw = fm.stringWidth(text);

                                // user Corner to compute (x,y)
      int x = c.getX(tw,gw);
                                // note, Corner assumes (x,y) is NE
                                // corner of object to be drawn, we
                                // have to add text ascent to
                                // to move text to this position
      int y = c.getY(th,gh)+ascent;

      g.drawString(text,x,y);   // and go draw text
    }
  }

  public static void main(String[] args) {
    JFrame f = new JFrame("Corner Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(new CornerTest());
    f.setSize(600,400);
    f.show();
  }

}

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $

Field Summary
static int DISABLE
          Constant indicating that item should not be drawn (it has been turned off).
static int NORTHEAST
          Constant indicating that item should be drawn in the north east corner.
static int NORTHWEST
          Constant indicating that item should be drawn in the north west corner.
static int SOUTHEAST
          Constant indicating that item should be drawn in the south east corner.
static int SOUTHWEST
          Constant indicating that item should be drawn in the south west corner.
 
Constructor Summary
Corner()
          Default constructor (assumes object will be in south west).
Corner(int corner)
          Constructor which allows one to specify which corner to occupy.
 
Method Summary
 Object clone()
          Returns a duplicate clone of the object.
static String cornerToString(int c)
          Get string name of a corner constant (note this may be localized).
 int getCornerType()
          Get which corner the calculations should be made from.
static int[] getMap()
          Get the "map" which indicates what corner constants the string representations map to.
static String[] getNames()
          Get the string representations of ALL of the legal corner constants.
 boolean getPoint(Point p, int cw, int ch, int dw, int dh)
          Determine whether item should be drawn, and if so, determine where it should be drawn.
 int getX(int cw, int dw)
          Determine x coordinate to draw object at.
 int getY(int ch, int dh)
          Determine y coordinate to draw object at.
 boolean isDisabled()
          Determine if object is to be drawn (returnes true unless disabled.
 void setCornerType(int val)
          Set which corner the calculations should be made from.
static int stringToCorner(String val)
          Convert a string value to one of the corner constants.
 String toString()
          Get string name the assigned corner constant (note this may be localized).
static void validateCorner(int val)
          Check to see if integer value is a valid Corner constant.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DISABLE

public static final int DISABLE
Constant indicating that item should not be drawn (it has been turned off).

Since:
1.0
See Also:
setCornerType(int), Constant Field Values

NORTHEAST

public static final int NORTHEAST
Constant indicating that item should be drawn in the north east corner.

Since:
1.0
See Also:
setCornerType(int), Constant Field Values

NORTHWEST

public static final int NORTHWEST
Constant indicating that item should be drawn in the north west corner.

Since:
1.0
See Also:
setCornerType(int), Constant Field Values

SOUTHEAST

public static final int SOUTHEAST
Constant indicating that item should be drawn in the south east corner.

Since:
1.0
See Also:
setCornerType(int), Constant Field Values

SOUTHWEST

public static final int SOUTHWEST
Constant indicating that item should be drawn in the south west corner.

Since:
1.0
See Also:
setCornerType(int), Constant Field Values
Constructor Detail

Corner

public Corner()
Default constructor (assumes object will be in south west).

Since:
1.0

Corner

public Corner(int corner)
       throws IllegalArgumentException
Constructor which allows one to specify which corner to occupy.

Use this method to set the desired position where drawing should occur.

Parameters:
val - New int value to assign - must be DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
Throws:
IllegalArgumentException - If you attempt to set a unrecognized value.
Since:
1.0
Method Detail

validateCorner

public static void validateCorner(int val)
                           throws IllegalArgumentException
Check to see if integer value is a valid Corner constant.

Parameters:
val - Value to verify - must be DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
Throws:
IllegalArgumentException - If value passed is not valid.
Since:
1.0

setCornerType

public void setCornerType(int val)
                   throws IllegalArgumentException
Set which corner the calculations should be made from.

Use this method to set the desired position where drawing should occur.

Parameters:
val - New int value to assign - must be DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
Throws:
IllegalArgumentException - If you attempt to set a unrecognized value.
See Also:
getCornerType()

getCornerType

public int getCornerType()
Get which corner the calculations should be made from.

Returns:
Current int value assigned.
See Also:
setCornerType(int)

isDisabled

public final boolean isDisabled()
Determine if object is to be drawn (returnes true unless disabled.

Returns:
true if not disabled.
Since:
1.0

getX

public final int getX(int cw,
                      int dw)
Determine x coordinate to draw object at.

This method can be used if you know that the Corner object has not be disabled. It computes the correct X value based upon its internal setting and the width of your component to be drawn and the width of the destination target.

Parameters:
cw - Width of the item to be drawn.
dw - Width of destination area to be drawn to.
Returns:
The X location to draw the item at (returns 0 if disabled).
Since:
1.0
See Also:
getPoint(java.awt.Point, int, int, int, int), isDisabled()

getY

public final int getY(int ch,
                      int dh)
Determine y coordinate to draw object at.

This method can be used if you know that the Corner object has not be disabled. It computes the correct Y value based upon its internal setting and the height of your component to be drawn and the height of the destination target.

Parameters:
ch - Height of the item to be drawn.
dh - Height of destination area to be drawn to.
Returns:
The Y location to draw the item at (returns 0 if disabled).
Since:
1.0
See Also:
getPoint(java.awt.Point, int, int, int, int), isDisabled()

getPoint

public final boolean getPoint(Point p,
                              int cw,
                              int ch,
                              int dw,
                              int dh)
Determine whether item should be drawn, and if so, determine where it should be drawn.

Parameters:
p - Where to store the (x,y) coordinates to properly draw the component (or other object).
cw - Width of the item to be drawn.
ch - Height of the item to be drawn.
dw - Width of destination area to be drawn to.
ch - Width of the destination area to be drawn to.
Returns:
true if not disabled.
Since:
1.0

clone

public Object clone()
Returns a duplicate clone of the object.

Overrides:
clone in class Object
Returns:
A clone of the current corner.
Since:
1.0

stringToCorner

public static int stringToCorner(String val)
Convert a string value to one of the corner constants.

This attempts to convert a String value to its corresponding integer value. It either succeeds or throws an exception. The string value must match one of the valid names, or be a numeric number that evaluates to a valid constant (like "3").

Parameters:
val - String to convert.
val - Constant corresponding to the string value, one of: DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
Throws:
IllegalArgumentException - If you attempt to set a unrecognized value.
Since:
1.0

cornerToString

public static String cornerToString(int c)
                             throws IllegalArgumentException
Get string name of a corner constant (note this may be localized).

Parameters:
c - Corner constant to convert to a string value, one of: DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
Returns:
String representation of the integer constant passed.
Throws:
IllegalArgumentException - If you pass a illegal value.
Since:
1.0

getNames

public static String[] getNames()
Get the string representations of ALL of the legal corner constants.

Returns an array of all of the values recognized by the stringToCorner(java.lang.String) method. Use the getMap method if you need to know what constant value each string maps to.

Returns:
Array of all legal string values.
Since:
1.0
See Also:
getMap()

getMap

public static int[] getMap()
Get the "map" which indicates what corner constants the string representations map to.

If you need to be able to map string names (returned from getNames) directly to corner constants, you can use this method to retrieve the mappings. Unless you need the mappings in an array form, it is probably simpler to just use the stringToCorner method.

Returns:
Array of what the corner names are mapped to.
Since:
1.0
See Also:
getNames()

toString

public String toString()
Get string name the assigned corner constant (note this may be localized).

Overrides:
toString in class Object
Returns:
String representation of the object.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.