Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Efficient method for drawing many rectangles.

Reply
Thread Tools

Efficient method for drawing many rectangles.

 
 
Chuck Dillon
Guest
Posts: n/a
 
      02-06-2004


If you have an array of N Rectangle2D.Double objects, (where N is
potentially tens of thousands), what's the most efficient way to render
them in a single color?

My application needs to render many rectangles (i.e. thousands). My
graphics experience tells me that in general the most efficient way to
render graphics is the case where you "batch" as much of the operation
as possible. That's probably biased by my X experience where you want
to a few drawlines() calls rather than do a lot of drawline() calls
since there's a commuications channel between your client and the X
server. But IMHO batching primitive calls is a good thing.

Applying this to Java, first I looked for a drawLines or drawShapes
method in Graphics2D and did't see any. So next I thought it should be
more efficient to shove a bunch of Rectangle shapes into a GeneralShape
object and draw(path) in one call than to draw(Rectangle) many times.
But I'm not seeing that to be true. In fact, I seem to be seeing a
pathalogical case where it can take a looong time to render a
GeneralPath with on the order of 32k rectangles in it. (JRE/JDK 1.4.1_01)

Thanks for any advice,

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
Reply With Quote
 
 
 
 
Andrew Hobbs
Guest
Posts: n/a
 
      02-07-2004

"Chuck Dillon" <(E-Mail Removed)> wrote in message
news:c00q0b$1oc$(E-Mail Removed)...
>
>
> If you have an array of N Rectangle2D.Double objects, (where N is
> potentially tens of thousands), what's the most efficient way to render
> them in a single color?
>
> My application needs to render many rectangles (i.e. thousands). My
> graphics experience tells me that in general the most efficient way to
> render graphics is the case where you "batch" as much of the operation
> as possible. That's probably biased by my X experience where you want
> to a few drawlines() calls rather than do a lot of drawline() calls
> since there's a commuications channel between your client and the X
> server. But IMHO batching primitive calls is a good thing.
>
> Applying this to Java, first I looked for a drawLines or drawShapes
> method in Graphics2D and did't see any. So next I thought it should be


However Graphics 2D does have drawLines() etc. They are inherited from
Graphics. (see docs).

> more efficient to shove a bunch of Rectangle shapes into a GeneralShape
> object and draw(path) in one call than to draw(Rectangle) many times.
> But I'm not seeing that to be true. In fact, I seem to be seeing a
> pathalogical case where it can take a looong time to render a
> GeneralPath with on the order of 32k rectangles in it. (JRE/JDK 1.4.1_01)
>


What do you mean by a long time to render?

I am not sure what exactly you are doing. As a test I just produced a
Rectangle class with its own draw method. Then I tried rendering Rectangle
objects onto a JPanel. Simply instantiating 1 million rectangle objects
with four randomly generated values for x1, y1, x2, y2 coordinates and then
getting each one to draw itself onto a Jpanel took 16 seconds. So 32,000
Rectangles shouldn't take more than about five hundred milliseconds.

Using the Graphics2D method 'draw(new Rectangle())' method as suggested in
the docs took 11 seconds to produce the 4 million random numbers and then
draw the rectangles on the Jpanel which is about 40% faster.

Of course if the rectangles require much processing then that will slow
things down a bit.

Cheers

Andrew


> Thanks for any advice,
>
> -- ced
>
> --
> Chuck Dillon
> Senior Software Engineer
> NimbleGen Systems Inc.
>



 
Reply With Quote
 
 
 
 
Chuck Dillon
Guest
Posts: n/a
 
      02-09-2004
Andrew Hobbs wrote:
> "Chuck Dillon" <(E-Mail Removed)> wrote in message
> news:c00q0b$1oc$(E-Mail Removed)...
>
>>
>>Applying this to Java, first I looked for a drawLines or drawShapes
>>method in Graphics2D and did't see any. So next I thought it should be

>
>
> However Graphics 2D does have drawLines() etc. They are inherited from
> Graphics. (see docs).


Sorry, I'm not seeing any such method(s) at
http://java.sun.com/j2se/1.4.2/docs/api/index.html. My IDE (Eclipse)
doesn't seem to be aware of any such methods either?

>
>
>>more efficient to shove a bunch of Rectangle shapes into a GeneralShape
>>object and draw(path) in one call than to draw(Rectangle) many times.
>>But I'm not seeing that to be true. In fact, I seem to be seeing a
>>pathalogical case where it can take a looong time to render a
>>GeneralPath with on the order of 32k rectangles in it. (JRE/JDK 1.4.1_01)
>>

>
>
> What do you mean by a long time to render?


What I'm seeing is that if I push on the order of 40k rectangles into a
GeneralPath, unconnected, I sometimes get a rendering time of about 3
minutes or sometimes my development machine (Win2K PIV) locks up
requiring a cold boot. I suppose I'm running into a JVM bug, or
somewhere below. The problem doesn't seem to be determined by the
number of segments in the path. It can also draw 40+k rectangles
pretty quickly (I don't have my timing notes available) when it works.

Regardless of the apparent bug, what I'm looking for is advice on the
most efficient method. I'm guessing that the GeneralPath approach is it.

>
> I am not sure what exactly you are doing.


I'm annotating features in an image with graphical rectangles. In the
paintComponent method of a JPanel I'm rendering rectangles derived from
a large set of feature objects aligned to the image.

The features, and their annotation rectangles, "live" in floating point
image coordinates. I'm using Rectangle2D.Double objects.

I realize that I can simply g2D.draw(feature.getRectangle()).

Performance is important. This is an interactive image display. So
I'm looking for the most efficient approach to render the information
since the user might be dragging the field of view as I'm doing it.

Thanks for the response,

-- ced


> As a test I just produced a
> Rectangle class with its own draw method. Then I tried rendering Rectangle
> objects onto a JPanel. Simply instantiating 1 million rectangle objects
> with four randomly generated values for x1, y1, x2, y2 coordinates and then
> getting each one to draw itself onto a Jpanel took 16 seconds. So 32,000
> Rectangles shouldn't take more than about five hundred milliseconds.
>
> Using the Graphics2D method 'draw(new Rectangle())' method as suggested in
> the docs took 11 seconds to produce the 4 million random numbers and then
> draw the rectangles on the Jpanel which is about 40% faster.
>
> Of course if the rectangles require much processing then that will slow
> things down a bit.
>
> Cheers
>
> Andrew
>
>
>
>>Thanks for any advice,
>>
>>-- ced
>>
>>--
>>Chuck Dillon
>>Senior Software Engineer
>>NimbleGen Systems Inc.
>>

>
>
>



--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
Reply With Quote
 
Dario
Guest
Posts: n/a
 
      02-09-2004
>> However Graphics 2D does have drawLines() etc. They are inherited from
>> Graphics. (see docs).

>
> Sorry, I'm not seeing any such method(s) at
> http://java.sun.com/j2se/1.4.2/docs/api/index.html.


Confirmed: drawLines does *not* exist.
 
Reply With Quote
 
Steve W. Jackson
Guest
Posts: n/a
 
      02-10-2004
In article <c088nm$n1r$(E-Mail Removed)>,
Dario <(E-Mail Removed)> wrote:

>:>> However Graphics 2D does have drawLines() etc. They are inherited from
>:>> Graphics. (see docs).
>:>
>:> Sorry, I'm not seeing any such method(s) at
>:> http://java.sun.com/j2se/1.4.2/docs/api/index.html.
>:
>:Confirmed: drawLines does *not* exist.


No, but drawLine (singular) does. Still, I'm curious why the use of
drawShape (since Rectangle implements Shape) or drawRect can't work
here. I didn't see any clear indication in the thread that it'd been
tried with bad performance.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Guide to the standard library? Library for drawing in GUIs?drawing inbrowsers? defn noob Java 1 06-28-2008 02:50 AM
funny drawing software:ScreenPen,drawing directly on screen! yyzzbb@sina.com Digital Photography 0 02-04-2006 12:31 AM
System.Drawing For Drawing Text Images jjbutera@hotmail.com ASP .Net 1 01-09-2006 09:55 PM
Any Method to save the free drawing as rtf format ? Michael Cheung ASP .Net 0 03-01-2005 04:04 AM
Efficient way of dynamically invoking a method that returns a primitive type? Andreas Jakobik Java 3 12-09-2004 09:02 PM



Advertisments