Java - 使用 Graphics2D 矩形在面板中创建 2D 平铺地图?
我正在尝试在一个非常基本的程序中模拟一场战斗,但由于这是我第一次使用 Java 编写大型程序,所以我对如何进行几乎一无所知。我想我应该有一个 600×600 的大面板,并使用 Graphics2D 将地形绘制为 20x20 矩形......不幸的是,即使有几个教程,我也不知道该怎么做。
我有 10 种不同类型的地形可供循环,以及 5 种不同的景观剖面。基本上,我希望程序做的是,当我在组合框中选择某个配置文件时,它会绘制风景和战斗中的两个对立面(尽管我还没有完全做到这一点)
老实说,我还没有取得很大进展在节目中。我应该只使用 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 教程走到了这一步,所以我的代码有点不稳定。我在主窗体代码中所拥有的只是一个复选框触发事件。 (GUI 是在 Netbeans 编辑器中预先设计的。)
I'm trying to simulate a battle in a really basic program, but since this is my first time with a big program in Java I'm pretty much clueless on how to proceed. I was thinking I'd have a big 600-by-600 panel and use Graphics2D to just draw the terrain as 20x20 rectangles... Unfortunately even with several tutorials I have no idea what to do.
I have 10 different types of terrain to cycle through, and 5 different landscape profiles. Basically what I want the program to do is when I select a certain profile in a combobox, it paints the landscape and the two opposing sides in the battle (though I'm not quite there yet)
Honestly I haven't made very much progress in the program. Should I be using just Graphics2D and rectangles for something like this, or should I switch to OpenGL or something similar? Although with my current Java experience, I don't think I'd get very far with it without help. Anyways, here's what I have so far:
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);
}
}
I got this far with several Youtube tutorials, so my code is a bit erratic. All I have in the main form code is a checkBox firing event. (GUI is pre-designed in Netbeans editor.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1) 我强烈建议你在学习 OpenGL 之前坚持学习 Java 2d。
2)理想情况下,您将有一些模型视图分离 - 您将有一个类代表地图内容,而另一个类则实际渲染它。
这里有一些示例代码,应该可以让您更接近您的目标。请尝试通读并理解它,而不是仅仅复制粘贴并破解它。
1) I would strongly suggest you stick with learning Java 2d before OpenGL.
2) Ideally you would have some model view separation - you'd have one class representing the map contents and another one to actually render it.
Here's some sample code that should get you a bit closer towards your goal. Please try to read through and understand it, not just copy paste and hack away at it.
老实说,我认为你学习 Java 的方式是错误的。如果这就是您想要通过学习这些教程来实现的目标。
我个人没有制作图块地图的经验,但是通过阅读您的代码,我可以看到缺少一些东西。
通常,当我查看图块地图代码时,我会看到一个 2D 数字数组,在您的情况下,它将是一个放置在随机索引中的 1 - 10 范围内的数字的 2D 数组。据我了解,您在显示的代码中所做的只是将图像分配给数字并将它们添加到块数组中。
问题是,由于没有 2D 数组来实际可视化您正在创建的地图,因此您所做的就是创建一个数组,该数组是一堆带有背景的矩形。
我无法理解的另一个问题是,您将地图区域设置为 720 个索引,这基本上是一个字段,我不知道 27 个图像 x 27 个图像将是您创建的那些整数中的 x 和 y 值。留给您的 JPanel 被划分为您要创建的不可见的平铺地图网格。
根据我之前讨论的二维数组索引中的数字,背景将填充相应的图像。
关于 OpenGL 或 Graphics 2D 问题:
使用什么图形库并不重要,问题在于如何编码。 OpenGL 对于初学者来说相当先进(尤其是 2D,根据我的经验),它是一个 3D 库,所以即使 2D 是可能的,但有点困难。在进入下一步之前,请先了解一些 Java API。
希望我有所帮助,或者再次有意义 O_o 我从来没有做过瓷砖地图,所以我正在回复您发布的代码。
I think you're going about learning java the wrong way to be honest. If that's what you're trying to do by learning from these tutorials.
I personally don't have exprience in doing tile maps but there are some things missing that I can see from reading your code.
Usually when I look at tile map code I see a 2D array of numbers, in your case it would be a 2D Array of numbers ranging from 1 - 10 placed in random indexes. From what I understand you're doing in the code you displayed is you're just assigning an image to a number and adding them to your blocks array.
Issue with that is, since there is no 2D array to actually visualize the map you are creating, all you're doing is creating an array is a bunch of Rectangles with backgrounds.
Another issue I can't make sense of is the fact you have your map area set to 720 indexes which is basically a field with, I don't know 27 images by 27 images will be your x and y value in those ints you created. leaving you with a JPanel divided up into the invisible tile map grid you are trying to create.
Based on what number is in the index of the 2D Array I was talking about earlier, the background will be filled in with the corresponding image.
About the OpenGL or Graphics 2D issue:
It doesn't matter what graphics library you use, the issue is how you code it. OpenGL is pretty advanced for a beginner to jump into (especially 2D, from my experience), it's a 3D library so even though 2D is possible, it's kinda hard. Learn some of the Java API before you go into any next step.
Hope I helped, or made sense O_o again I've never done tile maps so I'm responding to the code you posted.