下面列出了怎么用java.awt.geom.Rectangle2D.Double的API类实例代码及写法,或者点击链接到github查看源代码。
public void repaintBuildMode(Graphics g, MouseLocationProvider mouseLocationProvider, int offsetX, int offsetY, WorldObject playerCharacter, World world) {
if (buildMode) {
Graphics2D g2 = (Graphics2D) g;
Point mouseLocation = mouseLocationProvider.getMouseLocation();
Double rectangleToDraw = getRectangleToDraw(mouseLocation);
final Color color;
WorldObject buildLocation = getBuildLocation(mouseLocation, offsetX, offsetY);
if (isbuildActionPossible(playerCharacter, world, buildLocation)) {
color = Color.GREEN;
g.drawImage(okImage, (int)rectangleToDraw.x, (int)rectangleToDraw.getY(), null);
} else {
color = Color.RED;
g.drawImage(notOkImage, (int)rectangleToDraw.x, (int)rectangleToDraw.getY(), null);
}
g2.setColor(color);
g2.draw(rectangleToDraw);
}
}
@Override
protected void renderOutline( IPrimitiveRenderer renderer,
LineAttributes lineAttribs, Double rect )
{
if ( lineAttribs != null
&& lineAttribs.isVisible( )
&& lineAttribs.getColor( ) != null )
{
SVGGraphics2D g2d = (SVGGraphics2D) ( (IDeviceRenderer) renderer ).getGraphicsContext( );
Stroke sPrevious = null;
final ColorDefinition cd = lineAttribs.getColor( );
final Stroke sCurrent = ( (SVGRendererImpl) renderer ).getCachedStroke( lineAttribs );
if ( sCurrent != null ) // SOME STROKE DEFINED?
{
sPrevious = g2d.getStroke( );
g2d.setStroke( sCurrent );
}
g2d.setColor( (Color) _sxs.getColor( cd ) );
g2d.draw( rect );
g2d.setNoFillColor( g2d.getCurrentElement( ) );
if ( sPrevious != null ) // RESTORE PREVIOUS STROKE
{
g2d.setStroke( sPrevious );
}
}
}
private Rectangle2D.Double getRectangleToDraw(Point mouseLocation) {
int x = ((mouseLocation.x) / 48) * 48;
int y = ((mouseLocation.y) / 48) * 48;
int width = buildAction.getWidth() * 48;
int height = buildAction.getHeight() * 48;
return new Rectangle2D.Double(x, y, width, height);
}
private static void appendDescription(StringBuilder sb,
StringTreeNode node,
Map<StringTreeNode, Rectangle2D.Double> nodeBounds) {
Rectangle2D rect = nodeBounds.get(node);
sb.append(String.format("%s @ %d,%d (%dx%d)\n", node.getText(),
(int) rect.getX(), (int) rect.getY(), (int) rect.getWidth(),
(int) rect.getHeight()));
for (StringTreeNode child : node.getChildren()) {
appendDescription(sb, child, nodeBounds);
}
}
public static void dump(StringTreeNode node,
Map<StringTreeNode, Rectangle2D.Double> nodeBounds, String prefix) {
Rectangle2D rect = nodeBounds.get(node);
System.out.println(String.format("%s%s @ %d,%d (%dx%d)", prefix,
node.getText(), (int) rect.getX(), (int) rect.getY(),
(int) rect.getWidth(), (int) rect.getHeight()));
for (StringTreeNode child : node.getChildren()) {
dump(child, nodeBounds, prefix + " ");
}
}
@Test
public void testGetNodeBoundsIsCached() {
StringTreeNode root = new StringTreeNode("root");
StringTreeAsTreeForTreeLayout tree = new StringTreeAsTreeForTreeLayout(
root);
FixedNodeExtentProvider<StringTreeNode> nodeExtentProvider = new FixedNodeExtentProvider<StringTreeNode>();
DefaultConfiguration<StringTreeNode> config = new DefaultConfiguration<StringTreeNode>(
10, 20);
TreeLayout<StringTreeNode> layout = new TreeLayout<StringTreeNode>(
tree, nodeExtentProvider, config);
Map<StringTreeNode, Double> nodeBounds = layout.getNodeBounds();
assertEquals(nodeBounds, layout.getNodeBounds());
}
public DrawVisitorPrintable(Drawing drawing, Double world) {
this.drawing = drawing;
this.world = world;
}
public Rectangle2D getCellRect(int px, int py) {
return new Double(getX() + px * cellWidth, getY() + py * cellHeight, cellWidth, cellHeight);
}
/**
* Calculates the bounding box of the node.
*
* @return The bounding box of the node.
*/
@Override
public Double getBoundingBox() {
return m_realizer.getBoundingBox();
}
/**
* Returns the bounding box of the node.
*
* @return The bounding box of the node.
*/
Double getBoundingBox();
Double getBoundingBox();