Q:

Java: Measurement in paint

I'm still fairly new to java and I've been having trouble with the paint method.
when I learning how to use bufferedimages, I thought that the image sizes were a bit off relative to the frame size so I tested it out by changing the frame size to make the width & height multiples of 30:
int width = 1020;   //34 * 30
int height = 750;   //25 * 30
JFrame frame = new JFrame("Test");
GridT testGrid = new GridT();
frame.add(testGrid);
frame.setSize(width, height); 

Next I had the paint method draw a grid of 30 by 30 squares and, as I suspected, the grid was cut off at the edges:

(Note: The lines are all the same color and tickness but may appear differnt when the screenshot is resized.)
Does paint use a different unit of measurent than the JFrame? If so, how much is it off by? If not, what could I be doing wrong?

A:

You paint on a JPanel, overriding the paintComponent method. You set the preferred size of the JPanel to be the size of your drawing area. You pack the JFrame. The JFrame will be bigger than your drawing area because of the frame decorations.
You do have to add 2 pixels to the height and the width of the drawing area to see the grid.  You start at 1,1; rather than 0,0.
Here's a smaller version of your grid.

And here's the runnable code that creates the grid.
package com.ggl.testing;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GridTest implements Runnable {

    private JFrame frame;

    @Override
    public void run() {
        frame = new JFrame("Grid Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout());

        DrawingPanel drawingPanel = new DrawingPanel();
        mainPanel.add(drawingPanel);

        frame.add(mainPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GridTest());
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = -5711127036945010446L;

        private int width = 750; // 25 * 30
        private int height = 600; // 20 * 30

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(width + 2, height + 2));
        }

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

            int x = 1;
            int y = 1;
            int size = 30;

            for (int i = 0; i < 25; i++) {
                for (int j = 0; j < 20; j++) {
                    g.drawRect(x, y, size, size);
                    y += size;
                }
                x += size;
                y = 1;
            }
        }
    }

}

