0

What I am doing here is in one class reading in a file where the first line contains info about the number of rows and columns in a grid to be made in a GUI representation. I made the first class read in the file, and returns a value "e" that I'm trying to pass into the GUI class to make the grid. I'm stuck on this as I've tried to make one class a super, and still can't figure it out.

/**
 * @author unrealj
 * 
 * Battleship.java reads in a file defined by the text file 
 * "textfile.txt" and should return a value for the number
 * of rows and columns of the grid, and eventually an arraylist
 * of the ships, designated by the second and or more lines.
 */
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.LineNumberReader;


public abstract class BattleShip extends Board {
	private int e;
	private int se;
	
	public BattleShip(int e){
		super(e);
		this.e = e;
		
		//Constructors
	}
	public double getE(){
		//Allows the interest to be used by SuperClass
		return e;
	}
		
	
		   public static void main(String args[])
		  {
		      try{
		    // Open the file that is the first 
		    // command line parameter
		    FileInputStream fstream = new FileInputStream("textfile.txt");
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    FileReader fr = new FileReader("textfile.txt");
		    LineNumberReader ln = new LineNumberReader(fr);


		    while (ln.getLineNumber() == 0){


		    String s = ln.readLine();
		    int e = Integer.parseInt(s);
		    System.out.println(e);
		    in.close();
		    }

		   

		   while ((strLine = br.readLine()) != null)   {
		      // Print the content on the console
		      System.out.println (strLine);
		    }
		    //Close the input stream
		    
		    
		     
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		    }
		  }
		}

Here is Board.java

/**
 * @author unrealj
 * Board.java takes in a value from BS.java and
 * creates a GUI grid from it, and creates buttons based on e.
 * The cheat button will show where items in the arraylist are,
 * while the reset button will bring all buttons back to state
 * before the cheat button was placed.
 **/


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import javax.swing.*;


public abstract class Board extends JFrame {
    static ArrayList row = new ArrayList();
    static ArrayList column = new ArrayList();
   
    
	

	public Board(int e) {
    	
    	
        // Set the border layout for the frame
        setLayout(new BorderLayout());

     // Create panel1 for the button and
        // use a grid layout
        JPanel panel1 = new JPanel();
       
       
        //panel1.setOpaque(true);
        panel1.setLayout(new GridLayout(e,e));
        
     // create ButtonListener
        ButtonListener listener = new ButtonListener();
        // Add buttons to the panel
        for (int i=1; i<(e*e); i++) {
        	
        	
        	
        		JButton button = new JButton("" + "X" );
        		button.setBackground(Color.WHITE);
                button.setForeground(Color.BLACK);  
               
                panel1.add(button);
        		
        	}
       
        
        
    

        // Create panel2 to hold a text field and panel1
        JPanel panel2 = new JPanel(new BorderLayout());
        panel2.setLayout(new GridLayout(1,2));
        
       
        
        
       
    
        
        
        
        JButton cheat = new JButton("CHEAT");
        cheat.addActionListener(listener);
        cheat.setForeground(Color.BLACK);
       
        
        
        
        JButton resume = new JButton("RESUME");
        resume.setForeground(Color.BLACK);
       
        resume.addActionListener(listener);
        
        
        
        panel2.add(cheat);
        panel2.add(resume);
        
        add(panel2, BorderLayout.SOUTH);

        
        
       

        // Add panel2 and a button to the frame
        add(panel1, BorderLayout.CENTER);
        

        // set frame appearance
        setTitle("Battle them Ships!");
        setSize(400, 250);
        setLocation(100, 100);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
        
        
        
    } // Board

    class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent r) {
        	if (((Component) r.getSource()).getBackground()==Color.WHITE){
        		((Component) r.getSource()).setBackground(Color.BLACK);	
        		
        	}
        	else if(((Component) r.getSource()).getBackground()==Color.BLACK){
        		((Component) r.getSource()).setBackground(Color.WHITE);
        	}
           
        	
 
    }
    
    }

For tests sake, textfile.txt is

6
1 2 3 2

Thank you for your help.

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by unrealj
0

Here's an overview of the norml way to structure this kind of code. You already have most of the bits, but in the wrong order!

Make BattleShip a non-abstract class so you can create an instance of it. Don't extend anything, just let it be what it is - the data for a BattleShips game.
Move all the code from main into the constructor for BatteleShip.
Replace what's currently in main with a simple
BattleShip bs = new BattleShip();
Now you have a fully-initialised instance of BattleShip.

In the Board class have an instance variable that references an instance of BattleShip - so methods in the Board class can call methods in the BattleShip game class to query all the values. Pass that instance as a parameter to Board's constructor (instead of int e).

Finally add a second line to main so it now reads:

BattleShip bs = new BattleShip(); // create and initialise a game
new Board(bs); // create a GUI using that game

That's a few steps, but all easy.

0

Thanks! I got it, I tried to implement the Super, then realized that I never initialized the Board in the main class, and then deleted all the abstract classes.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.