|
-
code fixing??
here is my code i have, this is for a battleships game i am creating, i have made a GUI that works like a dream, it is a border layout and has a set of buttons and two arrays. anyway, i am now trying to use the random function to place ships in one of the grids, for this i am using a direction button to set direction (which is not working) and a switch statement (also not working) ...... i believe this code to be the right answer but i can not get the variables and buttons to work, i have also get lost with a bit of the coding in the switch statement!!
can anyone help me??
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
class trialgrid extends JFrame implements ActionListener
{
JButton btnStart = new JButton("Start Game"); //These buttons are menu buttons
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");
JButton btnSetDirection = new JButton("Direction"); // *** DECLARING DIRECTION BUTTON ***
JButton[][] enemyButtons = new JButton[10][10];
JButton[][] playerButtons = new JButton[10][10];
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");
Dimension gridButtonSize = new Dimension(20, 20);
Dimension menuButtonSize = new Dimension(110, 25);
public trialgrid()
{
int buttonNumber = 0;
JFrame frame = new JFrame("One Player"); // This produces a Border layout that contains 5 panels
Container content = frame.getContentPane();
JPanel title = new JPanel();
title.add(lblPlayer1);
title.add(lblTitle);
title.add(lblPlayer2);
content.add(title, BorderLayout.NORTH); // North panel holds just titles
JPanel center = new JPanel();
center.setLayout(new BorderLayout());
JPanel controls = new JPanel();
JPanel menu = new JPanel();
center.add(btnStart, BorderLayout.NORTH);
btnStart.setPreferredSize(menuButtonSize);
center.add(btnSetDirection, BorderLayout.SOUTH);
btnSetDirection.setPreferredSize(menuButtonSize); // *** HERE IS MY BUTTON TO SET DIRECTION ***
btnSetDirection.setDirection(1); // *** CANNOT GET THIS TO TALK TO LOWER FUNCTION ***
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(110,25)); // Used to create a blank space in menu
menu.add(jp);
menu.add(lblList);
menu.add(btnSub);
btnSub.setPreferredSize(menuButtonSize);
menu.add(btnCruiser);
btnCruiser.setPreferredSize(menuButtonSize);
menu.add(btnDestroy);
btnDestroy.setPreferredSize(menuButtonSize);
menu.add(btnBattle);
btnBattle.setPreferredSize(menuButtonSize);
center.add(controls);
center.add(menu);
content.add(center, BorderLayout.CENTER); //Center panel just contains a menu style list of buttons
JPanel grid1 = new JPanel();
grid1.setLayout(new GridLayout(10, 10)); // sets the grid layout for player 1
for (int i = 0; i < playerButtons.length; i++)
{
for (int j = 0; j < playerButtons[i].length; j++)
{
playerButtons[i][j] = new JButton();
playerButtons[i][j].setPreferredSize(gridButtonSize);
playerButtons[i][j].setBackground(Color.blue);
grid1.add(playerButtons[i][j]);
}
}
content.add(grid1, BorderLayout.WEST); // Sets grid1 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 < enemyButtons.length; i++)
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
enemyButtons[i][j] = new JButton();
enemyButtons[i][j].setPreferredSize(gridButtonSize);
enemyButtons[i][j].addActionListener(this);
enemyButtons[i][j].setBackground(Color.yellow);
grid2.add(enemyButtons[i][j]);
}
}
content.add(grid2, BorderLayout.EAST); // Sets grid2 to east in border layout manager
JPanel form = new JPanel();
form.add(btnMenu);
btnMenu.setPreferredSize(menuButtonSize);
form.add(btnReset);
btnReset.setPreferredSize(menuButtonSize);
btnReset.addActionListener(this);
form.add(btnExit);
btnExit.setPreferredSize(menuButtonSize);
btnExit.addActionListener(this);
content.add(form, BorderLayout.SOUTH); // This just contains two buttons that functional buttons
frame.pack();
frame.setSize(550, 400);
frame.setVisible(true);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main (String args[])
{
trialgrid gl = new trialgrid();
}
public void actionPerformed(ActionEvent evt) // This makes the exit button work
{
Object source = evt.getSource();
if (source == btnExit)
{
System.exit(0);
}
for (int i = 0; i < enemyButtons.length; i++) // Allows you to change the colour of buttons on enemy grid
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
if (source == enemyButtons[i][j])
{
enemyButtons[i][j].setBackground(Color.RED);
}
}
}
for (int i = 0; i < enemyButtons.length; i++) // Allows you to reset all the enemy buttons back to yellow
{
for (int j = 0; j < enemyButtons[i].length; j++)
{
if (source == btnReset)
{
enemyButtons[i][j].setBackground(Color.YELLOW);
}
}
}
}
public void randomPlaceShip()
{
int shipsize = 4; //*** NEED TO HAVE 4 DIFFERENT SHIPS HERE ***
final int fieldSizeX = 10;
final int fieldSizeY = 10;
Random ran = new Random(System.currentTimeMillis()); //*** IS THIS PLACED RIGHT? ***
int randomX = ran.nextInt(fieldSizeX); //*** HOW DO I DECLARE THESE? ***
int randomY = ran.nextInt(fieldSizeY);
int direction = this.getDirection();
placeRandomShip( 4, direction);
switch ( direction )
{
case 0: //assume going right
if ( (randomX + shipSize) < fieldSizeX ) //check if this ship fits to the right
{
for ( int i = randomX; i < randomX + shipSize; i++ )
{
enemyButtons[i][randomY] = blah; // indicator of ship here - *** WHAT DO I PUT HERE? ***
}
}
break;
case 1: //assume going down
if ( (randomY + shipSize) < fieldSizeY )
{ //check if this ship fits to the right
for ( int i = randomY; i < randomY + shipSize; i++ )
{
enemyButtons[randomX][i] = blah; // indicator of ship here
}
}
break;
}
}
}
Thanks again for any help given!
Similar Threads
-
Replies: 150
Last Post: 03-04-2002, 05:40 PM
-
By devi in forum VB Classic
Replies: 3
Last Post: 07-09-2001, 03:25 PM
-
By Steven Bell in forum .NET
Replies: 260
Last Post: 06-01-2001, 04:32 PM
-
Replies: 214
Last Post: 06-01-2001, 07:27 AM
-
Replies: 90
Last Post: 04-17-2001, 12:45 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks