Advertisement
Guest User

Untitled

a guest
May 27th, 2012
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.57 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3. import java.util.Random;
  4. import javax.imageio.ImageIO;
  5.  
  6. public class Bitmap {
  7.  
  8.     public static Bitmap sprites, font;
  9.     public static String characters = " !\"#$%&'()*+,-./0123456789:;<=>?"
  10.             + "@ABCDEFGHIJKLMNOPQRSTUVWXYZ(\\)^_";
  11.     public int pixels[], width, height;
  12.  
  13.     public Bitmap(String name) {
  14.         BufferedImage img = null;
  15.  
  16.         try {
  17.             img = ImageIO.read(Bitmap.class.getResourceAsStream("/res/" + name));
  18.         } catch (IOException ex) {
  19.             ex.printStackTrace();
  20.             return;
  21.         }
  22.  
  23.         width = img.getWidth();
  24.         height = img.getHeight();
  25.         pixels = img.getRGB(0, 0, width, height, pixels, 0, width);
  26.     }
  27.  
  28.     public Bitmap(int width, int height) {
  29.         this.width = width;
  30.         this.height = height;
  31.         pixels = new int[width * height];
  32.     }
  33.  
  34.     public static void init() {
  35.         font = new Bitmap("font.png");
  36.         sprites = new Bitmap("sprites.png");
  37.     }
  38.  
  39.     public void clear() {
  40.         for (int i = 0; i < pixels.length; i++) {
  41.             pixels[i] = 0x262626;
  42.         }
  43.     }
  44.  
  45.     public void random() {
  46.         for (int i = 0; i < pixels.length; i++) {
  47.             pixels[i] = new Random().nextInt(0xFF) * 0x010101;
  48.         }
  49.     }
  50.  
  51.     public Bitmap getSubImage(int x, int y, int width, int height) {
  52.         Bitmap returned = new Bitmap(width, height);
  53.  
  54.         for (int ix = 0; ix < width; ix++) {
  55.             for (int iy = 0; iy < height; iy++) {
  56.                 try {
  57.                     returned.pixels[ix + iy * width] = pixels[x + (ix + iy * width) % width + (y + (ix + iy * width) / width) * this.width];
  58.                 } catch (Exception e) {
  59.                     continue;
  60.                 }
  61.             }
  62.         }
  63.  
  64.         return returned;
  65.     }
  66.  
  67.     public Bitmap getSubImage(int x, int y, int width, int height, int color) {
  68.         Bitmap returned = new Bitmap(width, height);
  69.  
  70.         for (int ix = 0; ix < width; ix++) {
  71.             for (int iy = 0; iy < height; iy++) {
  72.                 try {
  73.                     returned.pixels[ix + iy * width] = pixels[x + (ix + iy * width) % width + (y + (ix + iy * width) / width) * this.width] & color;
  74.                 } catch (Exception e) {
  75.                     continue;
  76.                 }
  77.             }
  78.         }
  79.  
  80.         return returned;
  81.     }
  82.  
  83.     public void setSubImage(Bitmap bitmap, int x, int y) {
  84.         for (int i = 0; i < bitmap.pixels.length; i++) {
  85.             try {
  86.                 if (x + i % bitmap.width >= 0 && x + i % bitmap.width < width) {
  87.                     pixels[x + i % bitmap.width + (y + i / bitmap.width) * width] = bitmap.pixels[i] >> 24 == 0 ? pixels[x + i % bitmap.width + (y + i / bitmap.width) * width] : bitmap.pixels[i];
  88.                 }
  89.             } catch (Exception e) {
  90.                 continue;
  91.             }
  92.         }
  93.     }
  94.  
  95.     public void setSubImage(Bitmap bitmap, int x, int y, int color) {
  96.         for (int i = 0; i < bitmap.pixels.length; i++) {
  97.             try {
  98.                 if (x + i % bitmap.width >= 0 && x + i % bitmap.width < width) {
  99.                     pixels[x + i % bitmap.width + (y + i / bitmap.width) * width] = bitmap.pixels[i] >> 24 == 0 ? pixels[x + i % bitmap.width + (y + i / bitmap.width) * width] : bitmap.pixels[i] & color;
  100.                 }
  101.             } catch (Exception e) {
  102.                 continue;
  103.             }
  104.         }
  105.     }
  106.  
  107.     public Bitmap getFlippedCopy(boolean horizontal) {
  108.         Bitmap returned = new Bitmap(width, height);
  109.  
  110.         if (horizontal) {
  111.             for (int ix = 0; ix < width; ix++) {
  112.                 for (int iy = 0; iy < height; iy++) {
  113.                     try {
  114.                         returned.pixels[ix + iy * width] = pixels[(width - ix - 1) + iy * width];
  115.                     } catch (Exception e) {
  116.                         continue;
  117.                     }
  118.                 }
  119.             }
  120.  
  121.         } else {
  122.             System.arraycopy(pixels, 0, returned.pixels, 0, width * height);
  123.         }
  124.  
  125.         return returned;
  126.     }
  127.  
  128.     public void drawString(String string, int x, int y) {
  129.         for (int i = 0; i < string.length(); i++) {
  130.             int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
  131.             setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, 0xE6E4D5);
  132.         }
  133.     }
  134.  
  135.     public void drawString(String string, int x, int y, int color) {
  136.         for (int i = 0; i < string.length(); i++) {
  137.             int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
  138.             setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, color);
  139.         }
  140.     }
  141.  
  142.     public void drawCenteredString(String string, int y) {
  143.         int x = (Main.IMG_WIDTH - string.length() * 4) / 2;
  144.         for (int i = 0; i < string.length(); i++) {
  145.             int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
  146.             setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, 0xE6E4D5);
  147.         }
  148.     }
  149.  
  150.     public void drawCenteredString(String string, int y, int color) {
  151.         int x = (Main.IMG_WIDTH - string.length() * 4) / 2;
  152.         for (int i = 0; i < string.length(); i++) {
  153.             int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
  154.             setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, color);
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement