简体   繁体   中英

How to make a Image in Java?

I am working on a Survial game in java,and am working on map gen. Im looking for a way to Make a Image of the maps. I have a ArrayList of all the blocks,and would like to make it so if the block at 0,0 on the map if for example grass,I can set the pixel at 0,0 on this image to be green,and so on for the other blocks.

EDIT ---- as people are saying its unclear what Im trying to ask,this is the sort of image im talking about:

在此处输入图片说明

Where the dark green is the grass,the light green is trees,yellow is sand and blue is water.

private static void write(int id) {
        try {
            BufferedImage bi = ImageIO.read(new URL("http://xyz.com/abc.png"));
            BufferedImage bi2 = resizeImage(bi, bi.getType());
            Graphics g = bi2.getGraphics();
            g.drawString("Hello", 20, 20);
            ImageIO.write(bi2, "png", new File("out"+id+".png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static BufferedImage resizeImage(BufferedImage originalImage, int type){
        BufferedImage resizedImage = new BufferedImage(400, 400, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 50, 50, 248, 248, null);
        g.dispose();
        return resizedImage;
    }

Above is an example snapshot from something i had used. Its just a prototype so needs refining. Basically you can build a buffered image in memory and write to file or put that image in another component such as jlabel.

Once you have graphics reference you can pretty much use it as Canvas.

If you want to use something nifty, try libgdx( http://code.google.com/p/libgdx/ ). It has an good support for creating nice things and also supports some popular map editor. I'm currently working on a more complex card board came with some other poeple from university and we decided for it and against the java drawing api.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM