User interaction

Hello, i am making a battleships games and have so far designed the layout of the grids and buttons. The grids are 100 buttons are in a 10 x 10 formation. here is my code so far:-

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class trialgridextends JFrameimplements ActionListener

{

JButton btnStart =new JButton("Start Game");

JButton btnSub =new JButton("Submarine");

JButton btnCruiser =new JButton("Cruiser");

JButton btnDestroy =new JButton("Destroyer");

JButton btnBattle =new JButton("Battleship");

JButton btnReset =new JButton("Reset Game");

JButton btnMenu =new JButton("Menu");

JButton btnExit =new JButton("Exit");

JLabel lblTitle =new JLabel("Welcome to Battleships ", SwingConstants.CENTER);

JLabel lblPlayer1 =new JLabel("Your Battleship grid", SwingConstants.LEFT);

JLabel lblPlayer2 =new JLabel("The enemies Battleship grid", SwingConstants.RIGHT);

JLabel lblList =new JLabel("List of ships");

public trialgrid()

{

JFrame frame =new JFrame("One Player");

Container content = frame.getContentPane();

JPanel title =new JPanel();

title.add(lblPlayer1);

title.add(lblTitle);

title.add(lblPlayer2);

content.add(title, BorderLayout.NORTH);

JPanel controls =new JPanel();

controls.add(btnStart);

controls.add(lblList);

controls.add(btnSub);

controls.add(btnCruiser);

controls.add(btnDestroy);

controls.add(btnBattle);

controls.add(btnReset);

btnReset.setVerticalTextPosition(JButton.BOTTOM);

content.add(controls, BorderLayout.CENTER);

JPanel grid1 =new JPanel();

grid1.setLayout(new GridLayout(10, 10));// sets the grid layout for player 1

for (int i = 0; i < 10; i++)

{

for (int j = 0; j < 10; j++)

{

JButton square =new JButton();

square.setBackground(Color.blue);//nested for loops to create 10 x 10 grid of buttons

grid1.add(square);

}

}

content.add(grid1, BorderLayout.WEST);// Sets grid to west in border layout manager

JPanel grid2 =new JPanel();

grid2.setLayout(new GridLayout(10, 10));// sets the grid layout for the computer

for (int i = 0; i < 10; i++)

{

for (int j = 0; j < 10; j++)

{

JButton square =new JButton();

square.setBackground(Color.blue);//nested for loops to create 10 x 10 grid of buttons

grid2.add(square);

}

}

content.add(grid2, BorderLayout.EAST);// Sets grid to east in border layout manager

JPanel form =new JPanel();// second panel that i want to go beneath grid of buttons

form.add(btnMenu);

form.add(btnExit);

btnExit.addActionListener(this);

content.add(form, BorderLayout.SOUTH);// here i'm trying to put south below "north" grid

frame.pack();

frame.setSize(800, 400);

frame.setVisible(true);

frame.setResizable(false);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

publicstaticvoid main (String args[])

{

trialgrid gl =new trialgrid();

}

publicvoid actionPerformed(ActionEvent evt)

{

Object source = evt.getSource();

if (source == btnExit)

{

System.exit(0);

}

}

}

I am now trying to find a way to allow the user to select a ship using a button and then be able to place it on the grid. I will then have to develope a function that when the user clicks a grid button it checks to see if the computer has a ship in the space or not? I'm really stuck and not sure where to start!! Do i need to use arrays, mouseEvents, all the above!?!?

please, please, please help!!!!

[6442 byte] By [Freedomfighter_2001a] at [2007-10-2 6:43:03]
# 1
imo, you should create buttonlisteners.so on button press it fires an event.
StormcloudFirefoxa at 2007-7-16 13:51:23 > top of Java-index,Java Essentials,New To Java...
# 2
Have you had anymore luck ? I too am trying to create a battleship program using simple swing components. my AIM is ajkniffin , if you want to discuss ideas IM me. Has anyone else written a battleship program ?
kniffin03a at 2007-7-16 13:51:23 > top of Java-index,Java Essentials,New To Java...
# 3
> Has anyone else written a battleship program ?I did not, but Carlton Williams did: http://www.cs.princeton.edu/academics/ugradpgm/spe/summer04/chwillia/
prometheuzza at 2007-7-16 13:51:23 > top of Java-index,Java Essentials,New To Java...
# 4
yeah man IM me on drzoidberg_84@hotmail.com
Freedomfighter_2001a at 2007-7-16 13:51:23 > top of Java-index,Java Essentials,New To Java...