- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的代码:
import java.awt.*;
public class CafeWall {
public static void main(String[] args) {
DrawingPanel panel = new DrawingPanel(650, 400);
panel.setBackground(Color.GRAY);
Graphics g = panel.getGraphics();
// rows
row(g, 20, 4, 0, 0);
row(g, 30, 5, 50, 70);
// grids
grid(g, 25, 4, 10, 150, 0);
grid(g, 25, 3, 250, 200, 10);
grid(g, 20, 5, 425, 180, 10);
grid(g, 35, 2, 400, 20, 35);
}
// size is the pixel width/height of a square.
// multiples is the number of black/white pairs to draw.
// x,y are the screen position of the top left corner.
public static void row(Graphics g, int size, int multiples, int x, int y) {
for (int i = 0; i < multiples; i++) {
g.setColor(Color.BLACK);
g.fillRect(x + size * 2 * i, y, size, size);
g.setColor(Color.WHITE);
g.fillRect(x + size + size * 2 * i, y, size, size);
g.setColor(Color.BLUE);
g.drawLine(x + size * 2 * i, y, x + size + size * 2 * i, y + size);
g.drawLine(x + size + size * 2 * i, y, x + size * 2 * i, y + size);
}
}
// size is the pixel width/height of a square.
// multiples is the number of black/white pairs to draw.
// x,y are the screen position of the top left corner.
// offset is the amount to offset by.
public static void grid(Graphics g, int size, int multiples, int x, int y, int offset) {
for (int i = 0; i < multiples * 2; i++) {
row(g, size, multiples, x + (offset * i), y + (size * i) + (2 * i));
}
}
}
这就是我需要的样子。我觉得我已经尝试了一切。
最佳答案
@drifter265 我想回答,但我也希望你学习,因为这似乎是一个专为教学设计的入门级项目。
因此,我不是直接提供答案,而是通过解释当前程序当前正在执行的操作来向您展示错误所在。
// size is the pixel width/height of a square.
// multiples is the number of black/white pairs to draw.
// x,y are the screen position of the top left corner.
// offset is the amount to offset by.
public static void grid(Graphics g, int size, int multiples, int x, int y, int offset) {
for (int i = 0; i < multiples * 2; i++) {
row(g, size, multiples, x + (offset * i), y + (size * i) + (2 * i));
}
}
这里的代码相对简单。
它当前从 0 增量循环 1,以获得您想要绘制的黑白方 block 的总数。 (在倍数*2之前停止,从 0 开始是正确的)
每次循环时,都会调用 row。
大致相当于
row(g, size, 2, x + (offset * 0), y + (size * 0) + (2 * 0));
row(g, size, 2, x + (offset * 1), y + (size * 1) + (2 * 1));
row(g, size, 2, x + (offset * 2), y + (size * 2) + (2 * 2));
row(g, size, 2, x + (offset * 3), y + (size * 3) + (2 * 3));
(它创建的行数是黑色列的两倍)
您遇到的问题是您的偏移量总是在增长,而不是来回曲折。
where x = 0, and offset = 10
rowoffset = x + (offset * 0) = 0
rowoffset = x + (offset * 1) = 10
rowoffset = x + (offset * 2) = 20
rowoffset = x + (offset * 3) = 30
但是你想要的是
where x = 0, and offset = 10
rowoffset = 0; // where i == 0
rowoffset = 10 // where i == 1
rowoffset = 0 // where i == 2
rowoffset = 10 // where i == 3
实现分支行为的常见方法是使用 if 语句,这取决于要做出的决定。
因此,您可以不将 x+offset*i
传递给 row,而是在其中引入一个变量,该变量取决于 i 是奇数还是偶数。
计算整数是奇数还是偶数的常用方法是使用 remainder operator (%
) ,传入数字2。(但是在两边使用负值时必须小心)
0%2 == 0
1%2 == 1
2%2 == 0
3%2 == 1
~~~
8%2 == 0
9%2 == 1
因此,您现在可以使用数学或 if 语句来制作锯齿形图案。
关于java - 试图让我的代码看起来像咖啡馆的墙壁幻觉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808256/
#include #include #include using namespace std::chrono; int main(int arc, char* argv[]) { con
我是 opencv 的新手。当我将 sRGB png 提供给它时,我发现以下代码交换了红色和蓝色 channel 。我应该责怪哪个函数,imread 还是 fromarray? 最佳答案 是的,O
我正在使用以下代码,它缩小了每一行,并且由于某种原因整个用户界面也丢失了。我该如何解决?
它是关于以下设置的:Linux 机器、bash、adb、带有 Busybox 的嵌入式 Linux 目标系统。对于目标系统,以下适用: adb shell echo $SHELL /bin/sh ad
当我在 Android Studio 上创建一个空 fragment 时,它会生成以下代码: /** * A simple {@link Fragment} subclass. * Activit
我正在尝试从 Meteor 应用程序的服务器端发布用户的 Facebook 提要: result = Meteor.http.call 'POST', "https://graph.faceb
目前我有两个不同的查询,它们返回完全相同的结果,但是,更改从中过滤结果的参数会使它们以非常不同的方式运行。 搜索 cartography 时的结果 查询 #1: 22 行/~860 毫秒; SELEC
我已经创建了结构: typedef struct { short s; int i; struct Ss { short s; }; } S;
我想在 Java 中打印反斜杠 t。但每当我尝试时,它实际上都将它作为\t 运算符。双反斜杠不起作用。我该怎么做。 最佳答案 例如通过添加另一个反斜杠来转义反斜杠 System.out.println
我想弄清楚为什么 UIActivityViewController 发送一个稍微转换过的字符串来分享给邮件和微信。 这是我的代码: let activityViewController = UIAct
创建标准 SQLite 游标后,我将使用以下方法遍历条目: while (cursor.moveToNext()) { } 所有行都被正确处理。我读过的所有文档都表明您需要发出 moveToFirst
我正在尝试创建一个基本论坛,但在 SQL 中仅打印一行时遇到问题。这是我的 PHP: {$title}"; } } else { print "failed to reach post
我的新 div 元素 ( ) 似乎隐藏在图像后面。我键入的任何内容都显示在图像后面。我想在背景图片之后继续工作。 这是我的代码: DISPLAY
UPD。一行代码解决了问题:.lean() axplanation here 我在 Model.find(...blablabla : [ {"_id":"578763de6e8e0542195ef4
我在 Ubuntu 16.04 中安装了 Tomcat 8.0.45。我使用 let's encrypt 生成的证书启用了 HTTPS 连接器。它就像一个魅力。但是今天Tomcat无法启动HTTPS连
今天,我在一台全新安装了 Windows 7 Ultimate 64 位的新笔记本电脑上安装了 Visual Studio 2010 Professional。我非常习惯于 Visual Studio
根据 Oracle,我应该能够将 .intValue() 和 .compareTo() 之类的方法应用于 double ,但是当我编写 dbl.toString( ) 例如,在 NetBeans 中,
正在为应用程序开发一些拖放功能,虽然可以使用“重影图像”来完成很多事情,将毒品从一个地方转移到另一个地方,但它们看起来总是有点“褪色” - 因此得名鬼影。是否有可能使这些具有与原始颜色相同的深度?谢谢
我是这方面的新手,很抱歉犯了一些愚蠢的错误。快乐学习。每当我使用 URL : localhost:3000/posts/whatever 时,我都会收到无法获取错误。在你问之前,是的,那是在我将内容放
背景 这自然是合法的: let closure: (Int, Int) -> () = { print($0 + $1) } closure(1, 2) // 3 鉴于,自进化提案实现以来 SE-01
我是一名优秀的程序员,十分优秀!