Java 为什么我的网格上的线的端点超过该网格的给定宽度和高度?

Java 为什么我的网格上的线的端点超过该网格的给定宽度和高度?,java,grid,draw,Java,Grid,Draw,我想画一个网格。比如说,x列的行是5x6。我的问题是,东部和南部的行尾超出了给定的列和行值。它不是很大,可能只有一毫米,但还是很烦人。我希望网格看起来像一个矩形,在上面的例子中,有20个单元格。我的代码中的错误在哪里?下面是绘制这个有缺陷网格的代码片段。谢谢 package Main; import java.awt.Dimension; import javax.swing.JFrame; public class GridMain { public static void m

我想画一个网格。比如说,x列的行是5x6。我的问题是,东部和南部的行尾超出了给定的列和行值。它不是很大,可能只有一毫米,但还是很烦人。我希望网格看起来像一个矩形,在上面的例子中,有20个单元格。我的代码中的错误在哪里?下面是绘制这个有缺陷网格的代码片段。谢谢

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);
        }
    }
}
packagemain;
导入java.awt.Dimension;
导入javax.swing.JFrame;
公共类GridMain{
公共静态void main(字符串[]args){
JFrame JFrame=新JFrame();
jframe.setPreferredSize(新尺寸(640730));
jframe.setDefaultCloseOperation(jframe.EXIT_ON_CLOSE);
Mypanel Mypanel=新的Mypanel(5,6);
jframe.add(mypanel);
jframe.pack();
jframe.setVisible(true);
}
}
主包装;
导入java.awt.Graphics;
导入javax.swing.JPanel;
公共类Mypanel扩展了JPanel{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
私人内部高度;
私有整数宽度;
点与点之间的专用间隙=20;
FrameAndGrid之间的私有int GAP=15;
公共Mypanel(int列、int行){
宽度=列;
高度=行数;
}
受保护组件(图形g){
对于(int i=0;i
小心你的出发点;我将不触及宽度(高度),而仅触及宽度-1/高度-1。因此,以下代码:

    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);
    }
for(int i=0;i
大体上

    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);
    }
for(int i=0;i
有趣的是,当我从第一个for循环的(width*gapBetweenPoints)和第二个for循环的(height*gapBetweenPoints)中减去5时,我得到了我想要的结果。。但这显然不是我们做事的方式:-)太多了!这就解决了我的问题。澄清一下:在第二段代码中,在第一个循环中,为什么不将startingX添加到(i*gapBetweenPoints),而不是添加gapBetweenFrameAndGrid?我遗漏了什么?第二个循环也是如此