Label extends AbstractDrawable { public void draw(DrawingContext context) { boolean wordWrap = isWordWrapEnabled(); Shape labelShape = getCachedOutline(wordWrap); if (labelShape == null) { return; } Rectangle2D textBounds = labelShape.getBounds2D(); double rotation = getRotation(); if (MathUtils.isCalculatable(rotation) && rotation != 0.0) { AffineTransform txLabelText = AffineTransform.getRotateInstance( Math.toRadians(-rotation), textBounds.getCenterX(), textBounds.getCenterY() ); labelShape = txLabelText.createTransformedShape(labelShape); textBounds = labelShape.getBounds2D(); } Graphics2D graphics = context.getGraphics(); AffineTransform txOld = graphics.getTransform(); Paint background = getBackground(); if (background != null) { GraphicsUtils.fillPaintedShape(graphics, getBounds(), background, null); } double shapePosX = getX() - textBounds.getX(); double shapePosY = getY() - textBounds.getY(); double alignmentX = getAlignmentX(); double alignmentY = getAlignmentY(); shapePosX += alignmentX*(getWidth() - textBounds.getWidth()); shapePosY += alignmentY*(getHeight() - textBounds.getHeight()); graphics.translate(shapePosX, shapePosY); Paint paint = getColor(); GraphicsUtils.fillPaintedShape(graphics, labelShape, paint, null); graphics.setTransform(txOld); } Label();  Label(String text); void draw(DrawingContext context); @Override Dimension2D getPreferredSize(); Rectangle2D getTextRectangle(); String getText(); void setText(String text); @Override void setBounds(double x, double y, double width, double height); double getAlignmentX(); void setAlignmentX(double alignmentX); double getAlignmentY(); void setAlignmentY(double alignmentY); Font getFont(); void setFont(Font font); double getRotation(); void setRotation(double angle); Paint getColor(); void setColor(Paint color); double getTextAlignment(); void setTextAlignment(double textAlignment); boolean isWordWrapEnabled(); void setWordWrapEnabled(boolean wordWrapEnabled); Paint getBackground(); void setBackground(Paint background); @Override boolean equals(Object obj);  }
@Test public void testDraw() { MockLabel empty = new MockLabel(); MockLabel text = new MockLabel("foobar"); MockLabel rotated = new MockLabel("foobar"); rotated.setRotation(45.0); MockLabel[] labels = { empty, text, rotated }; for (MockLabel label : labels) { BufferedImage image = createTestImage(); label.setBounds(0.0, 0.0, image.getWidth(), image.getHeight()); DrawingContext context = new DrawingContext((Graphics2D) image.getGraphics()); label.draw(context); assertTrue(label.isDrawn); if (!label.getText().isEmpty()) { assertNotEmpty(image); } } }