Reputation: 179
I'm trying to use setStroke
and BasicStroke
to draw random thickness lines.
here is the painting code
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()
);
}
}
when I set the width of stroke to a certain number, it can draw correctly. I looked up the BasicStroke
class, it have the following parameters:
float width;
int join;
int cap;
float miterlimit;
float[] dash;
float dash_phase;
Besides width, I'm not sure what are the others function.
How can I use the BasicStroke
to generate random thickness lines?
Upvotes: 0
Views: 1070
Reputation: 28687
I think the biggest problem is that nextFloat()
is returning values between 0 and 1 - where I'm guessing you want numbers greater than 1 in order to be able to see any visible difference in line thickness.
Besides width, I'm not sure what are the others function.
Please refer to the Javadocs .
Upvotes: 2