MVC - 使用 Java 中的 Swing/Graphics 设计和显示数组
我正在开发一个小型国际象棋游戏,我认为我已经基本完成了基本模型并准备开始编写 GUI。
我长期以来一直想知道的是,如果棋盘由 [8][8] 的 Square 数组表示,并且我创建一个方法来绘制一个正方形,并以某种方式在该方法和 Square 之间建立关系模型,我会神奇地拥有一个视觉棋盘吗?
我的意思是,假设一个正方形是 10x10 像素,这是否意味着第一个元素(由正方形表示)将从 (0, 0) 开始,元素二在 (10, 0) 处,元素/正方形九在 (0, 10)等等? 当我查看 Graphics2D 时,我注意到绘制矩形的方法需要 x 和 y 坐标,如果我希望位置取决于数组元素,我该怎么办?
或者我必须写下板上每个方块的坐标?
另一件一直困扰我的事情是它是[行][列]还是[列][行]? 我一直在我的代码中使用 [column][row] 因为如果您将其视为 (x, y) 似乎是正确的。 当你上下移动时,你会改变行,= y,等等。
我一直在尝试谷歌搜索以了解如何应用 MVC 设计,要考虑什么,该做什么和不该做什么,等等。到目前为止,我知道模型 = 数据逻辑,视图 = gui,控制 = 交互,结论是要么我不擅长谷歌搜索,要么没有任何针对初学者的关于 MVC 的好信息。
主要是不太明白M、V、C之间的关系。
I'm working on a small Chess game and think I'm almost done with the basic model and ready to start writing the GUI.
What I've been wondering for a long time is that if the Chess board is represented by a Square array of [8][8], and I create a method to draw a square and somehow establish a relation between that method and the Square model, will I then magically have a visual Chess board?
I mean, say that a square is 10x10 pix, does that mean that the first element (represented by a square) will begin at (0, 0), element two at (10, 0), element/square nine at (0, 10) etc?
When I checked out Graphics2D I noticed that the method to draw a rectangle requires x and y coordinates, what do I do if I want the position to depend on the array element?
Or I am going to have to write down the coordinates of each square on the board?
Another thing that has been bothering me is whether it's [row][column] or [column][row]?
I've been using [column][row] in my code since it seems right if you think of it as (x, y).
When you move up and down you change the row, = y, etc.
I've been trying to google around to find out how to apply the MVC-design, what to think about, do's an dont's, etc. The only thing I know this far is that model = data logic, view = gui and control = interaction, the conclusion is that either I'm bad at googling or there isn't any good information targeted at beginners regarding MVC.
Mainly I don't really understand the relation between M, V and C.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么,让我们先解决一个简单的问题:就二维数组而言,实际上由您决定是[行][列]还是[列][行]。 Java 不在乎。只要您在整个应用程序中一致地对待它,您就可以以对您最有意义的方式来思考它。如果有帮助(也可能没有帮助),请记住二维数组只是数组的数组。
至于 MVC,我是这样想的:M 是数据的模型(例如数组),V 是该数据的可视化表示(显示给用户,可能是棋盘),C 是数据的可视化表示(显示给用户,可能是棋盘)控制器,它将两者连接起来并将其中一个的更改传输到另一个,根据需要翻译或以其他方式解释用户操作。如果用户将棋子从一个方格拖动到另一个方格,控制器会解释此手势,实现决定该操作是否合法以及可能产生哪些副作用(例如吃掉棋子)的逻辑,并更新模型和根据需要查看视图。令人困惑的是,大多数 UI 工具包(包括 Java 的工具包)经常模糊这些部分之间的界限,因此控制器的某些部分最终出现在视图或模型或两者中。这不一定是问题,但需要注意。主要的事情是尽可能地在数据、视图以及连接两者的逻辑之间保持明确的边界。
要回答有关绘图和坐标的问题,实际上取决于您如何实现棋盘 UI。例如,您可以完全使用 Java Swing 来实现它,并且通过巧妙地使用 JLabels、JPanels,也许还有 GridLayout,甚至可能是 JTable,您几乎(几乎!)拥有一个自动更新的棋盘。另一方面,您可以利用 Java2D 及其提供的绘图原语从头开始实现您自己的 UI 类,然后您必须对坐标等进行更多管理。
So, let's get the easy question out of the way: as far as 2D arrays goes, it's actually up to you to decide whether it's [row][column] or [column][row]. Java doesn't care. As long as you treat it consistently throughout your app you can think of it whichever way makes most sense to you. If it helps (and it might not) remember that a 2D array is just an array of arrays.
As for MVC, this is how I think of it: M is the model of the data (the array, for example), V is the visual representation of that data (displayed to the user, as a chessboard perhaps) and C is the controller, which connects the two and transmits changes in one to the other, translating or otherwise interpreting user actions as necessary. If the user drags a chess piece from one square to another, the Controller interprets this gesture, implements the logic that decides if the maneuver is legal and what side-effects it might have (like capturing a piece), and updates both the model and the view as required. The confusion lies in that most UI toolkits, Java's included, often blur the boundaries between these pieces, so that bits of the Controller end up in the View, or the Model, or both. It's not necessarily a problem, but it's something to be aware of. The main thing is to try, as far as possible, to keep a well-defined boundary between the data, the view, and the logic that interfaces the two.
To answer your question about drawing and co-ordinates, it really depends how you implement your chessboard UI. You could implement it entirely using Java Swing, for example, and by making clever use of JLabels, JPanels, and maybe a GridLayout, or perhaps even a JTable, you could almost (almost!) have an automagially updating chessboard. On the other hand, you could implement your own UI classes from scratch, making use of Java2D and the drawing primitives it provides, and then you'd have to do more management of co-ordinates and such.
Ri8亲爱的...实际上..我们无法清楚地看到MVC架构在小型应用程序中的优势意味着基于窗口的小型应用程序...但是当您使用大型企业应用程序时..您会意识到..
当大型企业级分布式系统时开发中有很多开发人员、设计师和其他人员正在与之合作。现在,假设开发人员在他们的编码部分进行设计..因此任何设计师都很难理解..因此这部分是分开的,因此设计师可以轻松修改并很好地理解这一点,因此不需要程序员的帮助它是一个 VIEW 组件...。
就像有一些业务逻辑与视图组件结合在一起一样,那么程序员很难理解,并且需要花费太多时间将该逻辑与 VIEW 组件分离,因此任何程序员都可以轻松地 & ;快速理解逻辑..所以逻辑意味着模型和设计意味着任何GUI部分意味着视图组件是分开的...
现在控制器..组件..所以,控制器组件在这两个模型和视图组件之间提供桥梁..它决定可以向最终用户提供哪个视图..
它在模型组件的帮助下控制视图.........
GOt .. IT........!!!!...
如果有任何建议... 最好来...................
Ri8 dear... Actually .. we cant seen clearly advantages of MVC architecture in small application means window based small application... but when you r going with large scale enterprise application .. you realize that ..
When large enterprise level distributed system developing there are lots of developers, designers and other persons are working with it. Now , suppose developer make designing in their coding part .. so it is very hard to understand by any designer.. so this part is seperated so disigner can easily modified and udnerstand this very well and for that there is no need of help of programmer and its a VIEW component....
Same as if there is some business logic is combined with view components then it is hard to understand by programmer and it take too much time to separate that logic from VIEW component, so any programmer can easily & speedly understand that logic .. so Logic means Model and design means any GUI part means VIEW component is separated...
Now Controller .. component .. so, CONTROLLER component provide bridge between those two model and view component.. menan it decided that which view may be given to end user..
and it controll View with the help of model component.............
GOt .. IT........!!!!...
IF any Suggestion... Most Well Come...................