如何解决棋盘格子不移动到中间的问题

目前我尝试制作一个固定尺寸的棋盘。我成功地将棋盘绘制在角落(坐标0,0,400,400)。但是,现在我希望如果我移动窗口,棋盘也将始终位于中间。棋盘的背景总是移动的,并且总是在中间,但方块是固定的。为了让您了解我的问题,我上传了一些图片,您可以在其中看到我的主要问题。

第一次开始:

https://img1.sycdn.imooc.com/65425bdc0001a4e506450515.jpg

将窗口移至左侧

https://img1.sycdn.imooc.com/65425be8000161a504680512.jpg

背景在中间,但没有白色方块

https://img1.sycdn.imooc.com/65425bf60001c4dc06510344.jpg

拉丁的传说
浏览 133回答 2
2回答

神不在的星期二

这是绘制正方形的“正确”方法。for (int i = width; i < 400 + width; i += 100) {&nbsp; &nbsp; for (int j = height; j < 400 + height; j += 100) {&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.white);&nbsp; &nbsp; &nbsp; &nbsp; g.fillRect(i, j, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.black);&nbsp; &nbsp; &nbsp; &nbsp; g.drawRect(i, j, 50, 50);&nbsp; &nbsp; }}for (int i = width + 50; i < 400 + width; i += 100) {&nbsp; &nbsp; for (int j = height + 50; j < 400 + height; j += 100) {&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.white);&nbsp; &nbsp; &nbsp; &nbsp; g.fillRect(i, j, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.black);&nbsp; &nbsp; &nbsp; &nbsp; g.drawRect(i, j, 50, 50);&nbsp; &nbsp; }}

米脂

您可以使用较小两倍的代码来实现它:for (int i = width; i < 400 + width; i += 100) {&nbsp; &nbsp;for (int j = height; j < 400 + height; j += 100) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.white);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fillRect(i, j, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fillRect(i+50, j+50, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setColor(Color.black);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawRect(i, j, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawRect(i+50, j+50, 50, 50);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java