* each possible anchor point. Note that the labels appear to
* be in the wrong locations: but they are the anchor of the
* label itself, not the displayed square, so they are correct.
*/
public void createLabels() {
FigureLayer layer = graphicsPane.getForegroundLayer();
// The data to display
int[] anchors = { SwingConstants.CENTER, SwingConstants.NORTH,
SwingConstants.NORTH_EAST, SwingConstants.EAST,
SwingConstants.SOUTH_EAST, SwingConstants.SOUTH,
SwingConstants.SOUTH_WEST, SwingConstants.WEST,
SwingConstants.NORTH_WEST };
String[] labels = { "center", "north", "north-east", "east",
"south-east", "south", "south-west", "west", "north-west" };
String[] fonts = { "Dialog", "DialogInput", "Monospaced", "Serif",
"SansSerif", "Symbol", "Times", "Courier", "Helvetica" };
int[] styles = { Font.PLAIN, Font.BOLD, Font.ITALIC,
Font.BOLD | Font.ITALIC, Font.PLAIN, Font.BOLD, Font.ITALIC,
Font.BOLD | Font.ITALIC, Font.PLAIN };
// Draw a rectangle to position them
BasicRectangle square = new BasicRectangle(160, 80, 120, 120);
square.setStrokePaint(Color.gray);
layer.add(square);
// Create the labels
for (int i = 0; i < anchors.length; i++) {
LabelFigure labelFigure = new LabelFigure(labels[i], fonts[i],
styles[i], 20);
// Set the anchor
labelFigure.setAnchor(anchors[i]);
// Move the anchor to the right location
Point2D pt = CanvasUtilities.getLocation(square.getBounds(),
CanvasUtilities.reverseDirection(anchors[i]));
labelFigure.translateTo(pt);
// Add to the layer
layer.add(labelFigure);
labelFigure.setInteractor(defaultInteractor);
// Draw a small circle there so we can see it
Figure mark = new BasicEllipse(pt.getX() - 2, pt.getY() - 2, 4, 4,
Color.red);
layer.add(mark);
}
}