create Image From Ascii Map - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

create Image From Ascii Map

Demo Code

/*// w w w .  j  a  v a2s .c o  m
    JPC: An x86 PC Hardware Emulator for a pure Java Virtual Machine
    Release Version 2.4

    A project from the Physics Dept, The University of Oxford

    Copyright (C) 2007-2010 The University of Oxford

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 2 as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
         
    Details (including contact information) can be found at: 

    jpc.sourceforge.net
    or the developer website
    sourceforge.net/projects/jpc/

    Conceived and Developed by:
    Rhys Newman, Ian Preston, Chris Dennis

    End of licence header
 */
//package com.java2s;
import java.awt.*;
import java.awt.geom.AffineTransform;

import java.awt.image.*;

public class Main {
    private static int BUFFER_SIZE = 200;
    private static BufferedImage buffer = new BufferedImage(BUFFER_SIZE,
            BUFFER_SIZE, BufferedImage.TYPE_INT_ARGB);
    private static Graphics2D bufferGraphics = (Graphics2D) buffer
            .getGraphics();

    public static BufferedImage createImageFromAsciiMap(String map,
            int width, int height) {
        return createImageFromAsciiMap(map, width, height, Color.black,
                Color.white);
    }

    public synchronized static BufferedImage createImageFromAsciiMap(
            String map, int width, int height, Color fg, Color bg) {
        if ((map == null) || (map.length() == 0))
            return null;
        double theta = 0;

        int w = BUFFER_SIZE;
        int h = BUFFER_SIZE;
        bufferGraphics.setColor(bg);
        bufferGraphics.fillRect(0, 0, w, h);

        int dx = 15;
        int dy = 25;
        int xpos = 20;
        int ypos = 20;
        bufferGraphics.setColor(fg);
        for (int i = 0; i < map.length(); i++) {
            char ch = map.charAt(i);
            if (ch == '\r')
                theta = Math.PI / 2;
            else if (ch == '\n') {
                ypos += dy;
                xpos = 20;
            } else {
                bufferGraphics.drawString("" + ch, xpos, ypos);
                xpos += dx;
            }
        }

        int xmin = w;
        int ymin = h;
        int xmax = 0;
        int ymax = 0;
        boolean isBlank = true;

        int rbg = bg.getRGB();
        for (int i = 0; i < w; i++)
            for (int j = 0; j < h; j++) {
                if (rbg == buffer.getRGB(i, j))
                    continue;

                isBlank = false;
                xmin = Math.min(xmin, i);
                xmax = Math.max(xmax, i);
                ymin = Math.min(ymin, j);
                ymax = Math.max(ymax, j);
            }

        if (isBlank)
            return null;

        xmin = Math.max(0, xmin - 1);
        ymin = Math.max(0, ymin - 1);
        xmax = Math.min(w, xmax + 1);
        ymax = Math.min(h, ymax + 1);

        BufferedImage buf2 = buffer.getSubimage(xmin, ymin,
                xmax - xmin + 1, ymax - ymin + 1);
        BufferedImage result = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) result.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setTransform(AffineTransform.getRotateInstance(theta, width / 2,
                height / 2));
        g2.drawImage(buf2, 0, 0, width, height, null);
        g2.dispose();
        return result;
    }
}

Related Tutorials