We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Drawing a dashed line
Page Index Toggle Pages: 1
Drawing a dashed line (Read 1393 times)
Drawing a dashed line
Jul 30th, 2008, 9:21pm
 
Is there an easy way to draw a dashed line in Processing? I know that Java has BasicStroke class which allows users to create a dashed pattern, but I'm not sure if Processing also supports this.

Thanks!
Re: Drawing a dashed line
Reply #1 - Jul 31st, 2008, 12:28pm
 
Hi, maybe have a look at lerp()

http://processing.org/reference/lerp_.html

quotted form the example:
Quote:
The lerp function is convenient for creating motion along a straight path and for drawing dotted lines


Re: Drawing a dashed line
Reply #2 - Jul 31st, 2008, 12:42pm
 
Looks like it is simpler than I thought:
Code:
float[] dashes = { 16.0f, 8.0f, 4.0f, 8.0f };
BasicStroke pen;

void setup()
{
size(400, 400);
noLoop();
pen = new BasicStroke(4.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER,
4.0f, dashes, 0.0f);

Graphics2D g2 = ((PGraphicsJava2D) g).g2;
g2.setStroke(pen);
}

void draw()
{
line(0, 0, width, height);
line(0, height, width, 0);
}
Re: Drawing a dashed line
Reply #3 - Jul 31st, 2008, 5:30pm
 
Great! Thanks guys Smiley
Re: Drawing a dashed line
Reply #4 - Sep 8th, 2008, 10:08pm
 
that's great...i thought you would need some fancier method to construct it...new to processing, how do you know what arguments the BasicStroke constructor takes ?
Re: Drawing a dashed line
Reply #5 - Sep 9th, 2008, 1:34am
 
We just had that topic some weeks ago :

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1219255354

Re: Drawing a dashed line
Reply #6 - Sep 9th, 2008, 4:07am
 
am I missing something - there's no mention of the BasicStroke constructor on that page...
Re: Drawing a dashed line
Reply #7 - Sep 9th, 2008, 11:33am
 
This kind of info can be found at the official Java site by Sun: BasicStroke. Smiley
Re: Drawing a dashed line
Reply #8 - Sep 9th, 2008, 7:10pm
 
also keep in mind that this only works in the default renderer, and as with anything when you grab the Graphics2D object, don't be sad if it doesn't work as expected. (see the faq for more dire warnings re: don't use java graphics.)

sincerely,

the caveat police
Page Index Toggle Pages: 1