我想绘制一个网格并在单元格中绘制东西(为了保持简单,只需填充它们).总的来说,我只是在一些面板尺寸上才能使用它,单元格距离它应该放置的位置大约1个像素(重叠线).TBH我还没有做足够的计算以便自己找到答案,所以我为此道歉,我真的不太确定如何处理这个"bug".
无论如何,这是代码:
public class Gui extends JFrame {
public static void main(String[] args) {
new Gui().setVisible(true);
}
public Gui() {
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
add(new JPanel() {
public static final int SIZE = 3;
/** Line thickness ratio to a block */
public static final float LINE_THICKNESS = 0.1f;
/** @return the width of a block. */
protected final int getBlockWidth() {
return getWidth() / SIZE;
}
/** @return the height of a block. */
protected final int getBlockHeight() {
return getHeight() / …Run Code Online (Sandbox Code Playgroud)