|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.Corner
public class Corner
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();
}
}
| 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 |
|---|
public static final int DISABLE
setCornerType(int),
Constant Field Valuespublic static final int NORTHEAST
setCornerType(int),
Constant Field Valuespublic static final int NORTHWEST
setCornerType(int),
Constant Field Valuespublic static final int SOUTHEAST
setCornerType(int),
Constant Field Valuespublic static final int SOUTHWEST
setCornerType(int),
Constant Field Values| Constructor Detail |
|---|
public Corner()
public Corner(int corner)
throws IllegalArgumentException
Use this method to set the desired position where drawing should occur.
val - New int value to assign - must be DISABLE,
NORTHEAST, NORTHWEST,
SOUTHEAST, or SOUTHWEST.
IllegalArgumentException - If you attempt to set a unrecognized value.| Method Detail |
|---|
public static void validateCorner(int val)
throws IllegalArgumentException
val - Value to verify - must be DISABLE,
NORTHEAST, NORTHWEST,
SOUTHEAST, or SOUTHWEST.
IllegalArgumentException - If value passed is not valid.
public void setCornerType(int val)
throws IllegalArgumentException
Use this method to set the desired position where drawing should occur.
val - New int value to assign - must be DISABLE,
NORTHEAST, NORTHWEST,
SOUTHEAST, or SOUTHWEST.
IllegalArgumentException - If you attempt to set a unrecognized value.getCornerType()public int getCornerType()
setCornerType(int)public final boolean isDisabled()
disabled.
public final int getX(int cw,
int dw)
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.
cw - Width of the item to be drawn.dw - Width of destination area to be drawn to.
getPoint(java.awt.Point, int, int, int, int),
isDisabled()
public final int getY(int ch,
int dh)
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.
ch - Height of the item to be drawn.dh - Height of destination area to be drawn to.
getPoint(java.awt.Point, int, int, int, int),
isDisabled()
public final boolean getPoint(Point p,
int cw,
int ch,
int dw,
int dh)
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.
public Object clone()
clone in class Objectpublic static int stringToCorner(String val)
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").
val - String to convert.val - Constant corresponding to the string value, one of: DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
IllegalArgumentException - If you attempt to set a unrecognized value.
public static String cornerToString(int c)
throws IllegalArgumentException
c - Corner constant to convert to a string value, one of: DISABLE, NORTHEAST, NORTHWEST, SOUTHEAST, or SOUTHWEST.
IllegalArgumentException - If you pass a illegal value.public static String[] getNames()
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.
getMap()public static int[] getMap()
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.
corner names are mapped to.getNames()public String toString()
toString in class Object
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||