/*
 * Copyright (c) 2015. Shen Yichen <2007.yichen@gmail.com>
 * Under The MIT License.
 */
package battleships.gui;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;

/**
 * A GUI panel for the game board display.
 *
 * @author Shen Yichen <2007.yichen@gmail.com>
 */
public class HighSeas extends JPanel {

    static final double MAX_SQUARE_SIZE = 60;

    private int xSqrs = 0;
    private int ySqrs = 0;
    private double sqrSize = MAX_SQUARE_SIZE;

    public HighSeas() {

    }

    public void setSquares(int x, int y) {
        if (x < 1 || y < 0) {
            throw new IllegalArgumentException("There must be as least 1 row/column!");
        }

        xSqrs = x;
        ySqrs = y;

        refresh();
    }

    public void refresh() {
        sqrSize = Math.min(MAX_SQUARE_SIZE, Math.min((getWidth() - 1) * 1.0 / (xSqrs + 1), (getHeight() - 1) * 1.0 / (ySqrs + 1)));

        repaint();
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        Graphics2D g2 = (Graphics2D) g;

        if (xSqrs > 0 && ySqrs > 0) {
            for (int x = 1; x <= xSqrs + 1; x++) {
                Line2D.Double xGrid = new Line2D.Double(x * sqrSize, sqrSize, x * sqrSize, (ySqrs + 1) * sqrSize);
                g2.draw(xGrid);
            }

            for (int y = 1; y <= ySqrs + 1; y++) {
                Line2D.Double xGrid = new Line2D.Double(sqrSize, y * sqrSize, (xSqrs + 1) * sqrSize, y * sqrSize);
                g2.draw(xGrid);
            }
        }

    }

}

@@ -190,24 +190,48 @@
 eom.Line2D;%0A
+import java.util.Stack;%0A
 import javax
@@ -1120,300 +1120,1551 @@
    i
-f (xSqrs %3E 0 && ySqrs %3E 0) %7B%0A            for (int x = 1; x %3C= xSqrs + 1; x++) %7B%0A                Line2D.Double xGrid = new Line2D.Double(x * sqrSize, sqrSize, x * sqrSize, (ySqrs + 1) * sqrSize);%0A                g2.draw(xGrid);%0A            %7D%0A%0A            for (int y = 1; y %3C= ySqrs + 1; y++) %7B
+nt maxDigits = (int) Math.max(Math.floor(Math.log10(xSqrs)) + 1, Math.floor(Math.log(ySqrs - 1) / Math.log(26) + 1));%0A%0A        double maxBound = g2.getFontMetrics(g2.getFont()).getHeight();%0A%0A        for (int w : g2.getFontMetrics().getWidths()) %7B%0A            maxBound = Math.max(maxBound, w);%0A        %7D%0A%0A        double fontSize = sqrSize / (maxBound * maxDigits) * g2.getFont().getSize();%0A        g2.setFont(g2.getFont().deriveFont((float) fontSize));%0A%0A        if (xSqrs %3E 0 && ySqrs %3E 0) %7B%0A%0A            double fontY = g2.getFontMetrics().getMaxDescent() + g2.getFontMetrics().getLeading();%0A            double fontX;%0A%0A            for (int x = 1; x %3C= xSqrs + 1; x++) %7B%0A                if (x %3C= xSqrs) %7B%0A                    fontX = g2.getFontMetrics().getStringBounds(Integer.toString(x), g2).getWidth();%0A%0A                    g2.drawString(Integer.toString(x), (float) (x * sqrSize + (sqrSize - fontX) / 2), (float) (sqrSize - fontY));%0A                %7D%0A                Line2D.Double xGrid = new Line2D.Double(x * sqrSize, sqrSize, x * sqrSize, (ySqrs + 1) * sqrSize);%0A                g2.draw(xGrid);%0A            %7D%0A%0A            double fontYHeight = g2.getFontMetrics().getHeight();%0A%0A            for (int y = 1; y %3C= ySqrs + 1; y++) %7B%0A                if (y %3C= ySqrs) %7B%0A                    fontX = g2.getFontMetrics().getStringBounds(Integer.toString(y), g2).getWidth();%0A%0A                    g2.drawString(Integer.toString(y), (float) ((sqrSize - fontX) / 2), (float) (sqrSize * (y + 1) - (sqrSize - fontYHeight) / 2 - fontY));%0A                %7D%0A
 %0A   
@@ -2831,18 +2831,16 @@
       %7D%0A
-%0A
     %7D%0A
-%0A
 %7D%0A

Add board numbers