Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 4 FirstFirst 1234 LastLast
Results 26 to 50 of 86

Thread: Battleship GUI Program

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    how do I get the buttons to be visible in the pane if it's mainly defined outside the constructor?
    What? Class level variables are visible in all class methods.

    Do you understand the code I posted? What do the comments in the code say?

  2. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  3. #27
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Quote Originally Posted by Norm View Post
    What? Class level variables are visible in all class methods.

    Do you understand the code I posted? What do the comments in the code say?
    Which array is that, the char array or the button one?

    Am I giving the values to each element?

    Ok, so defining it in the class but outside the constructor will make it accessible to the actionPerformed method?
    Last edited by javapenguin; June 4th, 2010 at 11:13 AM.

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    Put all variable definitions at the class level so that class methods can see them.
    Am I giving the values to each element?
    Not sure what you mean? Are you asking if your code does something? I'd have to see the code to tell you.

  5. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  6. #29
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Quote Originally Posted by Norm View Post
    Put all variable definitions at the class level so that class methods can see them.

    Not sure what you mean? Are you asking if your code does something? I'd have to see the code to tell you.
    Now all the compiler errors went away, but now it's throwing Null Pointer Exceptions like crazy and none of my buttons will work and my shot counter won't work because of that.

    It keeps citing the placeBattleship method as the source of the error.

      private static void placeBattleship(char charArray[][])
          { // beginning of method placeBattleship
             int valueOne, valueTwo;
             char B;
             valueOne= (int) ( 1 + (Math.random() * charArray.length -1)); 
             valueTwo = (int) ( 1 + (Math.random() * charArray.length -1 ));
             charArray[valueOne][valueTwo] = 'B';
     
          } //end of method placeBattleship

    Also, I never defined the arrays in the actionPerformed method, just above the constructor and gave it values in the constructor like you said.

    Also, is it my layout:

    gridSizeStr = JOptionPane.showInputDialog("Enter the size of the grid:");
                   n = Integer.parseInt(gridSizeStr);
     pane.setLayout(new GridLayout((n+1), (n+1)));

    Or is it my code for char array:
    char [][] charArray = new char[n][n];
                for (  int x = 0; x < charArray.length; x++)
                {
                   for (  int y = 0; y < charArray[x].length; y++)
                      charArray[x][y] = '0'; 
     
                }

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    Null Pointer Exceptions
    When you get errors, please copy and paste the full text of the error message. For large programs copy and paste the section of code around where the error occurs and put a comment on the line referred to in the error message.
    char [][] charArray = new char[n][n];
    What is the location of the above line of code. It defines a variable: charArray that is a two dim array. It also gives it a value and the following lines put data into it. If the code is INSIDE a method the variable goes away when the method exits!

    If there is a class variable named charArray, this local definition overrides and hides it.
    Remove "char [][]" from the above code if you want to use the class variable vs create a new one with the same name.
    See my posting #24
    Last edited by Norm; June 4th, 2010 at 12:16 PM.

  8. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  9. #31
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at BattleshipGUI.placeBattleship(BattleshipGUI.java:3 67)
    at BattleshipGUI.actionPerformed(BattleshipGUI.java:2 36)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JFrame;
       import javax.swing.JDialog;
       import javax.swing.JPanel;
       import java.awt.Graphics;
       import javax.swing.ImageIcon;
       import java.util.*;
       import java.io.*;
       import java.awt.Component;
     
        public class BattleshipGUI extends JFrame 
        implements ActionListener
       { // beginning of class
        // label
          private JLabel shotsLabel, arraySize;
     
       // area where text is displayed and user would normally enter text but I have made it so the user can't alter the shot count.
          private JTextField shotsTF, arraySizeTF;
     
          JFrame frame;
     
          JButton [][] arrayOfButtons;
          char [][] charArray;
     
       // buttons
          private JButton surrenderB, quitB;
     
          private static final int WIDTH = 500;
          private static final int HEIGHT = 500;
     
     
           public BattleshipGUI()
          { // beginning of constructor
             setTitle("Battleship Game");
             Container pane = getContentPane();
     
     
                	// makes Label for shots
             shotsLabel = new JLabel("Shots: ",
                                      SwingConstants.RIGHT);
             arraySize= new JLabel("Array Size ",
                                      SwingConstants.RIGHT);	
     
            // makes Text Field for shots and makes the text field non-editable
             shotsTF = new JTextField(7);      
             shotsTF.setEditable(false);
     
             arraySizeTF = new JTextField(2);
             arraySizeTF.setEditable(false);
     
          			// makes the buttons and sets their value initially to 0 and makes the method actionPerformed
          			// a method of the class BattleshipGUI.  I don't need individual handlers.
     
             surrenderB = new JButton("Surrender");
             surrenderB.addActionListener(this);
     
             quitB = new JButton("Quit");
             quitB.addActionListener(this);
     
          // n is the size of the grid
             int n = 0;
             String gridSizeStr, outputStr;
             boolean isInRange = false;
          // while loop and boolean allow user to keep entering int values till n is between 2 and 10.  Also, it needs to have 
          // the array of buttons  in a square shape,  and also so I can place the battleship in the array and later use actionListeners to
          // change the text to either a M, an A, or a H.  Hence why all buttons in the array have the same label to begin with.
     
             shotsTF.setText("0");
             boolean isValid = false;
     
             while (isValid == false)
             { // beginning of while
                try
                { // beginning of try
                   gridSizeStr = JOptionPane.showInputDialog("Enter the size of the grid:");
                   n = Integer.parseInt(gridSizeStr);
     
                   if (n < 2)
                      throw new MyTooSmallException();
     
                   if (n > 10)
                      throw new MyTooBigException();
     
                   isValid = true;
     
                } // end of try
     
                    catch ( MyTooSmallException mtse)
                   { // beginning of catch
     
                      JOptionPane.showMessageDialog(null, "Enter a value that is not less than" 
                         + " 2 and is no greater than 10. " +"\n " + mtse.toString() );
     
                   } // end of catch
     
                    catch ( MyTooBigException mtbe)
                   { // beginning of catch
     
                      JOptionPane.showMessageDialog(null, "Enter a value that is not less than" 
                         + "2 and is no greater than 10. " +"\n " + mtbe.toString() );
     
                   } // end of catch
     
     
                    catch (NumberFormatException nfeRef)          
                   { // beginning of catch
                      JOptionPane.showMessageDialog(null,
                         "Enter an integer.  Exception "
                         + nfeRef.toString(),
                         "NumberFormatException",
                         JOptionPane.ERROR_MESSAGE);       
                   } // end of catch
     
             } // end of while 
     
     
             if (n >=2 && n <= 10)
             { // beginning of if				
     
                Integer Int;
     
                Int = n;
     
                arraySizeTF.setText(Int.toString());
     
                char [][] charArray = new char[n][n];
                for (  int x = 0; x < charArray.length; x++)
                {
                   for (  int y = 0; y < charArray[x].length; y++)
                      charArray[x][y] = '0'; 
     
                } 
     
                JButton[][] arrayOfButtons = new JButton[n][n];
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (  int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j] = new JButton("0");
                }
     
                for (int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j].addActionListener(this);
                }
     
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      pane.add(arrayOfButtons[i][j]);
                }
     
     
     
                pane.setLayout(new GridLayout((n+1), (n+1)));
     
     
                pane.add(surrenderB);
                pane.add(quitB);
                pane.add(shotsLabel);
                pane.add(shotsTF);
     
               // the char array is where the actual 1 square battleship is going to be placed.
               // it is a n by n grid and the other array needs to be too to make them parallel.  
               // A value in the arrayOfButtons[i][j] should be the same as charArray[x][y].
     
     
     
     
             } // end of if      
     
     
     
     
          // makes an n by n grid
     
     
             setSize(WIDTH, HEIGHT);
             setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
          } // end of constructor
     
           public static void main(String[] args)
          { // beginning of main
             BattleshipGUI refVar = new BattleshipGUI();
     
     
     
          } // end of main
     
       	// this method handles the buttons.  
           public void actionPerformed(ActionEvent e)
          { // beginning of method
             String outputStr2, outputStr3, outputStr4, outputStr5, outputStr6;
             String outputStrM, outputStrA, outputStrAA;
             outputStrM = "Miss";
             outputStrA = "Already shot there";
             outputStrAA = "I told you that you already shot there";
     
             outputStr2 = "Are you sure you want to quit? ";
             outputStr3 = "You Quit.";
             outputStr4 = "Ok don't quit then." ;
             outputStr5 = "Thought not.";
             outputStr6 = "You surrendered.";
     
     
             int i, j, x,y;
             x = 0;
             i = 0;
             y = 0;
             j = 0;
     
             int shots =  Integer.parseInt(shotsTF.getText());
     
     
     
     
     
             placeBattleship(charArray); 
     
             Integer tries;
             Integer tries2;
             Integer tries3;
             Integer tries4;
             Integer tries5;
     
             tries = shots;
             tries2 = shots;
             tries3 = shots;
             tries4 = shots;
             tries5 = shots;
     
             if (e.getActionCommand().equals("Quit"))
             { // beginning of if
                Object[] options = {"Yes",
                        "Continue Playing"};
     
                int n = JOptionPane.showOptionDialog(frame,
                   "Are you sure you want to Quit?",
                   "Quit",
                   JOptionPane.YES_NO_OPTION,
                   JOptionPane.QUESTION_MESSAGE,
                   null,        options,     options[0]); 
             // creates Yes and Continue playing butons buttons.
             		// if Yee button is clicked, sends output "You quit." as heading and sends a message "Adiós!" and exits when 
             		// user clicks OK on dialog box
                if (n == JOptionPane.YES_OPTION) 
                { // beginning of if
                   JOptionPane.showMessageDialog(null,"Adiós!", outputStr3,  JOptionPane.PLAIN_MESSAGE);
                   System.exit(0);
                } // end of if
                // changes the string the standard dialog box for the No button but still uses its predefined return value.  If user clicks on 
                // Continue Playing, sends title "Ok don't quit then." and the message "Whatever." and takes the user back to the grid.
                else if (n == JOptionPane.NO_OPTION)
                { // beginning of else if
                   JOptionPane.showMessageDialog(null,"Whatever.",  outputStr4,  JOptionPane.PLAIN_MESSAGE);
                } // end of else if
     
             } // end of if
     
             else if (e.getActionCommand().equals("Surrender"))
             { // beginning of else if
                shots = shots + 1;
                Object[] options2 = {"Yes",
                        "No, I have not yet begun to fight!"};
                int p = JOptionPane.showOptionDialog(frame,
                   "Are you sure you want to Surrender?",
                   "Surrender?",
                   JOptionPane.YES_NO_OPTION,
                   JOptionPane.QUESTION_MESSAGE,
                   null,        options2,   options2[0]); 
             // creates Yes and No, I have not yet begun to fight! butons .
             		// if Yes button is clicked, sends output "You surrendered." as heading and sends a message "It took you [number of shots]
             		// shots!" and exits when user clicks OK on dialog box
                if (p == JOptionPane.YES_OPTION) 
                { // beginning of if
                // code for showing grid
                   arrayOfButtons[i][j].setText("B");
                   JOptionPane.showMessageDialog(null, "You took "  + shotsTF.getText() + " shots" , outputStr6, JOptionPane.INFORMATION_MESSAGE);
                   System.exit(0);
                } // end of if
                // changes the string the standard dialog box for the No button but still uses its predefined return value.  If user clicks on 
                // "No button", sends output "Thought not." and the title "You didn't surrender." and takes the user back to the grid.
                else if (p == JOptionPane.NO_OPTION)
                { // beginning of else if
                   JOptionPane.showMessageDialog(null, outputStr5, "You didn't surrender." , JOptionPane.PLAIN_MESSAGE);
                } // end of else if
             } // end of else if
     
     
             else if (e.getActionCommand().equals("0"))
             { // beginning of else if
     
     
                if (  Fire(charArray, x, y) == 'H')
                { // beginning of if
                   String outputStr7;
                   arrayOfButtons[i][j].setText("H");  // changes text on button to H
                   shots = shots + 1;
                   tries = new Integer(shots);
                   shotsTF.setText(tries.toString());
     
                   outputStr7 = "You sank my battleship. " + "\n" + "You sank my battleship in" + shotsTF.getText() + "tries";
                   JOptionPane.showMessageDialog(null, outputStr7 , "You win!", +  JOptionPane.INFORMATION_MESSAGE);
                   System.exit(0);
                }// end of if
     
                else 
                { // beginning of else
                   JOptionPane.showMessageDialog(null, outputStrM , "You missed!", +  JOptionPane.INFORMATION_MESSAGE);
                   arrayOfButtons[i][j].setText("M"); // changes text on button to M
                   shots = shots + 1;
                   tries2 = new Integer(shots);
                   shotsTF.setText(tries2.toString());
     
                } // end of else
             } // end of else if 
                   // does this if text on button hit is M.
             else if (e.getActionCommand().equals("M"))
             { // beginning of else if
                shots = shots + 1;
                tries3 = new Integer(shots);
                shotsTF.setText(tries3.toString());
     
                JOptionPane.showMessageDialog(null, outputStrA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text on button to A
             } // end of else if
                   // does this if text on button hit is A
             else if (e.getActionCommand().equals("A"))
             { // beginning of else if
                shots = shots + 1;
                tries4 = new Integer(shots);
                shotsTF.setText(tries4.toString());
     
                JOptionPane.showMessageDialog(null, outputStrAA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text, sorta, on button to A
             } // end of else if
     
          } // end of method
     
          // the method placeBattleship, places the battleship by randomly choosing a
       // row and column coordinate and storing a 'B' there.  The 'B' is not displayed to 
       // the user unless the user hits the surrender button.  It takes the array as a parameter and 
       // should return nothing.  This method sets the location of the battleship.
     
           private static void placeBattleship(char charArray[][])
          { // beginning of method placeBattleship
             int valueOne, valueTwo;
             char B;
             valueOne= (int) ( 1 + (Math.random() * charArray.length -1)); 
             valueTwo = (int) ( 1 + (Math.random() * charArray.length -1 ));
             charArray[valueOne][valueTwo] = 'B';
     
          } //end of method placeBattleship
     
           public static char Fire(char charArray[][], int x, int y)
          { // beginning of method Fire
             char A;
             char M;
             char H;
     
             if (charArray[x][y] == 'B')  // if battleship is hit return H and end game
             { // beginning of if statement
                charArray[x][y] = 'H';
     
     
                return ('H');
     
             } // end of if statement
     
             else if (charArray[x][y] == 'M')  // this is else if 
                                              // because it could also 
                                              // be just a miss.
     
             { // beginning of else if statement
     
                charArray[x][y] = 'A';
     
                return ('A');
             } // end of else if statement
     
             else if (charArray[x][y] == 'A') // makes sure it won't set an A to an M again.
             { // beginning of else if  
     
                charArray[x][y] = 'A';
                return ('A');
             } // end of else if
             else
     
             { // beginning of else statement
                charArray[x][y] = 'M'; 
                return ('M');                                          
             } // end of else statement
          } // end of method fire
     
     
       } // end of program

  10. #32
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    It's getting closer.



    But I still have this mysterious problem:

    It now lets my non-grid buttons work. My grid buttons will only show the output miss, like they're supposed to, and display this error message:

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at BattleshipGUI.actionPerformed(BattleshipGUI.java:3 24)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6263)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:602 8)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4630)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4574)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 60)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)

    If I click on a certain one, it doesn't say miss unless I click it twice. I think that it's setting it but losing it and it's still somehow pointing to nothing.

  11. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    at BattleshipGUI.actionPerformed(BattleshipGUI.java:3 24)
    Can you post the code around lines 324?
    What variable is at line 324 that is null? What is it null? Where in the code does it set it to a value?

  12. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  13. #34
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

     else 
                { // beginning of else
                   JOptionPane.showMessageDialog(null, outputStrM , "You missed!", +    OptionPane.INFORMATION_MESSAGE);
                   arrayOfButtons[i][j].setText("M"); // changes text on button to M  // line 324
                   shots = shots + 1;
                   tries2 = new Integer(shots);
                   shotsTF.setText(tries2.toString());
     
                } // end of else

  14. #35
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    Is arrayOfButtons null at line 324?
    Where does the code give arrayOfButtons a value? I mean the class member variable NOT one defined in a method;

    Read up on "scope". A term describing where a variable is known and where not.

  15. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  16. #36
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Quote Originally Posted by Norm View Post
    Is arrayOfButtons null at line 324?
    Where does the code give arrayOfButtons a value? I mean the class member variable NOT one defined in a method;

    Read up on "scope". A term describing where a variable is known and where not.
    Ok, seems if I put them in main, they'll always be visible.



    Ok, I'm lost.

    I defined arrayOfButtons[][], i.e.

    arrayOfButtons = new JButton[n][n] in the constructor.

    but put

    JButton arrayOfButtons[][] before constructor.

    Perhaps the error lies with these for loops:

    c
    harArray = new char[n][n];
                for (  int x = 0; x < charArray.length; x++)
                {
                   for (  int y = 0; y < charArray[x].length; y++)
                      charArray[x][y] = '0'; 
     
                } 
     
                JButton[][] arrayOfButtons = new JButton[n][n];
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (  int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j] = new JButton("0");
                }
     
                for (int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j].addActionListener(this);
                }
     
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      pane.add(arrayOfButtons[i][j]);
                }

    Last edited by javapenguin; June 4th, 2010 at 02:45 PM.

  17. #37
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    arrayOfButtons = new JButton[n][n] in the constructor.
    That is not a definition. That assigns a value to a variable.

    char[][] arrayOfButtons; // define the variable arrayOfButtons as a 2 dim array of char

    JButton[][] arrayOfButtons = new JButton[n][n];
    That is a local definition of the variable that is only visible inside a method. It masks/hides the any class level definition.
    Remove the "JButton[][]" to change its being a definition to being an assignment.

  18. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  19. #38
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    It seems that nothing is in arrayOfButtons[][].

    How do I put something in it?

    It's an array of Buttons. You can't put a value in a button, can you?

  20. #39
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Ok, now my assignments aren't working correctly.

    Also, should I remove the

    char[][] in

    char[][] charArray = new char[n][n];

  21. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    arrayOfButtons[i][j] = new JButton("a btn");
    put a value in a button
    What does that mean? You need to be a bit more specific.
    For example: Can you save a String(or at least a reference to a String) in a JButton object?

    Yes, a couple of ways. See the put/getClientProperties() methods.
    Extend the JButton class with your own class that has a String variable member and has accessor methods for the String.


    Just got your second post?
    now my assignments aren't working correctly
    Please explain. Compile or runtime errors?

  22. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  23. #41
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    How come it's only setting arrayOfButtons[0][0] to M when it's a miss, even if I didn't click there, and if I do click there, it sets it to A, even if I've only shot there once and if it click another one, it sets it back to M again?

     else if (e.getActionCommand().equals("0"))
             { // beginning of else if
     
     
                if (  Fire(charArray, x, y) == 'H')
                { // beginning of if
                   String outputStr7;
                   arrayOfButtons[i][j].setText("H");  // changes text on button to H
                   shots = shots + 1;
                   tries = new Integer(shots);
                   shotsTF.setText(tries.toString());
     
                   outputStr7 = "You sank my battleship. " + "\n" + "You sank my battleship in" + shotsTF.getText() + "tries";
                   JOptionPane.showMessageDialog(null, outputStr7 , "You win!", +  JOptionPane.INFORMATION_MESSAGE);
                   System.exit(0);
                }// end of if
     
                else 
                { // beginning of else
                   JOptionPane.showMessageDialog(null, outputStrM , "You missed!", +  JOptionPane.INFORMATION_MESSAGE);
                   arrayOfButtons[i][j].setText("M"); // changes text on button to M
                   shots = shots + 1;
                   tries2 = new Integer(shots);
                   shotsTF.setText(tries2.toString());
     
                } // end of else
             } // end of else if 
                   // does this if text on button hit is M.
             else if (e.getActionCommand().equals("M"))
             { // beginning of else if
                shots = shots + 1;
                tries3 = new Integer(shots);
                shotsTF.setText(tries3.toString());
     
                JOptionPane.showMessageDialog(null, outputStrA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text on button to A
             } // end of else if
                   // does this if text on button hit is A
             else if (e.getActionCommand().equals("A"))
             { // beginning of else if
                shots = shots + 1;
                tries4 = new Integer(shots);
                shotsTF.setText(tries4.toString());
     
                JOptionPane.showMessageDialog(null, outputStrAA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text, sorta, on button to A
             } // end of else if

    public static char Fire(char charArray[][], int x, int y)
          { // beginning of method Fire
             char A;
             char M;
             char H;
     
             if (charArray[x][y] == 'B')  // if battleship is hit return H and end game
             { // beginning of if statement
                charArray[x][y] = 'H';
     
     
                return ('H');
     
             } // end of if statement
     
             else if (charArray[x][y] == 'M')  // this is else if 
                                              // because it could also 
                                              // be just a miss.
     
             { // beginning of else if statement
     
                charArray[x][y] = 'A';
     
                return ('A');
             } // end of else if statement
     
             else if (charArray[x][y] == 'A') // makes sure it won't set an A to an M again.
             { // beginning of else if  
     
                charArray[x][y] = 'A';
                return ('A');
             } // end of else if
             else
     
             { // beginning of else statement
                charArray[x][y] = 'M'; 
                return ('M');                                          
             } // end of else statement
          } // end of method fire

    It's so close!

    ^^


  24. #42
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Let's see what could be causing the remaining glitches, though it now no longer throws those exceptions.

    import java.awt.*;
       import java.awt.event.*;
       import javax.swing.*;
       import javax.swing.JFrame;
       import javax.swing.JDialog;
       import javax.swing.JPanel;
       import java.awt.Graphics;
       import javax.swing.ImageIcon;
       import java.util.*;
       import java.io.*;
       import java.awt.Component;
     
        public class BattleshipGUI extends JFrame 
        implements ActionListener
       { // beginning of class
        // label
          private JLabel shotsLabel, arraySize;
     
       // area where text is displayed and user would normally enter text but I have made it so the user can't alter the shot count.
          private JTextField shotsTF, arraySizeTF;
     
          JFrame frame;
     
          JButton [][] arrayOfButtons;
          char [][] charArray;
     
       // buttons
          private JButton surrenderB, quitB;
     
          private static final int WIDTH = 500;
          private static final int HEIGHT = 500;
     
     
           public BattleshipGUI()
          { // beginning of constructor
             setTitle("Battleship Game");
             Container pane = getContentPane();
     
     
                	// makes Label for shots
             shotsLabel = new JLabel("Shots: ",
                                      SwingConstants.RIGHT);
             arraySize= new JLabel("Array Size ",
                                      SwingConstants.RIGHT);	
     
            // makes Text Field for shots and makes the text field non-editable
             shotsTF = new JTextField(7);      
             shotsTF.setEditable(false);
     
             arraySizeTF = new JTextField(2);
             arraySizeTF.setEditable(false);
     
          			// makes the buttons and sets their value initially to 0 and makes the method actionPerformed
          			// a method of the class BattleshipGUI.  I don't need individual handlers.
     
             surrenderB = new JButton("Surrender");
             surrenderB.addActionListener(this);
     
             quitB = new JButton("Quit");
             quitB.addActionListener(this);
     
          // n is the size of the grid
             int n = 0;
             String gridSizeStr, outputStr;
             boolean isInRange = false;
          // while loop and boolean allow user to keep entering int values till n is between 2 and 10.  Also, it needs to have 
          // the array of buttons  in a square shape,  and also so I can place the battleship in the array and later use actionListeners to
          // change the text to either a M, an A, or a H.  Hence why all buttons in the array have the same label to begin with.
     
             shotsTF.setText("0");
             boolean isValid = false;
     
             while (isValid == false)
             { // beginning of while
                try
                { // beginning of try
                   gridSizeStr = JOptionPane.showInputDialog("Enter the size of the grid:");
                   n = Integer.parseInt(gridSizeStr);
     
                   if (n < 2)
                      throw new MyTooSmallException();
     
                   if (n > 10)
                      throw new MyTooBigException();
     
                   isValid = true;
     
                } // end of try
     
                    catch ( MyTooSmallException mtse)
                   { // beginning of catch
     
                      JOptionPane.showMessageDialog(null, "Enter a value that is not less than" 
                         + " 2 and is no greater than 10. " +"\n " + mtse.toString() );
     
                   } // end of catch
     
                    catch ( MyTooBigException mtbe)
                   { // beginning of catch
     
                      JOptionPane.showMessageDialog(null, "Enter a value that is not less than" 
                         + "2 and is no greater than 10. " +"\n " + mtbe.toString() );
     
                   } // end of catch
     
     
                    catch (NumberFormatException nfeRef)          
                   { // beginning of catch
                      JOptionPane.showMessageDialog(null,
                         "Enter an integer.  Exception "
                         + nfeRef.toString(),
                         "NumberFormatException",
                         JOptionPane.ERROR_MESSAGE);       
                   } // end of catch
     
             } // end of while 
     
     
             if (n >=2 && n <= 10)
             { // beginning of if				
     
     
     
                charArray = new char[n][n];
                for (  int x = 0; x < charArray.length; x++)
                {
                   for (  int y = 0; y < charArray[x].length; y++)
                      charArray[x][y] = '0'; 
     
                } 
     
                 arrayOfButtons = new JButton[n][n];
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (  int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j] = new JButton("0");
                }
     
                for (int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      arrayOfButtons[i][j].addActionListener(this);
                }
     
     
                for ( int i = 0; i < arrayOfButtons.length; i++)
                {
                   for (   int j = 0; j < arrayOfButtons[i].length; j++)
                      pane.add(arrayOfButtons[i][j]);
                }
     
     
     
     
                pane.setLayout(new GridLayout((n+1), (n+1)));
     
     
                pane.add(surrenderB);
                pane.add(quitB);
                pane.add(shotsLabel);
                pane.add(shotsTF);
     
               // the char array is where the actual 1 square battleship is going to be placed.
               // it is a n by n grid and the other array needs to be too to make them parallel.  
               // A value in the arrayOfButtons[i][j] should be the same as charArray[x][y].
     
     
     
     
             } // end of if      
     
     
     
     
          // makes an n by n grid
     
     
             setSize(WIDTH, HEIGHT);
             setVisible(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     
          } // end of constructor
     
           public static void main(String[] args)
          { // beginning of main
             BattleshipGUI refVar = new BattleshipGUI();
     
     
     
          } // end of main
     
       	// this method handles the buttons.  
           public void actionPerformed(ActionEvent e)
          { // beginning of method
             String outputStr2, outputStr3, outputStr4, outputStr5, outputStr6;
             String outputStrM, outputStrA, outputStrAA;
             outputStrM = "Miss";
             outputStrA = "Already shot there";
             outputStrAA = "I told you that you already shot there";
     
             outputStr2 = "Are you sure you want to quit? ";
             outputStr3 = "You Quit.";
             outputStr4 = "Ok don't quit then." ;
             outputStr5 = "Thought not.";
             outputStr6 = "You surrendered.";
     
     
             int i, j, x,y;
             x = 0;
             i = 0;
             y = 0;
             j = 0;
     
             int shots =  Integer.parseInt(shotsTF.getText());
     
     
     
     
     
             placeBattleship(charArray); 
     
             Integer tries;
             Integer tries2;
             Integer tries3;
             Integer tries4;
             Integer tries5;
     
             tries = shots;
             tries2 = shots;
             tries3 = shots;
             tries4 = shots;
             tries5 = shots;
     
             if (e.getActionCommand().equals("Quit"))
             { // beginning of if
                Object[] options = {"Yes",
                        "Continue Playing"};
     
                int n = JOptionPane.showOptionDialog(frame,
                   "Are you sure you want to Quit?",
                   "Quit",
                   JOptionPane.YES_NO_OPTION,
                   JOptionPane.QUESTION_MESSAGE,
                   null,        options,     options[0]); 
             // creates Yes and Continue playing butons buttons.
             		// if Yee button is clicked, sends output "You quit." as heading and sends a message "Adiós!" and exits when 
             		// user clicks OK on dialog box
                if (n == JOptionPane.YES_OPTION) 
                { // beginning of if
                   JOptionPane.showMessageDialog(null,"Adiós!", outputStr3,  JOptionPane.PLAIN_MESSAGE);
                   System.exit(0);
                } // end of if
                // changes the string the standard dialog box for the No button but still uses its predefined return value.  If user clicks on 
                // Continue Playing, sends title "Ok don't quit then." and the message "Whatever." and takes the user back to the grid.
                else if (n == JOptionPane.NO_OPTION)
                { // beginning of else if
                   JOptionPane.showMessageDialog(null,"Whatever.",  outputStr4,  JOptionPane.PLAIN_MESSAGE);
                } // end of else if
     
             } // end of if
     
             else if (e.getActionCommand().equals("Surrender"))
             { // beginning of else if
     
                Object[] options2 = {"Yes",
                        "No, I have not yet begun to fight!"};
                int p = JOptionPane.showOptionDialog(frame,
                   "Are you sure you want to Surrender?",
                   "Surrender?",
                   JOptionPane.YES_NO_OPTION,
                   JOptionPane.QUESTION_MESSAGE,
                   null,        options2,   options2[0]); 
             // creates Yes and No, I have not yet begun to fight! butons .
             		// if Yes button is clicked, sends output "You surrendered." as heading and sends a message "It took you [number of shots]
             		// shots!" and exits when user clicks OK on dialog box
                if (p == JOptionPane.YES_OPTION) 
                { // beginning of if
                // code for showing grid
                   arrayOfButtons[i][j].setText("B");
                   JOptionPane.showMessageDialog(null, "You took "  + shotsTF.getText() + " shots" , outputStr6, JOptionPane.INFORMATION_MESSAGE);
                   System.exit(0);
                } // end of if
                // changes the string the standard dialog box for the No button but still uses its predefined return value.  If user clicks on 
                // "No button", sends output "Thought not." and the title "You didn't surrender." and takes the user back to the grid.
                else if (p == JOptionPane.NO_OPTION)
                { // beginning of else if
                   JOptionPane.showMessageDialog(null, outputStr5, "You didn't surrender." , JOptionPane.PLAIN_MESSAGE);
                } // end of else if
             } // end of else if
     
     
             else if (e.getActionCommand().equals("0"))
             { // beginning of else if
     
     
                if (  Fire(charArray, x, y) == 'H')
                { // beginning of if
                   String outputStr7;
                   arrayOfButtons[i][j].setText("H");  // changes text on button to H
                   shots = shots + 1;
                   tries = new Integer(shots);
                   shotsTF.setText(tries.toString());
     
                   outputStr7 = "You sank my battleship. " + "\n" + "You sank my battleship in" + shotsTF.getText() + "tries";
                   JOptionPane.showMessageDialog(null, outputStr7 , "You win!", +  JOptionPane.INFORMATION_MESSAGE);
                   System.exit(0);
                }// end of if
     
                else 
                { // beginning of else
                   JOptionPane.showMessageDialog(null, outputStrM , "You missed!", +  JOptionPane.INFORMATION_MESSAGE);
                   arrayOfButtons[i][j].setText("M"); // changes text on button to M
                   shots = shots + 1;
                   tries2 = new Integer(shots);
                   shotsTF.setText(tries2.toString());
     
                } // end of else
             } // end of else if 
                   // does this if text on button hit is M.
             else if (e.getActionCommand().equals("M"))
             { // beginning of else if
                shots = shots + 1;
                tries3 = new Integer(shots);
                shotsTF.setText(tries3.toString());
     
                JOptionPane.showMessageDialog(null, outputStrA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text on button to A
             } // end of else if
                   // does this if text on button hit is A
             else if (e.getActionCommand().equals("A"))
             { // beginning of else if
                shots = shots + 1;
                tries4 = new Integer(shots);
                shotsTF.setText(tries4.toString());
     
                JOptionPane.showMessageDialog(null, outputStrAA , "Still a miss!", +  JOptionPane.INFORMATION_MESSAGE);
                arrayOfButtons[i][j].setText("A"); // changes text, sorta, on button to A
             } // end of else if
     
          } // end of method
     
          // the method placeBattleship, places the battleship by randomly choosing a
       // row and column coordinate and storing a 'B' there.  The 'B' is not displayed to 
       // the user unless the user hits the surrender button.  It takes the array as a parameter and 
       // should return nothing.  This method sets the location of the battleship.
     
           private static void placeBattleship(char charArray[][])
          { // beginning of method placeBattleship
             int valueOne, valueTwo;
             char B;
             valueOne= (int) ( 1 + (Math.random() * charArray.length -1)); 
             valueTwo = (int) ( 1 + (Math.random() * charArray.length -1 ));
             charArray[valueOne][valueTwo] = 'B';
     
          } //end of method placeBattleship
     
           public static char Fire(char charArray[][], int x, int y)
          { // beginning of method Fire
             char A;
             char M;
             char H;
     
             if (charArray[x][y] == 'B')  // if battleship is hit return H and end game
             { // beginning of if statement
                charArray[x][y] = 'H';
     
     
                return ('H');
     
             } // end of if statement
     
             else if (charArray[x][y] == 'M')  // this is else if 
                                              // because it could also 
                                              // be just a miss.
     
             { // beginning of else if statement
     
                charArray[x][y] = 'A';
     
                return ('A');
             } // end of else if statement
     
             else if (charArray[x][y] == 'A') // makes sure it won't set an A to an M again.
             { // beginning of else if  
     
                charArray[x][y] = 'A';
                return ('A');
             } // end of else if
             else
     
             { // beginning of else statement
                charArray[x][y] = 'M'; 
                return ('M');                                          
             } // end of else statement
          } // end of method fire
     
     
       } // end of program


  25. #43
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Do I need to enter something like

    arrayOfButtons[i][j].updateUI();

    Or is that only if I told it told change icons as well as text based on certain clicks and conditions?

  26. #44
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Printing the button when clicked proved it's only looking at 0,0, though I can still hit the battleship and win. It's only changing the text of 0,0. It sets it to M if I miss, anywhere. If I click 0,0, next, it sets 0,0 to A and says already shot there. If I click 0,0 again, immediately afterward, it stays a and says "I already told you that you shot there." If I then click somewhere else that's not the battleship, it sets the text of 0,0 back to M.

    Why is it doing that?

  27. #45
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    It printed:

    javax.swing.JButton[,0,0,121x116,alignmentX=0.0,alignmentY=0.5,border= javax.swing.plaf.BorderUIResource$CompoundBorderUI Resource@47858e,flags=296,maximumSize=,minimumSize =,preferredSize=,defaultIcon=,disabledIcon=,disabl edSelectedIcon=,margin=javax.swing.plaf.InsetsUIRe source[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rol loverEnabled=true,rolloverIcon=,rolloverSelectedIc on=,selectedIcon=,text=M,defaultCapable=true]

  28. #46
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Neither type of error right now. Now just it's not doing exactly what I want it to type of error.

  29. #47
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    Why not think and compose your question(s) instead of this steady stream of posts?
    it's not doing exactly what I want it to
    ???
    sets the text of 0,0 back to M
    That's because your indexes are both = 0. Where does your code set the values of the indexes to other than 0?

  30. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

  31. #48
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Quote Originally Posted by Norm View Post
    Why not think and compose your question(s) instead of this steady stream of posts?

    ???

    That's because your indexes are both = 0. Where does your code set the values of the indexes to other than 0?
    arrayOfButtons[i][j].setText("H"); // changes text on button to H


    arrayOfButtons[i][j].setText("B");

    arrayOfButtons[i][j].setText("A"); // changes text on button to A

    arrayOfButtons[i][j].setText("A"); // changes text, sorta, on button to A

    The reason it starts out all "0" is because

    else if (e.getActionCommand().equals("0"))

    That should make it able to deal with any button that has a text of "0", which is all of them except, of course, for surrender and quit.

  32. #49
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Battleship GUI Program

    Also, how do you get something to be a parseString?

    like Double.parseDouble();

    or Integer.parseInt();

    What's the one for String?

  33. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Battleship GUI Program

    How does your program set the values of i and j that you use for indexes?

    What does: e.getActionCommand().equals("0") have to do with the problem?

    What is the value of e.getActionCommand()? Use println() to display it!!! Then you'll know what the program is doing.

    You need to learn how to debug your program. Your assuming that your thoughts are somehow making into your code. Sometimes it works differently than you think. To see what it is doing, use System.out.println() to show values.

  34. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 4th, 2010)

Page 2 of 4 FirstFirst 1234 LastLast