0

When a Grid Object is instantiated, a JFrame and JPanel are created. Lines are drawn upon the JPanel to create a square grid. Ideally, the grid will scale if the window is resized.

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.ComponentListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentAdapter;

public class Grid {
    private int lines;
    private int space;
    public static final int DEFAULT_LINES = 5;
    public static final int DEFAULT_SPACE = 5;

private JPanel panel = new GridPanel();
private JFrame frame;

public Grid(String name, int lines, int space) {
    this.frame = new JFrame(name);
    this.lines = lines;
    this.space = space;
    this.frame.setSize((lines * space), (lines * space));
}

private class GridPanel extends JPanel {
    private int start = 0;

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

        int end = (lines * space);
        g.setColor(Color.BLACK);
        for (int i = 0; i < lines; i++) {
            int crawl = (space * i);
            g.drawLine(start, crawl, end, crawl);
            g.drawLine(crawl, start, crawl, end);
        }
    }
}

/*private class GridHandler extends ComponentAdapter {

    @Override
    public void componentResized(ComponentEvent e) {
        super.componentResized(e);
        setSpace();
        frame.repaint();
        frame.revalidate();
    }
}*/

public void setSpace() {
    space = (frame.getSize().width * frame.getSize().height) / lines;
}

public void run() {
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.add(panel);
    //frame.addComponentListener(new GridHandler());
    frame.setVisible(true);
}
}


public class Run {
    public static final int ONE_LINES = 15;
    public static final int ONE_SPACE = 20;

    public static final int TWO_LINES = 35;
    public static final int TWO_SPACE = 16;

    public static void main(String[] args) {
        Grid grid1 = new Grid("Grid One", ONE_LINES, ONE_SPACE);
        Grid grid2 = new Grid("Grid Two", TWO_LINES, TWO_SPACE);
        grid1.run();
        grid2.run();
    }
}

These are the only two files being used. The Handler is currently commented out, and the code does what is expected. Two windows with grids are created. However, when the handler is implemented, the grid no longer shows up. What is the correct implementation for the handler?

  • 1
    Why not just use the component's `width` and `height` and calculate the grid size when ever `paintComponent` is called? Something like [this for example](http://stackoverflow.com/questions/34036216/drawing-java-grid-using-swing/34036397#34036397)? – MadProgrammer Apr 09 '17 at 05:43
  • Thank you for the advice! I have posted my solution below (it uses `width` and `height` as you suggested) I find the code to be more readable this way. Thanks again! – G. Martinek Apr 09 '17 at 20:46

1 Answers1

0

For anyone that cares, this is a viable solution. As it turns out, a componentResized Event Handler is not needed. The paintComponent is automatically executed when the JFrame is re-sized.

The following code creates two separate JFrames. Each JFrame contains a single JPanel. A unique square grid is drawn on the JPanel. If the JFrame is re-sized, the grid will also re-size (while still remaining square).

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class Grid extends JPanel {
    private static final int DEFAULT_COUNT = 10;
    private static final int DEFAULT_SIZE = 100;

    private int count;

    public Grid(int count, int size) {
        if (size < 1) {
            this.count = DEFAULT_COUNT;
            this.setPreferredSize(new Dimension(DEFAULT_SIZE, DEFAULT_SIZE));
        } else {
            this.count = count;
            this.setPreferredSize(new Dimension(size, size));
        }
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphics = (Graphics2D) g;
        graphics.setColor(Color.black);

        Dimension size = getSize();
        int w = size.width;
        int h = size.height;

        if (w > h) {
            w = h;
        } else if (h > w) {
            h = w;
        }

        int spaceWidth = (int)((double)w / count);
        int spaceHeight = (int)((double)h / count);
        for (int row = 0; row <= count; row++) {
            int y = (row * spaceHeight);
            int x = (row * spaceWidth);
            graphics.drawLine(0, y, (spaceWidth * count), y);
            graphics.drawLine(x, 0, x, (spaceHeight * count));
        }
    }
}

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.*;

public class Test {
    public static final int COUNT_1 = (-5);
    public static final int SIZE_1 = 4;

    public static final int COUNT_2 = 35;
    public static final int SIZE_2 = 16;

    public static void main(String[] args) {
        int area1 = COUNT_1 * SIZE_1;
        int area2 = COUNT_2 * SIZE_2;

        Grid grid = new Grid(COUNT_1, area1);
        JFrame frame = new JFrame("Grid");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(grid);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Grid grid2 = new Grid(COUNT_2, area2);
        JFrame frame2 = new JFrame("Grid");
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.add(grid2);
        frame2.pack();
        frame2.setLocationRelativeTo(null);
        frame2.setVisible(true);
    }
}