/** * Write the given text string in the current font, left-aligned at (x, y). * * @param x the x-coordinate of the text * @param y the y-coordinate of the text * @param s the text */ public static void textLeft(double x, double y, String s) { offscreen.setFont(font); FontMetrics metrics = offscreen.getFontMetrics(); double xs = scaleX(x); double ys = scaleY(y); int hs = metrics.getDescent(); offscreen.drawString(s, (float) (xs), (float) (ys + hs)); draw(); }
@Override public void draw(Graphics2D g) { g.setColor(Color.lightGray); g.fillRect(0, 0, game.getScreenSize().width, game.getScreenSize().height); drawSnake(g); drawApple(g); g.setFont(new Font("Default", Font.BOLD, 12)); String message = "Press <F5> to save, <F6> to load."; Rectangle2D messageBounds = g.getFontMetrics() .getStringBounds( message, g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ �������� // ������ � �������� int messageWidth = (int) (messageBounds.getWidth()); int messageHeight = (int) (messageBounds.getHeight()); g.drawString(message, 15, 10); if (paused) { g.setFont(new Font("Default", Font.BOLD, 60)); g.setColor(Color.white); message = "Pause"; messageBounds = g.getFontMetrics() .getStringBounds( message, g); // g.getFontMetrics().getStringBounds - ��������� �������� ������ �������� // ������ � �������� messageWidth = (int) (messageBounds.getWidth()); messageHeight = (int) (messageBounds.getHeight()); g.drawString( message, game.getScreenSize().width / 2 - messageWidth / 2, game.getScreenSize().height / 2 - messageHeight / 2); } }
public void draw( Graphics2D g, Color stringColor, Color foreground, Color background, String info, double x, double y) { FontMetrics fm = g.getFontMetrics(); int h = fm.getHeight(); int w = fm.stringWidth(info); r1.setRect( x - w / 2 - in.left, y - in.top - h / 2, w + in.right + in.left, h + in.bottom + in.top); g.setColor(background); g.fill(r1); g.setColor(stringColor); g.draw(r1); g.setColor(foreground); r2.setRect(r1.getX() + 1, r1.getY() + 1, r1.getWidth() - 2, r1.getHeight() - 2); g.draw(r2); g.setColor(stringColor); g.drawString( info, (float) (r2.getX() + in.left), (float) (r2.getY() + h - (r2.getHeight() - h) / 2)); }