在 Java 中使用 Turtle 为形状填充颜色
Color filling a shape using Turtle in Java
我一直在寻找如何使用 java 中的 Turtle 以某种颜色填充形状。
假设我有这样的事情:
private static void test(Turtle t1) {
for (int count = 0; count < 4; count++) {
t1.fd(100);
t1.right(90);
}
}
其中 t1
是:Turtle t1 = new Turtle();
上面的代码创建了一个简单的矩形,而我想弄清楚的是如何用颜色 (red, green, blue) 填充该矩形。我已经查看了这个 link 中的文档,但到目前为止我还没有弄明白。
谢谢。
我见过更多有用的方法,例如 begin_fill
然后是 "do something" 和 end_fill
用于 Turtle Graphics[= Python 中的 53=] 并且 Java 的实现似乎有所不同,但我相信我实现了你所需要的(但我不确定这是否是最好的方法) 但我同意你的看法,我找不到关于此的信息以及 link 中描述的方法您提供的与 Java 代码中的 Turtle 对象中的不同。为什么?因为你的 Turtle 对象正在使用 fd
方法向前移动,而提供的文档说有一个 forward
方法可以这样做 (make确保您使用的是正确的库,在我的例子中,我使用的是 jturtle-0.1.1.jar
并且这些方法与 OP 中的代码片段是准确的).
Java代码:
package com.turtle.main;
import java.awt.Color;
import ch.aplu.turtle.Turtle;
public class TurtleMain {
public static void main(String[] args) {
Turtle turtle = new Turtle();
// Init config
turtle.setColor(Color.RED);
turtle.setPenColor(Color.BLUE);
turtle.setFillColor(Color.GREEN);
// Draw rectangle
for (int count = 0; count < 4; count++) {
turtle.fd(100);
turtle.right(90);
}
// Fill rectangle
turtle.fill(1, 1);
}
}
所以,当使用 fill
方法时 (这就是诀窍) 你会看到一个没有参数,另一个需要 X 和 Y 坐标。
从海龟那里获得 class:
/** Fills the region the Turtle is in.
A Region is bounded by lines
of any other color than the background color and by the border of
the Window. <br>
@return the turtle to allow chaining.
*/
public Turtle fill(){
getPlayground().fill(this);
return this;
}
/** Fills the region with coordinates <code>x</code> and <code>y</code>.
A Region is bounded by lines
of any other color than the background color and by the border of
the Window. <br>
@return the turtle to allow chaining.
*/
public Turtle fill(double x, double y){
double oldX = getX();
double oldY = getY();
boolean hidden = isHidden();
ht().setPos(x,y);
getPlayground().fill(this);
setPos(oldX, oldY);
if(!hidden){
st();
}
return this;
}
然而,当我只调用 fill()
时它不起作用,但是当在 fill(x, y)
方法中指定 X=1
和 Y=1
时,它起作用 (调试核心方法以查看发生了什么会很好吗?) 无论如何,我没有这样做但是当使用上面的代码时,这是输出:
我一直在寻找如何使用 java 中的 Turtle 以某种颜色填充形状。 假设我有这样的事情:
private static void test(Turtle t1) {
for (int count = 0; count < 4; count++) {
t1.fd(100);
t1.right(90);
}
}
其中 t1
是:Turtle t1 = new Turtle();
上面的代码创建了一个简单的矩形,而我想弄清楚的是如何用颜色 (red, green, blue) 填充该矩形。我已经查看了这个 link 中的文档,但到目前为止我还没有弄明白。
谢谢。
我见过更多有用的方法,例如 begin_fill
然后是 "do something" 和 end_fill
用于 Turtle Graphics[= Python 中的 53=] 并且 Java 的实现似乎有所不同,但我相信我实现了你所需要的(但我不确定这是否是最好的方法) 但我同意你的看法,我找不到关于此的信息以及 link 中描述的方法您提供的与 Java 代码中的 Turtle 对象中的不同。为什么?因为你的 Turtle 对象正在使用 fd
方法向前移动,而提供的文档说有一个 forward
方法可以这样做 (make确保您使用的是正确的库,在我的例子中,我使用的是 jturtle-0.1.1.jar
并且这些方法与 OP 中的代码片段是准确的).
Java代码:
package com.turtle.main;
import java.awt.Color;
import ch.aplu.turtle.Turtle;
public class TurtleMain {
public static void main(String[] args) {
Turtle turtle = new Turtle();
// Init config
turtle.setColor(Color.RED);
turtle.setPenColor(Color.BLUE);
turtle.setFillColor(Color.GREEN);
// Draw rectangle
for (int count = 0; count < 4; count++) {
turtle.fd(100);
turtle.right(90);
}
// Fill rectangle
turtle.fill(1, 1);
}
}
所以,当使用 fill
方法时 (这就是诀窍) 你会看到一个没有参数,另一个需要 X 和 Y 坐标。
从海龟那里获得 class:
/** Fills the region the Turtle is in.
A Region is bounded by lines
of any other color than the background color and by the border of
the Window. <br>
@return the turtle to allow chaining.
*/
public Turtle fill(){
getPlayground().fill(this);
return this;
}
/** Fills the region with coordinates <code>x</code> and <code>y</code>.
A Region is bounded by lines
of any other color than the background color and by the border of
the Window. <br>
@return the turtle to allow chaining.
*/
public Turtle fill(double x, double y){
double oldX = getX();
double oldY = getY();
boolean hidden = isHidden();
ht().setPos(x,y);
getPlayground().fill(this);
setPos(oldX, oldY);
if(!hidden){
st();
}
return this;
}
然而,当我只调用 fill()
时它不起作用,但是当在 fill(x, y)
方法中指定 X=1
和 Y=1
时,它起作用 (调试核心方法以查看发生了什么会很好吗?) 无论如何,我没有这样做但是当使用上面的代码时,这是输出: