mrkirby153

Untitled

May 8th, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package me.mrkirby153.quarxelnetwork.core.api.text;
  2.  
  3. import me.mrkirby153.quarxelnetwork.core.api.Cuboid;
  4. import me.mrkirby153.quarxelnetwork.core.api.Direction;
  5. import org.bukkit.Location;
  6. import org.bukkit.Material;
  7. import org.bukkit.World;
  8. import org.bukkit.block.Block;
  9.  
  10. import java.awt.*;
  11. import java.awt.font.FontRenderContext;
  12. import java.awt.geom.Rectangle2D;
  13. import java.awt.image.BufferedImage;
  14.  
  15. public class BlockText {
  16.     private final String text;
  17.     private final Font font;
  18.     private Cuboid cuboid;
  19.  
  20.     public BlockText(String text, Font font) {
  21.         this.text = text;
  22.         this.font = font;
  23.     }
  24.  
  25.  
  26.     public String getText() {
  27.         return this.text;
  28.     }
  29.  
  30.     public Font getFont() {
  31.         return this.font;
  32.     }
  33.  
  34.  
  35.     public void generate(Direction generateDirection, Location location, Material material) {
  36.         BufferedImage image = getImage();
  37.         int color = 0;
  38.         int startX = location.getBlockX();
  39.         int startY = location.getBlockY();
  40.         int startZ = location.getBlockZ();
  41.         World w = location.getWorld();
  42.         for (int y = 0; y < image.getHeight(); y++) {
  43.             for (int x = 0; x < image.getWidth(); x++) {
  44.                 color = image.getRGB(x, y);
  45.                 if (Color.black.getRGB() != color)
  46.                     continue;
  47.                 // Actually draw the string
  48.                 Block b = null;
  49.                 switch (generateDirection) {
  50.                     case NORTH:
  51.                         b = w.getBlockAt(startX+x, startY+y, startZ);
  52.                 }
  53.                 if(b == null)
  54.                     continue;
  55.                 b.setType(material, false);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private BufferedImage getImage() {
  61.         BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
  62.         Graphics g = img.getGraphics();
  63.         g.setFont(this.font);
  64.  
  65.         FontRenderContext frc = g.getFontMetrics().getFontRenderContext();
  66.         Rectangle2D rect = this.font.getStringBounds(this.text, frc);
  67.         g.dispose();
  68.  
  69.         img = new BufferedImage((int) Math.ceil(rect.getWidth()), (int) Math.ceil(rect.getHeight()), BufferedImage.TYPE_4BYTE_ABGR);
  70.         g = img.getGraphics();
  71.         g.setColor(Color.black);
  72.         g.setFont(this.font);
  73.  
  74.         FontMetrics fm = g.getFontMetrics();
  75.         int x = 0;
  76.         int y = fm.getAscent();
  77.         g.drawString(this.text, x, y);
  78.         g.dispose();
  79.         return img;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment