简体   繁体   English

如何用Java制作图像?

[英]How to make a Image in Java?

I am working on a Survial game in java,and am working on map gen. 我正在用Java开发Survial游戏,并且正在研究地图生成。 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. 我有一个所有块的ArrayList,并希望这样做,如果该块在地图上位于0,0的情况下(例如草),我可以将此图像上位于0,0的像素设置为绿色,依此类推对于其他块。

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. 基本上,您可以在内存中构建缓冲的图像,然后写入文件或将该图像放入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/ ). 如果您想使用一些漂亮的东西,请尝试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. 我目前正在与来自大学的其他人一起使用更复杂的纸板,我们决定这样做,并反对使用Java绘图API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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