creating a GUI battleship game in java

Started by
6 comments, last by ractoc 15 years, 7 months ago
I have a battleship game in java that 2 people can play over a network. Right now it is all console text based. I want to make it graphical. I made a tetris game with C++ and for that i had the tetris board be a 2d int array and then based on what value was at that position in the array i painted a piece there. This can be done the same way because the text based version of my battleship game just updates a 2d array for the boards. I think all i would have to do is add something in main that went through the 2d char array game board and drew something at each block. My java gui experience is limited and the only things i've seen from it are where they have a new frame be created inside main and then make an instance of that same class inside that classes main method and it called a paintComponent which i guess overrides a paintComponent method from a different class? I'm confused on how java handles GUI. Would it be that hard to add a method that creates a frame and draws different shapes to that frame based on the values of a 2d array because all the logic of playing the game is done i just cant understand java gui. Too many classes. I just need a paint method that goes through the 2d array and draws stuff to a frame and do that after every turn. Any suggestions?
Advertisement
Yes, as a matter of factly, it is quite simple to override the paint method of a frame, panel, or any ui object for that matter and perform your own painting. User interfaces are completely abstract and they only correlate to your data as you see fit. Would you like an example? Here is some code from a class that extends JPanel from javax.swing:

...        // This is where I paint my JPanel the way I want it to look	public void paintComponent(Graphics g) {		super.paintComponent(g);				this.setIgnoreRepaint(true);				BufferedImage backgroundImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);		Graphics2D bi = backgroundImage.createGraphics();		Color darkGray = new Color(136, 136, 136);		bi.setColor(Color.LIGHT_GRAY);		bi.fillRect(0, 0, 16, 16);		bi.setColor(darkGray);		bi.fillRect(16, 0, 16, 16);		bi.setColor(darkGray);		bi.fillRect(0, 16, 16, 16);		bi.setColor(Color.LIGHT_GRAY);		bi.fillRect(16, 16, 16, 16);				TexturePaint tp = new TexturePaint(backgroundImage, new Rectangle(0, 0, 32, 32));				BufferedImage bg = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);				Graphics2D g2 = bg.createGraphics();		g2.setPaint(tp);		g2.fill(new Rectangle(0, 0, _width, _height));				g.drawImage(bg, 0, 0, this);			}	        // This is the actual method I call to do it, which overrides JPanel's	public void update(Graphics g) {		paintComponent(g);	}
hey there,

Just thought I'd hijack this thread for a bit... sorry about that.

I've recently started work on a Java based space flight shooter/rpg/trade game with a few friends. I'm currently the only actual programmer and we could totally use your help if you want. We are using JMonkeyEngine as the 3D engine. I'm currently working on the Map system but I could use some help on other parts like movement and such. The game will be build in three stages, first a single player demo version. Then a multi player version for up to 10 players and finally a full on-line version with a persistent universe and everything (this is a long way off though).

If you're interested drop me a line and we'll talk. We have a project management environment we use for setting tasks scheduling meetings / forum / svn etc.
@ nathandelane,
So then would i have the class with my main method extend jpanel and then i would over ride the paint method and then everywhere i would display the board i would call the paint method? Or would the Board class that has the board extend jpanel and over ride the paint method and then everywhere where i call the method to print the board i would call that paint method or something like that?

@ ractoc,
I dont know. It sounds like an interesting project that could be fun. However, with school starting again next week and a part time job on top of that i dont think i will really have much time. Maybe I could fit it in but i will have to see how that schedule will work out first. Thanks for the offer though.
You would create a new class that extends JPanel and overrides the Paint method -- in the Pain method you would put what you want to paint the board as based on the data, but create a "game loop" inside of a different class also containing your main method, and have the game loop call the update method (because you're technically not supposed to call the Paint method directly).

Nathan
so basically i would leave everything how it is now but add another class that would handle the graphics. All this class would do is extend JPanel and have the paint method that paints the board and then another method that calls that paint method. Now can i pass this paint method a Board object. If i can't how do i get it to see the game board? Then another question is i have the players enter their names and where they want to place the ships. I am thinking of doing this through prompts that pop up when a new game is started. Right now i prompt the user enter their name in the Player constructor and i have a method 1 for each ship to enter the placement of the ships. How would i move this so i can have the pop up text prompts instead?
so i started messing with the GUI builder thing in netbeans. I have used this before. I made a JFrame Form and added JOptionPane to enter the name. I guess i would do this and then add another frame to the form that would draw the board? Does that sound right? I still have trouble figuring out how i get this new JFrameForm class to "see" the other classes.
One way of making it possible for your paint method to know the board is to pass it in the constructor of you class. Then set it as a class variable.
For getting the player names etc. I would just ask for it in the same loop you use to make the ships etc. (I'm guessing you made a loop to do this.)

This topic is closed to new replies.

Advertisement