|
|
@ -0,0 +1,64 @@ |
|
|
|
package Snake; |
|
|
|
|
|
|
|
import java.awt.*; |
|
|
|
|
|
|
|
public class TextView implements Drawable |
|
|
|
{ |
|
|
|
public enum AnchorType { Left, Center, Right }; |
|
|
|
|
|
|
|
private String text; |
|
|
|
|
|
|
|
private Point position; |
|
|
|
|
|
|
|
private boolean visible; |
|
|
|
|
|
|
|
private AnchorType anchor; |
|
|
|
|
|
|
|
public TextView(String text, Point position) |
|
|
|
{ |
|
|
|
this(text, position, true); |
|
|
|
} |
|
|
|
|
|
|
|
public TextView(String text, Point position, boolean visible) |
|
|
|
{ |
|
|
|
this.text = text; |
|
|
|
this.position = position; |
|
|
|
this.visible = visible; |
|
|
|
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() / 2; |
|
|
|
|
|
|
|
if (anchor == AnchorType.Center) |
|
|
|
x -= fontMetrics.stringWidth(text) / 2; |
|
|
|
else if (anchor == AnchorType.Right) |
|
|
|
x -= fontMetrics.stringWidth(text); |
|
|
|
|
|
|
|
g.setColor(Color.WHITE); |
|
|
|
g.drawString(text, x, y); |
|
|
|
} |
|
|
|
} |
|
|
|
} |