我正在尝试使用setStroke
并BasicStroke
绘制随机粗细线。
这是绘画代码
public void paintComponent(Graphics g1) {
Random rand = new Random();
Graphics g2 = (Graphics2D) g1;
//set background color
g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());
Dimension d = getPreferredSize();
//set line's color
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
Color randomColor = new Color(r,g,b);
g2.setColor(randomColor);
//set line's stroke
float width = rand.nextFloat();
BasicStroke randomStroke = new BasicStroke(width);
((Graphics2D) g2).setStroke(randomStroke);
for (Line2D.Double line : lines) {
g2.drawLine(
(int)line.getX1(),
(int)line.getY1(),
(int)line.getX2(),
(int)line.getY2()
);
}
}
当我将笔画的宽度设置为某个数字时,它可以正确绘制。我查了一下这个BasicStroke
类,它有以下参数:
float width;
int join;
int cap;
float miterlimit;
float[] dash;
float dash_phase;
除了宽度,我不确定其他功能是什么。如何使用BasicStroke
生成随机粗细线?