klasscho

Tile class

May 26th, 2020
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package com.packag;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.BufferedImage;
  5.  
  6. public class Tile {
  7.     public static final int WIDTH = 80;
  8.     public static final int HEIGHT = 80;
  9.     public static final int SLIDE_SPEED = 20;
  10.     public static final int ARC_WIDTH = 15;
  11.     public static final int ARC_HEIGHT = 15;
  12.  
  13.     private int value;
  14.     private BufferedImage tilelImage;
  15.     private Color background;
  16.     private Color text;
  17.     private Font font;
  18.     private int x;
  19.     private int y;
  20.  
  21.     public Tile(int value, int x, int y){
  22.         this.value = value;
  23.         this.x = x;
  24.         this.y = y;
  25.         tilelImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
  26.         drawImage();
  27.     }
  28.  
  29.     private void drawImage(){
  30.         Graphics2D g = (Graphics2D) tilelImage.getGraphics();
  31.         if (value == 2){
  32.             background = new Color(0xe9e9e9);
  33.             text = new Color(0x000000);
  34.         }
  35.         else if (value == 4){
  36.             background = new Color(0xe6daab);
  37.             text = new Color(0x000000);
  38.         }
  39.         else if (value == 8){
  40.             background = new Color(0xf79d3d );
  41.             text = new Color(0xffffff);
  42.         }
  43.         else if (value == 16){
  44.             background = new Color(0xF28007);
  45.             text = new Color(0xffffff);
  46.         }
  47.         else if (value == 32){
  48.             background = new Color(0xf55e3b);
  49.             text = new Color(0xffffff);
  50.         }
  51.         else if (value == 64){
  52.             background = new Color(0xff0000);
  53.             text = new Color(0xffffff);
  54.         }
  55.         else if (value == 128){
  56.             background = new Color(0xe9de84);
  57.             text = new Color(0xffff);
  58.         }
  59.         else if (value == 256){
  60.             background = new Color(0xf6e873);
  61.             text = new Color(0xffffff);
  62.         }
  63.         else if (value == 512){
  64.             background = new Color(0xf5e455);
  65.             text = new Color(0xffffff);
  66.         }
  67.         else if (value == 1024){
  68.             background = new Color(0xf7e12c);
  69.             text = new Color(0xffffff);
  70.         }
  71.         else if (value == 2048){
  72.             background = new Color(0xffe400);
  73.             text = new Color(0xffffff);
  74.         }
  75.         else {
  76.              background = Color.black;
  77.              text = Color.white;
  78.         }
  79.  
  80.         g.setColor(new Color(0, 0, 0, 0));
  81.         g.fillRect(0, 0, WIDTH, HEIGHT);
  82.  
  83.         g.setColor(background);
  84.         g.fillRoundRect(0,0, WIDTH, HEIGHT, ARC_WIDTH, ARC_HEIGHT);
  85.  
  86.         g.setColor(text);
  87.  
  88.         if(value <= 64){
  89.             font = Game.main.deriveFont(36f);
  90.         }
  91.         else {
  92.             font = Game.main;
  93.         }
  94.         g.setFont(font);
  95.  
  96.         int drawX = WIDTH/2 - DrawUtils.getMessageWidth("" + value, font, g)/2;
  97.         int drawY = HEIGHT/2 + DrawUtils.getMessageHeight("" + value, font, g)/2;
  98.         g.drawString("" + value, drawX, drawY);
  99.         g.dispose();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment