在 JFrame java 上写一个二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6877373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write a 2D Array on a JFrame java
提问by Jan Vos
So i was wondering I'm new to java but I know my way around it but I wanted to make a 2d tile game. Now I heard that you can do this with a 2d Array to make the map. But how do you make the map appear on the screen, JFrame, as pictures? So an example of the array/map here:
所以我想知道我是 Java 新手,但我知道我的方法,但我想制作一个 2d 瓷砖游戏。现在我听说你可以用一个二维数组来制作地图。但是如何让地图以图片的形式出现在 JFrame 屏幕上呢?所以这里有一个数组/映射的例子:
1111111111
1011011001
1001100011
0000100011
0000000000
2222222222
0 = blueSky.png
1 = cloud.png
2 = grass.png
Thanks! EDIT 2So i have now this:
谢谢! 编辑 2所以我现在有这个:
import javax.swing.*;
import java.awt.*;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
ImageIcon sky = new ImageIcon ("/Users/pro/Desktop/sky.png");
JPanel grid = new JPanel();
grid.setLayout(new GridLayout(25, 25));
for (int i = 0; i < 25; i++) {
for (int n = 0; n < 25; n++) {
grid.add(new JLabel(sky));
}
}
JFrame frame = new JFrame("Map");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setPreferredSize(new Dimension(640, 400));
frame.add(grid);
frame.pack();
frame.setVisible(true);
}
}
this prints some tiles with the sky picture but how do i make the bottom row an other picture?
这会打印一些带有天空图片的瓷砖,但是我如何将底行变成另一张图片?
回答by Hovercraft Full Of Eels
I'd consider the 2D array as the non-GUI model, that the data itself would likely be held in some data file, perhaps a text file, that there would be methods for reading the data in to be held by a 2D array of perhaps ints, perhaps custom Cell classes (again, still all non-GUI). Then the GUI would have to interpret the model and display the data in some logical way. This could perhaps be done by creating a 2D grid of JLabels held by a JPAnel that uses GridLayout, and then use ImageIcons to hold the images, and set the icon of each JLabel based on the state of the model.
我将 2D 数组视为非 GUI 模型,数据本身可能保存在某个数据文件中,也许是一个文本文件,有一些方法可以读取由 2D 数组保存的数据也许是整数,也许是自定义 Cell 类(同样,仍然都是非 GUI)。然后 GUI 必须解释模型并以某种逻辑方式显示数据。这也许可以通过创建由使用 GridLayout 的 JPAnel 保存的 JLabel 的 2D 网格来完成,然后使用 ImageIcons 保存图像,并根据模型的状态设置每个 JLabel 的图标。
Edit 1
So possible classes used include:
编辑 1
所以可能使用的类包括:
- TileType: an enum that associates the tile concept with the numbers held by the data file
- TileCell: non-GUI class, holds a TileType field, also may hold a List of items that can be found on the cell (if the game needs this). May have information about its neighbors.
- TileCellGrid: non-GUI class holding a 2D grid of TileCells.
- GridDataIO: utility class to read in and write out grid data to a file.
- GameGrid: GUI class that would hold a GridLayout using JPanel that holds JLabels whose ImageIcons display the images you list in your OP.
- TileType:一个枚举,将 tile 概念与数据文件中保存的数字相关联
- TileCell:非 GUI 类,包含一个 TileType 字段,也可能包含一个可以在单元格上找到的项目列表(如果游戏需要这个)。可能有关于它的邻居的信息。
- TileCellGrid:持有 TileCell 的 2D 网格的非 GUI 类。
- GridDataIO:用于将网格数据读入和写出到文件的实用程序类。
- GameGrid:GUI 类将使用 JPanel 保存 GridLayout,该 JPanel 保存 JLabel,其 ImageIcons 显示您在 OP 中列出的图像。
Edit 2
regarding your question:
编辑 2
关于您的问题:
Alright how can i set the right picture for everyLabel ?
好吧,我如何为每个标签设置正确的图片?
I would use an observer/observable pattern and add a listener to the model. Whenever the model changes it should thus notify the GUI or view. Then the view would request the data array, would iterate through it and would change the image icons that need changing as it loops through the array.
我会使用观察者/可观察模式并向模型添加一个监听器。每当模型更改时,它应该通知 GUI 或视图。然后视图将请求数据数组,将遍历它并在它循环遍历数组时更改需要更改的图像图标。
回答by aioobe
I suggest you use JLabel
s with icons and lay them out using GridLayout
.
我建议你使用JLabel
带有图标的 s 并使用GridLayout
.
Related question / answer with sample code and screen shot:
与示例代码和屏幕截图相关的问题/答案: