Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2003

Tip: Looking for answers? Try searching our database.

how to draw a dashed line between two points

Thread view: 
Rick - 30 Sep 2003 14:24 GMT
Can someone please show me how to draw a dashed line between two points?
Is there a method in the AWT library that allows this? Thanks!

Rick
Robert Olofsson - 30 Sep 2003 16:31 GMT
: Can someone please show me how to draw a dashed line between two points?
: Is there a method in the AWT library that allows this? Thanks!

http://java.sun.com/docs/books/tutorial/2d/display/strokeandfill.html

/robo
Tr0mBoNe- - 30 Sep 2003 20:21 GMT
> Can someone please show me how to draw a dashed line between two points?
> Is there a method in the AWT library that allows this? Thanks!
>
> Rick

There is only one way i can think of. First you have to take the 2
points, and their line connecting them, and dividing it up into n
segments where n is really big. Then, you can compute the endpoints of
each segment and make every n/oh say 50 lines wite (or the background
color). There is more math and a trickey algorithm to get down so it
will be scalable, but its really not that hard. There is no method in
any awt class that will make the style of the line dotted, unless you
create a border around a rectangle of 0 width and 0 padding, but it
would take about the same amount of code to implement.

Cheers.
Sandip Chitale - 30 Sep 2003 20:36 GMT
> Can someone please show me how to draw a dashed line between two points?
> Is there a method in the AWT library that allows this? Thanks!
>
> Rick
http://groups.google.com/groups?q=drawDashedLine+group:comp.lang.java.programmer
&hl=en&lr=&ie=UTF-8&oe=UTF-8&group=comp.lang.java.programmer&edition=us&selm=330
0C40A.1281%40mad.scientist.com&rnum=2


For reference:

public void drawDashedLine(Graphics g,int x1,int y1,int x2,int
y2,double dashlength,double spacelength){
   double linelength=Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
   double yincrement=(y2-y1)/(linelength/(dashlength+spacelength));
   double xincdashspace=(x2-x1)/(linelength/(dashlength+spacelength));
   double yincdashspace=(y2-y1)/(linelength/(dashlength+spacelength));
   double xincdash=(x2-x1)/(linelength/(dashlength));
   double yincdash=(y2-y1)/(linelength/(dashlength));
   int counter=0;
   for(double i=0;i<linelength-dashlength;i+=dashlength+spacelength){
     g.drawLine((int) (x1+xincdashspace*counter),(int)
(y1+yincdashspace*counter),
       (int) (x1+xincdashspace*counter+xincdash),(int)
(y1+yincdashspace*counter+yincdash));
     counter++;
   }
   if ((dashlength+spacelength)*counter<=linelength)
   g.drawLine((int) (x1+xincdashspace*counter),(int)
(y1+yincdashspace*counter),x2,y2);
 }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.