melar
melar

Reputation: 212

Why do the ends of lines on my grid exceed the given width and height of that grid?

I'm trying to draw a grid. Column x row is, say, 5x6. My problem is that the line endings in the east and in the south exceed the given column and row values. It's not big, just a milimeter maybe, but still annoying. i want the grid to look like a rectangle with, in above case, 20 cells. Where is the bug in my code? The code snippet that draws this flawed grid is below . thx!

package Main;

import java.awt.Dimension;

import javax.swing.JFrame;

public class GridMain {

    public static void main(String[] args) {
        JFrame jframe = new JFrame();
        jframe.setPreferredSize(new Dimension(640, 730));
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Mypanel mypanel = new Mypanel(5,6);
        jframe.add(mypanel);
        jframe.pack();
        jframe.setVisible(true);
    }
}
package Main;

import java.awt.Graphics;

import javax.swing.JPanel;

public class Mypanel extends JPanel {
    /**
 * 
 */
    private static final long serialVersionUID = 1L;
    private int height;
    private int width;
    private int gapBetweenPoints = 20;
    private int gapBetweenFrameAndGrid=15;
    public Mypanel(int columns, int rows) {
        width = columns;
        height = rows;
    }

    protected void paintComponent(Graphics g) {
        for (int i =0 ; i < height; i++) {
            g.drawLine(gapBetweenFrameAndGrid, i * gapBetweenPoints
                    + gapBetweenFrameAndGrid, width * gapBetweenPoints, i
                    * gapBetweenPoints + gapBetweenFrameAndGrid);
        }
        for (int i = 0; i < width; i++) {
            g.drawLine(i * gapBetweenPoints + gapBetweenFrameAndGrid,
                    gapBetweenFrameAndGrid, i * gapBetweenPoints +  gapBetweenFrameAndGrid,
                    height * gapBetweenPoints);
        }
    }
}

Upvotes: 1

Views: 57

Answers (1)

gpasch
gpasch

Reputation: 2682

Be careful where you get your starting point; and i will not reach to width(height) but only width-1/height-1. So the following code:

    for (int i =0 ; i < height; i++) {
        g.drawLine(gapBetweenFrameAndGrid, i * gapBetweenPoints
                + gapBetweenFrameAndGrid, gapBetweenFrameAndGrid+(width-1) * gapBetweenPoints, i
                * gapBetweenPoints + gapBetweenFrameAndGrid);
    }
    for (int i = 0; i < width; i++) {
        g.drawLine(i * gapBetweenPoints + gapBetweenFrameAndGrid,
                gapBetweenFrameAndGrid, i * gapBetweenPoints +  gapBetweenFrameAndGrid,
                gapBetweenFrameAndGrid+(height-1) * gapBetweenPoints);
    }

In general

    for (int i =0 ; i < height; i++) {
        g.drawLine(startingX, i * gapBetweenPoints
                + gapBetweenFrameAndGrid, startingX+(width-1) * gapBetweenPoints, i
                * gapBetweenPoints + gapBetweenFrameAndGrid);
    }
    for (int i = 0; i < width; i++) {
        g.drawLine(i * gapBetweenPoints + gapBetweenFrameAndGrid,
                startingY, i * gapBetweenPoints +  gapBetweenFrameAndGrid,
                startingY+(height-1) * gapBetweenPoints);
    }

Upvotes: 2

Related Questions