Advertisement
zedjasper

Untitled

Feb 5th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. private Texture texture;
  2. boolean maintainAspectRatio = true;
  3.  
  4. public Rectangle(){}
  5.  
  6. public Rectangle(float width, float height, Color color) {
  7. this(0, 0, width, height, color, true);
  8. }
  9.  
  10. public Rectangle(float x, float y, float width, float height, Color color) {
  11. this(x, y, width, height, color, true);
  12. }
  13.  
  14. public Rectangle(float x, float y, float width, float height, Color color, boolean aspectRatio) {
  15. maintainAspectRatio = aspectRatio;
  16.  
  17. createTexture((int)width, (int)height, color);
  18.  
  19. setWidth(width);
  20. setHeight(height);
  21. setOrigin(Align.center);
  22. setPosition(x, y);
  23. }
  24.  
  25. private void createTexture(int width, int height, Color color) {
  26. Pixmap pixmap = new Pixmap(width, height, Pixmap.Format.RGBA8888);
  27. pixmap.setColor(color);
  28. pixmap.fillRectangle(0, 0, width, height);
  29. texture = new Texture(pixmap);
  30. pixmap.dispose();
  31. }
  32.  
  33. public void setAlpha(float alpha){
  34. this.getColor().a = alpha;
  35. }
  36.  
  37. @Override
  38. public void draw(Batch batch, float parentAlpha) {
  39. Color color = getColor();
  40. batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
  41. batch.draw(texture, getX(), getY(), getWidth(), getHeight());
  42. }
  43.  
  44. @Override
  45. public void setPosition(float x, float y) {
  46. x -= getWidth()/2;
  47. y -= getHeight()/2;
  48.  
  49. super.setPosition(x, y);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement