【发布时间】:2015-03-01 17:00:16
【问题描述】:
我的任务是随机绘制宽度和粗细的水平线。当我运行我的程序时,厚度会部分改变。我不明白这是怎么可能的,因为我是在设置宽度然后画线。我不更新,所以据我所知,我不会重画这条线。程序中唯一的其他代码是 init(),因为我们必须在 applet 中进行。
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.red);
int increment = 70; // space between lines
int linesNum = 5; // number of lines were drawing
Random rand = new Random();
int randW;
Line2D.Float[] lines = new Line2D.Float[linesNum];
for (int y = 0; y < linesNum; y++) {
// where we are at on they axis
int yoffset = (increment * y) + increment;
// random width of line max 499 min 50
randW = rand.nextInt((499 - 5) + 1) + 5;
// random thick line
g2.setStroke(new BasicStroke(rand.nextInt((increment - 10) + 1) + 5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
lines[y] = new Line2D.Float(1, yoffset, randW, yoffset);
g2.draw(lines[y]);
}
}
}
【问题讨论】:
-
你明白这个方法会被再次调用,从而绘制不同值的不同线条,每次你的小程序被重新绘制时,比如如果窗口被遮挡和未被覆盖,不是吗?
-
我不是,这清楚了谢谢你:)
标签: java variable-assignment graphics2d