Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- public class Bitmap {
- public static Bitmap sprites, font;
- public static String characters = " !\"#$%&'()*+,-./0123456789:;<=>?"
- + "@ABCDEFGHIJKLMNOPQRSTUVWXYZ(\\)^_";
- public int pixels[], width, height;
- public Bitmap(String name) {
- BufferedImage img = null;
- try {
- img = ImageIO.read(Bitmap.class.getResourceAsStream("/res/" + name));
- } catch (IOException ex) {
- ex.printStackTrace();
- return;
- }
- width = img.getWidth();
- height = img.getHeight();
- pixels = img.getRGB(0, 0, width, height, pixels, 0, width);
- }
- public Bitmap(int width, int height) {
- this.width = width;
- this.height = height;
- pixels = new int[width * height];
- }
- public static void init() {
- font = new Bitmap("font.png");
- sprites = new Bitmap("sprites.png");
- }
- public void clear() {
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = 0x262626;
- }
- }
- public void random() {
- for (int i = 0; i < pixels.length; i++) {
- pixels[i] = new Random().nextInt(0xFF) * 0x010101;
- }
- }
- public Bitmap getSubImage(int x, int y, int width, int height) {
- Bitmap returned = new Bitmap(width, height);
- for (int ix = 0; ix < width; ix++) {
- for (int iy = 0; iy < height; iy++) {
- try {
- returned.pixels[ix + iy * width] = pixels[x + (ix + iy * width) % width + (y + (ix + iy * width) / width) * this.width];
- } catch (Exception e) {
- continue;
- }
- }
- }
- return returned;
- }
- public Bitmap getSubImage(int x, int y, int width, int height, int color) {
- Bitmap returned = new Bitmap(width, height);
- for (int ix = 0; ix < width; ix++) {
- for (int iy = 0; iy < height; iy++) {
- try {
- returned.pixels[ix + iy * width] = pixels[x + (ix + iy * width) % width + (y + (ix + iy * width) / width) * this.width] & color;
- } catch (Exception e) {
- continue;
- }
- }
- }
- return returned;
- }
- public void setSubImage(Bitmap bitmap, int x, int y) {
- for (int i = 0; i < bitmap.pixels.length; i++) {
- try {
- if (x + i % bitmap.width >= 0 && x + i % bitmap.width < width) {
- 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];
- }
- } catch (Exception e) {
- continue;
- }
- }
- }
- public void setSubImage(Bitmap bitmap, int x, int y, int color) {
- for (int i = 0; i < bitmap.pixels.length; i++) {
- try {
- if (x + i % bitmap.width >= 0 && x + i % bitmap.width < width) {
- 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;
- }
- } catch (Exception e) {
- continue;
- }
- }
- }
- public Bitmap getFlippedCopy(boolean horizontal) {
- Bitmap returned = new Bitmap(width, height);
- if (horizontal) {
- for (int ix = 0; ix < width; ix++) {
- for (int iy = 0; iy < height; iy++) {
- try {
- returned.pixels[ix + iy * width] = pixels[(width - ix - 1) + iy * width];
- } catch (Exception e) {
- continue;
- }
- }
- }
- } else {
- System.arraycopy(pixels, 0, returned.pixels, 0, width * height);
- }
- return returned;
- }
- public void drawString(String string, int x, int y) {
- for (int i = 0; i < string.length(); i++) {
- int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
- setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, 0xE6E4D5);
- }
- }
- public void drawString(String string, int x, int y, int color) {
- for (int i = 0; i < string.length(); i++) {
- int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
- setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, color);
- }
- }
- public void drawCenteredString(String string, int y) {
- int x = (Main.IMG_WIDTH - string.length() * 4) / 2;
- for (int i = 0; i < string.length(); i++) {
- int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
- setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, 0xE6E4D5);
- }
- }
- public void drawCenteredString(String string, int y, int color) {
- int x = (Main.IMG_WIDTH - string.length() * 4) / 2;
- for (int i = 0; i < string.length(); i++) {
- int character = characters.indexOf(Character.toUpperCase(string.charAt(i)));
- setSubImage(font.getSubImage((character % 32) * 4, (character / 32) * 6, 4, 6), x + i * 4, y, color);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement