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 / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Dotted lines.. without Graphics2D

Thread view: 
SPG - 27 Feb 2005 16:54 GMT
Hi,

I need to draw a dotted line, but have to support JDK 1.1 (don't ask why, I
have protested against this to my bosses on many occasion!)

Anyway, I have a 1.2 enables class that does the dots and thick line drawing
if a 2D enables graphics object is available, but, my math is not strong
enough to work out how to do the drawing of a dotted line manually..

Any takers?

Steve
Andrey Kuznetsov - 27 Feb 2005 17:05 GMT
> I need to draw a dotted line, but have to support JDK 1.1 (don't ask why,
> I have protested against this to my bosses on many occasion!)

I think Acme.com has solution for that.
http://www.acme.com/java/software/Acme.Psg.html

Signature

Andrey Kuznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

SPG - 28 Feb 2005 10:27 GMT
Hmmm..

Not quite.
He has a thick line drawer which cheats by drawing ovals down the vector.
Not quite the same math I thinks..

Steve

>> I need to draw a dotted line, but have to support JDK 1.1 (don't ask why,
>> I have protested against this to my bosses on many occasion!)
>
> I think Acme.com has solution for that.
> http://www.acme.com/java/software/Acme.Psg.html
Thomas Weidenfeller - 01 Mar 2005 08:17 GMT
> Anyway, I have a 1.2 enables class that does the dots and thick line drawing
> if a 2D enables graphics object is available, but, my math is not strong
> enough to work out how to do the drawing of a dotted line manually..

Look for Brewsenham's line drawing algorithm or for Wu's two-step line
drawing algorithm for the basics of drawing a line. Then a simple way to
apply a pattern to the line is to map the outcome of the algorithm
against a predefined bit mask to decide if/how a particular pixel should
be colored. The bit mask should contain a 360 degree view of the line
pattern. Of course, in practice a 90 degree quadrant and some simple
mirroring of the quadrant works, too. A more precise way to apply a
pattern is to track the pattern along the line (similar to what
Brewsenham does with the error epsilon value).

Just for some extra fun you could throw in some anti-aliasing when
plotting the line, too, because you have a lot of information in the
algorithm to do so.

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

SPG - 01 Mar 2005 09:21 GMT
Whoooaaaa!
Lost me at "Brewsenham's line drawing algorithm"... ;)
great information. I will look this up and see what I can do, even if for my
own personal achievment!

Thanks again,
Steve

>> Anyway, I have a 1.2 enables class that does the dots and thick line
>> drawing if a 2D enables graphics object is available, but, my math is not
[quoted text clipped - 16 lines]
>
> /Thomas
Babu Kalakrishnan - 01 Mar 2005 11:42 GMT
[Moving top posted response down]

>>Anyway, I have a 1.2 enables class that does the dots and thick line
>>>drawing if a 2D enables graphics object is available, but, my math is not
[quoted text clipped - 8 lines]
> great information. I will look this up and see what I can do, even if for my
> own personal achievment!

Please do not top-post.

I think a printer's devil crept into Thomas's post in the name of the
algorithm - you might find better results if you search for "Bresenham's
Algorithm" instead.

BK
SPG - 01 Mar 2005 16:01 GMT
> [Moving top posted response down]
>
[quoted text clipped - 18 lines]
>
> BK

Hi,

Yes, thanks for that, I managed to find the algorithm and coded it thus:

<SNIP>
public static void drawDottedLine(Graphics g, int x0, int y0, int x1, int
y1, Color color, int dashLen, int spaceLen) {
   Color c = g.getColor();
   g.setColor( color );
   int dx = x1 - x0;
   int dy = y1 - y0;
   float t = 0.5f;

   g.setColor(color);
   g.drawLine(x0, y0, x0, y0);

   int dashCount = 0;
   int spaceCount = 0;
   boolean doPlot = dashLen > 1;

   if (Math.abs(dx) > Math.abs(dy)) { // slope < 1
       float m = (float) dy / (float) dx; // compute slope
       t += y0;
       dx = (dx < 0) ? -1 : 1;
       m *= dx;
       while (x0 != x1) {
           x0 += dx; // step to next x value
           t += m;
           if (doPlot) {
               g.drawLine(x0, (int) t, x0, (int) t);
               dashCount++;
               if (dashCount >= dashLen) {
                   dashCount = 0;
                   spaceCount = 0;
                   doPlot = false;
               }
           }
           else {
               spaceCount++;
               if (spaceCount >= spaceLen) {
                   spaceCount = 0;
                   dashCount = 0;
                   doPlot = true;
               }
           }

       }
   }
   else if (dy != 0) { // slope >= 1
       float m = (float) dx / (float) dy; // compute slope
       t += x0;
       dy = (dy < 0) ? -1 : 1;
       m *= dy;
       while (y0 != y1) {
           y0 += dy; // step to next y value
           t += m;
           if (doPlot) {
               g.drawLine( (int) t, y0, (int) t, y0);
               dashCount++;
               if (dashCount >= dashLen) {
                   dashCount = 0;
                   spaceCount = 0;
                   doPlot = false;
               }
           }
           else {
               spaceCount++;
               if (spaceCount >= spaceLen) {
                   spaceCount = 0;
                   dashCount = 0;
                   doPlot = true;
               }
           }
       }
   }
   g.setColor(c);
}

</SNIP>

Works very well!

Steve


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



©2008 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.