### Question:
Java2DStringUtilities { public static void drawString(Graphics2D g, Alignment alignment, int x, int y, String text) { Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(text, g); g.drawString(text, alignment.stringRightSide(stringBounds, x), alignment.stringBaseline(stringBounds, y)); }  static void drawString(Graphics2D g, Alignment alignment, int x, int y, String text);  }### Answer:
@Test public void drawCenteredText1() throws Exception { if (!"Windows 7".equals(System.getProperty("os.name"))) { return; } Java2DStringUtilities.drawString(graphics, CENTER, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.1", image); }
@Test public void drawCenteredText2() throws Exception { graphics.setFont(FontUtil.getLiberationSansRegular().deriveFont(Font.PLAIN, 10)); Java2DStringUtilities.drawString(graphics, CENTER, x, y, "0"); ImageAssert.compareImages("textUtilities.2", image); }
@Test public void drawRightText1() throws Exception { if (!"Windows 7".equals(System.getProperty("os.name"))) { return; } Java2DStringUtilities.drawString(graphics, RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.3", image); }
@Test public void drawRightText2() throws Exception { graphics.setFont(FontUtil.getLiberationSansRegular().deriveFont(Font.PLAIN, 10)); Java2DStringUtilities.drawString(graphics, RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.4", image); }
@Test public void drawBottomRightText1() throws Exception { if (!"Windows 7".equals(System.getProperty("os.name"))) { return; } Java2DStringUtilities.drawString(graphics, BOTTOM_RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.5", image); }
@Test public void drawBottomRightText2() throws Exception { graphics.setFont(FontUtil.getLiberationSansRegular().deriveFont(Font.PLAIN, 10)); Java2DStringUtilities.drawString(graphics, BOTTOM_RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.6", image); }
@Test public void drawTopRightText1() throws Exception { if (!"Windows 7".equals(System.getProperty("os.name"))) { return; } Java2DStringUtilities.drawString(graphics, TOP_RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.7", image); }
@Test public void drawTopRightText2() throws Exception { graphics.setFont(FontUtil.getLiberationSansRegular().deriveFont(Font.PLAIN, 10)); Java2DStringUtilities.drawString(graphics, TOP_RIGHT, x, y, "ABCD"); ImageAssert.compareImages("textUtilities.8", image); }