Where do I run AI logic in MVC app.?

时间:2015-10-31 00:03:10

标签: java swing model-view-controller

I need to make a swing application for a school project who let 2 AI play against each others on some kind of snake game. I used a MVC design pattern and created the class I need for some basics test but I'm not sure where I should run the code who makes the AI plays. The class of JPanel I want to run the game into look like that : public class GamePanel extends JPanel { public GamePanel (JFrame frame) { ... } public paintComponent (Graphics g) { ... // draw the grid that represent the game } } I tried to put the code who makes the AI plays in the constructor but it wasn't working properly because I need to call the paintComponent() method each time an AI play (to refresh the grid who represent the game) and i can't call it because getGraphics() gives me a 'null' (I can't call paintComponent() without an instance of Graphics). Where am I supposed to place the code who make the AI play? I've never used Swing before so I think I'm missing something obvious, I'm kinda lost... I hope all of this make sense, English is not my mother tongue and I have some troubles to explain this issue. I really hope someone understand what I meant and can help me!

0 个答案:

没有答案