过去一个小时左右我一直在研究,但我似乎无法渲染等轴测图。我想实现类似 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/