Hi guys,
Im trying to do battleships game. But I dont know, what is the best way to create interactive battlefield? I created JFrame and put inside two JPanels (my and opponents field).I wanted to put inside JButtons (JFrame had GridLayout), but I was told this isnt right solution. It should be done with MouseListener. So I created JPanel and added mouselistener. Now, when I click somewhere in the field, program writes coordinates of field. But how to do interaction? I mean if you click somewhere, how should program find out what "box" did user hit and how to change/add icon on that position?

Thanx for answers

Recommended Answers

All 5 Replies

Who told you not to use JButtons? If it was your teacher then you have no choice, but if it was someone else then I would disagree. A grid of buttons (or JLabels) would be a perfectly sensible implementation.
So, if you're stuck with one panel...
you have the mouse position, you have the width/height of the panel, you know how many rows/cols of boxes there are, so you know the width/height of each box, so the rest is just arithmetic. Try doing a sample case by hand to see exactly what the calculation is.
To draw icons (and grid lines etc) in your panel you need to create and use your own subclass of JPanel, and override its paintComponent method to draw lines and paint icons in the right position. You'll find loads of paintComponent tutorials and examples on the web.

JamesCherrill is pretty much spot-on on the matter. On the architectural side it is customary to break the application structure in logical components. One of typical models is called MVC (Model-View-Controller, see. http://www.enode.com/x/markup/tutorial/mvc.html for example).

In MVC design pattern you split the application into three logical parts: the Model is the application engine that contains all the generic functionality and data (in this case the board data for both players and methods to access and manipulate those boards). The View(s) displays the model (or part of it) to the user in some manner (in this case it would be the battleship board grid plus some possible controls like menus etc.) and the Controller handles the user input and changes them into a method calls to the Model. In Java application the Controller is usually a fixed part of the View.

So, in this case - for example - you would record the mouse click (View), calculate the board coordinates from it and call appropriate Model method (Controller). In Model, you check the opponents board cell if it has been fired into already and if the cell contains a ship and notify the Views (and Controllers if needed) about the Model change (invalid cell, missed / hit).

Check also the Observer pattern (http://www.research.ibm.com/designpatterns/example.htm) as it is the foundation of MVC model.

@Rasas. spot-on yourself also! (Although this may be a bit too much too soon for a beginner)

Yup, probably way too soon for a beginner.

However, I myself had a hard time to understand how to create applications until I ran into design patterns and architectural design altogether. After that it all clicked into place and I started to see the applications as a whole. I think part of the original poster's problem may be that he cannot form a picture of how the application could be put together.

I also wanted to bring the design patters to attention because at some (later) point they will become very important tools. Especially MVC when coding with Swing as all Swing components are build using the MVC model.

^ agree 100% J.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.