From 30ee3005f7c14a787b720e8a87216fbd405d61ba Mon Sep 17 00:00:00 2001 From: Justin Senn Date: Mon, 6 Feb 2023 18:17:33 +0100 Subject: [PATCH] update --- src/main/java/Snake/TextView.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/main/java/Snake/TextView.java b/src/main/java/Snake/TextView.java index 29746ac..4fd6078 100644 --- a/src/main/java/Snake/TextView.java +++ b/src/main/java/Snake/TextView.java @@ -27,11 +27,38 @@ public class TextView implements Drawable this.anchor = AnchorType.Center; } + public void setText(String newText) + { + text = newText; + } + + public void setVisibility(boolean visibility) + { + visible = visibility; + } + + public void setAnchor(AnchorType newAnchor) + { + anchor = newAnchor; + } @Override public void draw(Graphics g) { + if(visible) + { + FontMetrics fontMetrics = g.getFontMetrics(); + int x = position.x; + int y = position.y + fontMetrics.getHeight(); + + if (anchor == AnchorType.Center) + x -= fontMetrics.stringWidth(text); + else if (anchor == AnchorType.Right) + x -= fontMetrics.stringWidth(text); + g.setColor(Color.WHITE); + g.drawString(text, x, y); + } } }