2

I am referencing this link, however this won't ultimately fix my problem (i.e I run my program from someone else's computer). How to deal with "java.lang.OutOfMemoryError: Java heap space" error (64MB heap size). Right now, I have a game board that has 10x10 squares, but I need to increase this to 100x100, but when I do, I get this error. What is the best way to increase my game board size while avoiding this error? Current output is below, Code should compile and run. Thanks!

enter image description here

GameBoard Class:

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;

public class GameBoard {

private final JPanel board = new JPanel(new BorderLayout(3, 3));
private JButton[][] c1squares = new JButton[10][10];
private JPanel c1Board, c2Board;
private final JLabel messagec1 = new JLabel("Player 1 Board");
JToolBar tool = new JToolBar();
Insets Margin = new Insets(0,0,0,0);
int squares = 10;
int space = 100;
ImageIcon icon = new ImageIcon(new BufferedImage(space, space, BufferedImage.TYPE_INT_ARGB));

GameBoard() {
    initializeGui();
}

public final void initializeGui() {

    board.setBorder(new EmptyBorder(5, 5, 5, 5));
    tool.setFloatable(false);
    board.add(tool, BorderLayout.PAGE_START);
    tool.add(messagec1);
    c1Board = new JPanel(new GridLayout(0, 10));
    c1Board.setBorder(new LineBorder(Color.BLACK));
    board.add(c1Board);

    for (int i = 1; i < c1squares.length; i++) {
        for (int j = 0; j < c1squares[i].length; j++) {
            JButton b = new JButton();
            b.setMargin(Margin);              
            b.setIcon(icon);
            if ((j % 2 == 1 && i % 2 == 1) || (j % 2 == 0 && i % 2 == 0)) {
                b.setBackground(Color.WHITE);
            } else {
                b.setBackground(Color.BLACK);
            }
            c1squares[j][i] = b;
        }
    } 
    for (int i = 1; i < squares; i++) {
        for (int j = 0; j < squares; j++) {     
                    c1Board.add(c1squares[j][i]);      
        }
    } 
public final JComponent getGui() {
    return board;
}
public final JComponent getGui2() {
    return board2;
}
}

BattleShipFinal Class:

import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JFrame;

public class BattleshipFinal {

public static void main(String[] args) {

    GameBoard gb = new GameBoard();
    JFrame frame = new JFrame("Battleship - Server");
    frame.add(gb.getGui());
    frame.setLocationByPlatform(true);
    frame.setMinimumSize(frame.getSize());
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setPreferredSize(new Dimension(900,900));
    frame.setMinimumSize(new Dimension(900,900));
    frame.setLocation(50,50);
    frame.pack();
    frame.setVisible(true);

}

}
Community
  • 1
  • 1
Millie
  • 73
  • 1
  • 2
  • 7
  • You need to increase you heap size http://stackoverflow.com/questions/6452765/how-to-increase-heap-size-of-jvm – barna10 Nov 22 '16 at 16:22
  • So you wish to have a frame with 10,000 buttons on it ? (100x100) ? I would suggest not using buttons at all, but have a drawn area with 'virtual' buttons. You can determine which 'button' (square) the player is clicking by using the mouse co-ordinates. – jr593 Nov 22 '16 at 16:23
  • ...or restructure the approach as jr593 suggested – barna10 Nov 22 '16 at 16:23
  • I think jr593's approach will be better, because it will take care of my issue directly within the code, instead of having to go into the cmd. Can you please give an example of how to do this/what it would look like? – Millie Nov 22 '16 at 16:26

2 Answers2

1

In case you are curious, and to illustrate what people said in the comments, you could have a custom JPanel painting the squares.

If you ever need to respond to mouse events, add a MouseListener to your panel, which will take care of the selected square (haven't added that part, but the selected field inside the Square class is a hint).

I removed stuff from your code, just to demonstrate this painting part.

GameBoard :

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class GameBoard {

    private JPanel board;
    private final Square[][] c1squares = new Square[10][10];

    GameBoard() {
        initializeGui();
    }

    public final void initializeGui() {

        for (int i = 0; i < c1squares.length; i++) {
            for (int j = 0; j < c1squares[i].length; j++) {

                Square square = new Square();

                if ((j % 2 == 1 && i % 2 == 1) || (j % 2 == 0 && i % 2 == 0)) {
                    square.setBackground(Color.WHITE);
                } else {
                    square.setBackground(Color.BLACK);
                }

                c1squares[i][j] = square;
            }
        }

        board = new BoardPanel(c1squares);
        board.setBorder(new EmptyBorder(5, 5, 5, 5));

    }

    public final JComponent getGui() {
        return board;
    }

    private class BoardPanel extends JPanel {

        Square[][] squares;

        public BoardPanel(final Square[][] squares) {

            this.squares = squares;

        }

        @Override
        public void paintComponent(final Graphics g) {

            super.paintComponent(g);

            int width = getWidth();
            int height = getHeight();

            for (int i = 0; i < squares.length; i++) {
                for (int j = 0; j < squares[i].length; j++) {

                    Square currentSquare = squares[i][j];

                    System.out.println("Managing square " + i + "  " + j);

                    g.setColor(currentSquare.getBackground());
                    g.fillRect(i * width / squares.length, j * height / squares.length, width / squares.length,
                            height / squares.length);

                }
            }

        }

    }

    private class Square {

        boolean isSelected;
        Color background;

        public boolean isSelected() {
            return isSelected;
        }

        public void setSelected(final boolean isSelected) {
            this.isSelected = isSelected;
        }

        public Color getBackground() {
            return background;
        }

        public void setBackground(final Color background) {
            this.background = background;
        }

    }

}

BattleshipFinal :

import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class BattleshipFinal {

    public static void main(final String[] args) {

        GameBoard gb = new GameBoard();
        JFrame frame = new JFrame("Battleship - Server");
        JComponent board = gb.getGui();
        frame.add(board);
        frame.setLocationByPlatform(true);
        //frame.setMinimumSize(frame.getSize());
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        //frame.setPreferredSize(new Dimension(100, 100));
        board.setMinimumSize(new Dimension(100, 100));
        board.setPreferredSize(new Dimension(100, 100));
        frame.setMinimumSize(new Dimension(100, 100));
        frame.setLocation(50, 50);
        frame.pack();
        frame.setVisible(true);

    }

}
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

Okay, I figured out the solution for this. Here is the output:

enter image description here

The way I fixed this was by changing these lines of code:

private JButton[][] c1squares = new JButton[100][100];
int squares = 100;
int space = 1000;
c1Board = new JPanel(new GridLayout(0, 100));
Millie
  • 73
  • 1
  • 2
  • 7