本文介绍了什么是瓷砖和他们如何在创建的BufferedImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我张贴的Sun Java论坛中提出问题前一段时间,我发现很难理解的,虽然看起来他给了我正确的方法来我的问题回复人收到我的第一反应。这个问题的链接是:

I posted a question in sun java forums sometime ago and i am finding it hard to understand the first response i received from the replier though it seems he gave me the correct approach to my problem. The link to the question is:

有人回答说,我应该使用的BufferedImage ,使瓷砖。我真的不明白什么是瓷砖与连接意味着的BufferedImage

Someone replied that i should use BufferedImage and make tiles. I don't really understand what the tiles mean in connection with the BufferedImage.

我想有人向我解释瓷砖是什么,以及它们是如何创建的的BufferedImage

I would like someone to explain to me what the tiles are and how they are created in the BufferedImage.

我已经寻找了一会儿网,但无法找到任何一个环节,可以帮助我了解瓷砖的基本知识和创造的瓷砖。任何指针指向一个网站也是AP preciated。

I have searched the web for a while but couldn't find any link that can help me understanding the basics of the tiles and creating the tiles. Any pointer to a site is also appreciated.

我需要帮助与的BufferedImage 也是它们是如何创建的。

I need help in understanding the tiles in connection with the BufferedImage and also how they are created.

推荐答案

在2D游戏A砖只是指的的形象不是整个屏幕,您可以重复使用多次创造背景较小的

A "tile" in a 2D game simply means an "image smaller than whole screen that you can reuse several times to create the background".

下面是一个在其中创建四个区域(加入一些随机噪声每个像素)工作的例子。每瓦是50x50像素。

Here's a a working example where four tiles are created (adding some random noise to every pixel). Each tile is 50x50 pixels.

然后有一个地图(你在你的情况称之为网格)再presenting要放在哪里而瓷砖。

Then there's a "map" (that you call a "grid" in your case) representing which tiles you want to put where.

从这个图,一个更大的的BufferedImage 创建(请注意,这只是一个例子,在一个真正的程序,你需要使用一个BufferedImage副本,而不是一个像素逐像素复印件)。

From that map, a bigger BufferedImage is created (note that it's just an example, in a real program you'll want to use a BufferedImage copy, not a pixel-by-pixel copy).

地图是9×7,每瓦是50x50像素,由此产生的图像是9×50×7 * 50(即350 450)。

The map is 9x7, each tile is 50x50 pixels, hence the resulting image is 9*50 x 7*50 (ie 450 by 350).

请注意,以下仅仅是一个简单的例子,越短越好,说明了如何使用几个瓷砖创造更大的BufferedImage:我们的目标是的的给予最佳的Swing的使用,也不是一个教程如何表演的每一位挤出BufferedImages的,等等。

Note that the following is really just a simple example, as short as possible, showing how to create a bigger BufferedImage using several tiles: the goal is not to give a tutorial on best Swing usage nor on how to squeeze every bit of performances out of BufferedImages, etc.

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ToyTiled extends JFrame {

    private static final int IMAGE_TYPE = BufferedImage.TYPE_INT_ARGB;

    private BufferedImage img;

    public static void main( String[] args ) {
        new ToyTiled();
    }

    public ToyTiled() {
        super();
        this.add(new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                g.drawImage(img, 0, 0, null);
            }

        });
        img = new BufferedImage( 450, 350, IMAGE_TYPE );   // here you should create a compatible BufferedImage
        this.setSize(img.getWidth(), img.getHeight());
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);


        final int NB_TILES = 4;
        BufferedImage[] tiles = new BufferedImage[NB_TILES];
        tiles[0] = createOneTile( new Color( 255, 255, 255 ) );
        tiles[1] = createOneTile( new Color( 255,   0, 255 ) );
        tiles[2] = createOneTile( new Color(   0,   0, 255 ) );
        tiles[3] = createOneTile( new Color(   0, 255, 255 ) );  

        final int[][] map = new int[][] {
                { 1, 0, 2, 3, 0, 1, 2, 2, 2 },
                { 0, 2, 3, 0, 1, 2, 2, 2, 3 },
                { 1, 0, 2, 3, 0, 1, 2, 2, 2 },
                { 2, 1, 0, 1, 2, 3, 2, 0, 0 },
                { 1, 0, 2, 3, 0, 1, 2, 2, 3 },
                { 1, 0, 2, 2, 1, 1, 2, 2, 3 },
                { 1, 0, 2, 3, 0, 1, 2, 2, 3 },
        };

        for (int i = 0; i < map[0].length; i++) {
            for (int j = 0; j < map.length; j++) {
                final BufferedImage tile = tiles[map[j][i]];
                for (int x = 0; x < tile.getWidth(); x++) {
                    for (int y = 0; y < tile.getHeight(); y++) {
                        img.setRGB( x + i * 50, y + j * 50, tile.getRGB(x,y) );
                    }
                }
            }
        }

        this.setVisible( true );
    }

    private BufferedImage createOneTile( final Color c ) {
        final Random r = new Random();
        final BufferedImage res = new BufferedImage( 50, 50, IMAGE_TYPE );
        for (int x = 0; x < res.getWidth(); x++) {
            for (int y = 0; y < res.getHeight(); y++) {
                res.setRGB( x, y, c.getRGB() - r.nextInt(150) );
            }
        }
        return res;
    }

}

这篇关于什么是瓷砖和他们如何在创建的BufferedImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 09:50