Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Applets
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Applets
how do i draw a dotted straight line on an applet.
ravi vardhin
Ranch Hand
Posts: 37
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
how do i draw a dotted straight line on an
applet
.
can some one give the logic..
Thanks and regards,
ravi
Manfred Leonhardt
Ranch Hand
Posts: 1492
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Ravi,
Try the following code.
public void drawDashedLine(Graphics g,int x1,int y1,int x2,int y2, double dashlength, double spacelength) { if((x1==x2)&&(y1==y2)) { g.drawLine(x1,y1,x2,y2); return; } 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); }
Regards,
Manfred.
Amit Agarwal
Ranch Hand
Posts: 92
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi ravi, if you are running your applet thru the
java
plugin, you can use the BasicStroke class of the java2D api. it is available in the java.awt.geom package. Usage:
public void drawDashedLine(Graphics g1, int x1, int y1, int x2, int y2, double dashlength, double spacelength) { Graphics2D g=(Graphics2D) g1; Stroke oldStroke=g.getStroke(); float f[]={dashlength,spacelength}; BasicStroke dashStroke=new BasicStroke( 1.0f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 10.0f, f, 0.0f); g.draw(new Line2D.Float( x1, y1, x2, y2)); g.setStroke(oldStroke); }
Amit
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How do i find the centre of the applet
Drawing dotted/dashed lines in a scaled JPanel?
Rational XDE - create stereotype ?
Draw Rectangle dotted line the selected Image area
Drawing a rectangular box
More...