Java – 使用Graphics2D矩形在面板中创建2D平铺贴图?

我正试图在一个非常基本的程序中模拟一场战斗,但由于这是我第一次使用Java编写一个大程序,所以我对如何继续进行操作几乎一无所知。 我以为我会有一个600×600的大面板并使用Graphics2D来绘制20×20矩形的地形……不幸的是,即使有几个教程,我也不知道该怎么做。

我有10种不同类型的地形可以循环,还有5种不同的景观轮廓。 基本上我想让程序做的是当我在combobox中选择一个特定的轮廓时,它描绘了战斗中的景观和两个相对的侧面(虽然我还没有完全在那里)

老实说,我在该计划中没有取得很大进展。 我应该只使用Graphics2D和矩形这样的东西,还是应该切换到OpenGL或类似的东西? 虽然凭借我目前的Java经验,我认为如果没有帮助我就不会对它有太大的帮助。 无论如何,这是我到目前为止所拥有的:

public class Map extends JPanel { int n = 1; int x; int y; int Area = 750; public Color City = new Color(214,217,223); public Color Desert = new Color(255,204,102); public Color DirtRoad = new Color(153,102,0); public Color Forest = new Color(0,102,0); public Color Hills = new Color(51,153,0); public Color Lake = new Color(0,153,153); public Color Mountains = new Color(102,102,255); public Color Ocean = new Color(0,0,153); public Color PavedRoad = new Color(51,51,0); public Color Plains = new Color(102,153,0); public Rectangle blocks[]; public Map(){ blocks = new Rectangle[750]; if (n == 1) { setBackground(City); n = 2; } else if (n == 2) { setBackground(Desert); n = 3; } else if (n == 3) { setBackground(DirtRoad); n = 4; } else if (n == 4) { setBackground(Forest); n = 5; } else if (n == 5) { setBackground(Hills); n = 6; } else if (n == 6) { setBackground(Lake); n = 7; } else if (n == 7) { setBackground(Mountains); n = 8; } else if (n == 8) { setBackground(Ocean); n = 9; } else if (n == 9) { setBackground(PavedRoad); n = 10; } else if (n == 10) { setBackground(Plains); n = 1; } else { } for (int i = 1; i <= Area; i++) { blocks[i] = new Rectangle(x, y, 20, 20); } } 

我用几个Youtube教程得到了这个,所以我的代码有点不稳定。 我在主窗体代码中所拥有的只是一个checkBox触发事件。 (GUI是在Netbeans编辑器中预先设计的。)

1)我强烈建议你坚持在OpenGL之前学习Java 2d。
2)理想情况下,你会有一些模型视图分离 – 你有一个代表地图内容的类和另一个实际渲染它的类。

这里有一些示例代码可以让您更接近目标。 请尝试阅读并理解它,而不仅仅是复制粘贴并劈开它。

在此处输入图像描述

 import javax.swing.*; import java.awt.*; import java.util.Random; public class Map extends JPanel { public static final Color CITY = new Color(214,217,223); public static final Color DESERT = new Color(255,204,102); public static final Color DIRT_ROAD = new Color(153,102,0); public static final Color FOREST = new Color(0,102,0); public static final Color HILLS = new Color(51,153,0); public static final Color LAKE = new Color(0,153,153); public static final Color MOUNTAINS = new Color(102,102,255); public static final Color OCEAN = new Color(0,0,153); public static final Color PAVED_ROAD = new Color(51,51,0); public static final Color PLAINS = new Color(102,153,0); public static final Color[] TERRAIN = { CITY, DESERT, DIRT_ROAD, FOREST, HILLS, LAKE, MOUNTAINS, OCEAN, PAVED_ROAD, PLAINS }; public static final int NUM_ROWS = 25; public static final int NUM_COLS = 30; public static final int PREFERRED_GRID_SIZE_PIXELS = 10; // In reality you will probably want a class here to represent a map tile, // which will include things like dimensions, color, properties in the // game world. Keeping simple just to illustrate. private final Color[][] terrainGrid; public Map(){ this.terrainGrid = new Color[NUM_ROWS][NUM_COLS]; Random r = new Random(); // Randomize the terrain for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j < NUM_COLS; j++) { int randomTerrainIndex = r.nextInt(TERRAIN.length); Color randomColor = TERRAIN[randomTerrainIndex]; this.terrainGrid[i][j] = randomColor; } } int preferredWidth = NUM_COLS * PREFERRED_GRID_SIZE_PIXELS; int preferredHeight = NUM_ROWS * PREFERRED_GRID_SIZE_PIXELS; setPreferredSize(new Dimension(preferredWidth, preferredHeight)); } @Override public void paintComponent(Graphics g) { // Important to call super class method super.paintComponent(g); // Clear the board g.clearRect(0, 0, getWidth(), getHeight()); // Draw the grid int rectWidth = getWidth() / NUM_COLS; int rectHeight = getHeight() / NUM_ROWS; for (int i = 0; i < NUM_ROWS; i++) { for (int j = 0; j < NUM_COLS; j++) { // Upper left corner of this terrain rect int x = i * rectWidth; int y = j * rectHeight; Color terrainColor = terrainGrid[i][j]; g.setColor(terrainColor); g.fillRect(x, y, rectWidth, rectHeight); } } } public static void main(String[] args) { // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html SwingUtilities.invokeLater(new Runnable() { public void run() { JFrame frame = new JFrame("Game"); Map map = new Map(); frame.add(map); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); } }); } } 

老实说,我认为你正在以错误的方式学习java。 如果您正在尝试通过学习这些教程来做到这一点。

我个人在做瓷砖地图方面没有经验,但是在阅读你的代码时我可以看到一些缺失的东西。

通常,当我查看瓷砖地图代码时,我会看到一个二维数组,在您的情况下,它将是一个2D数组,数字范围从1到10放在随机索引中。 根据我的理解,您在显示的代码中所做的只是将图像分配给数字并将它们添加到您的块数组中。

问题是,由于没有2D数组实际可视化您正在创建的地图,所以您所做的只是创建一个数组是一堆带背景的矩形。

我无法理解的另一个问题是,你将地图区域设置为720个索引,这基本上是一个字段,我不知道27个图像中的27个图像将是你创建的那些整数中的x和y值。 让你的JPanel分成你想要创建的隐形瓷砖地图网格。

根据我之前讨论的2D数组索引中的数字,背景将填入相应的图像。

关于OpenGL或Graphics 2D问题:使用什么图形库并不重要,问题是如何编码它。 对于初学者而言,OpenGL是非常先进的(特别是2D,根据我的经验),它是一个3D库,所以尽管2D是可能的,但它有点难。 在进入下一步之前,先了解一些Java API。

希望我帮助,或者有意义O_o再次我从来没有做过瓷砖地图所以我回应你发布的代码。