You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.9 KiB

2 years ago
  1. package gameobjects;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.font.FontRenderContext;
  6. import java.awt.font.TextAttribute;
  7. import java.text.AttributedString;
  8. import java.util.LinkedList;
  9. import collider.*;
  10. import controller.ObjectController;
  11. import playground.Playground;
  12. import rendering.*;
  13. /**
  14. * Convenience Class subclassing {@link GameObject}, directly instanciating {@link TextArtist} a
  15. * subclass of {@link Artist} that draws a text. The controller is left undefined, the collider as
  16. * well. However, a single call to the overwritten method {@link #generateColliders} will in fact
  17. * generate a {@link RectCollider} of just the right size for the text.
  18. *
  19. */
  20. public class TextObject extends GameObject {
  21. private String text = null;
  22. protected double rx, ry;
  23. public String getText() {
  24. return this.text;
  25. }
  26. /**
  27. * Constructor.
  28. *
  29. * @param id object name
  30. * @param playground containing {@link Playground} instance
  31. * @param x positionx
  32. * @param y positiony
  33. * @param vx speedx
  34. * @param vy speedy
  35. * @param size font size in Pixel
  36. * @param text String to be displayed
  37. * @param textColor text color, see java.awt.Color
  38. */
  39. public TextObject(String id, Playground playground, double x, double y, double vx, double vy,
  40. String text, int size, Color textColor) {
  41. super(id, playground, x, y, vx, vy);
  42. this.artist = new TextArtist(this, text, size, textColor);
  43. this.setColliders(new LinkedList<Collider>());
  44. }
  45. public void setText(String s) {
  46. this.text = s;
  47. ((TextArtist) this.artist).setText(s);
  48. }
  49. public TextObject generateColliders() {
  50. // we need to Cast to TextArtist as we want to access Width and Height of text
  51. TextArtist kruecke = (TextArtist) (this.artist);
  52. this.scol.clear();
  53. this.scol.add(new RectCollider("rect", this, kruecke.getTextWidth(), kruecke.getTextHeight()));
  54. return this;
  55. }
  56. }