为什么我的网格上的线的末端超过了该网格的给定宽度和高度?
Why do the ends of lines on my grid exceed the given width and height of that grid?
我正在尝试绘制网格。例如,列 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);
}
}
}
小心你的起点;我不会达到宽度(高度),但只会达到 width-1/height-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 < 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);
}
我正在尝试绘制网格。例如,列 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);
}
}
}
小心你的起点;我不会达到宽度(高度),但只会达到 width-1/height-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 < 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);
}