java - 在java中用单独的矩形渲染等轴测图

标签 java arrays dictionary isometric

过去一个小时左右我一直在研究,但我似乎无法渲染等轴测图。我想实现类似 this 的目标。 但我得到this... 。我将我的 map 存储为一维数组中的图 block ,如下所示:

private final int width, height;
    private final int tileWidth, length;
    private int[] tiles;

    public Level(int width, int height) {
        this.width = width;
        this.height = height;
        tiles = new int[width * height];
        tileWidth = 68;
        length = 48;
    }

我传递 10, 10 作为宽度和高度的参数。我像这样渲染 map :

public void render(Graphics g) {
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                g.setColor(Color.red);
                if (x % 2 == 0)
                    g.drawRect(x * tileWidth, y * length / 2, tileWidth, length);
                else
                    g.fillRect((x * tileWidth) + (tileWidth / 2), (y * length / 2), width, length);
            }
        }
    }

任何帮助都会非常感激,我一直想学习制作等距游戏,但已经被平面 2D 困住了一段时间。

最佳答案

对于仅瓷砖,您可以使用 shear transform :

Graphics2D g2d = (Graphics2D) g;
AffineTransform at = AffineTransform.getShearInstance(1, 0);
g2d.transform(at);
// rest of your drawing code here

您可能还想设置剪切 anchor :

double sa_x = 100, sa_y = 100; // or whatever
AffineTransform at = new AffineTransform();

// S3: Move back to original origin
at.translate(sa_x, sa_y);

// S2: Shear
at.shear(1, 0);

// S1: Set origin
at.translate(-sa_x, -sa_y);

您可以改变剪切因子 1 以获得不同的剪切量。

关于java - 在java中用单独的矩形渲染等轴测图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16868591/

相关文章:

java - 如何从 Java 中的序列化对象加载选择性数据?

java.lang.StringIndexOutOfBoundsException : String index out of range in yuicompressor

java - 我如何比较java中没有格式和数据差异标题的csv

python - 在python中将列表转换为字典

c# - 在 C# 中从字典中过滤键值

algorithm - 从子树中查找所需的最少字符

python - 将文本中的键值对解析为字典

java - Derby "A truncation error was encountered trying to shrink CLOB ' <流值 >' to length 255"

java - 在 Maven 插件中调用 MavenCli 失败

Python 列表到元组