0
我試圖用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
生成隨機厚度線?
感謝您的回答。它現在有效,我做了一個簡單的乘法運算 'float width =(rand.nextFloat()* 10);' – roccia