Efficient method for drawing many rectangles.

Discussion in 'Java' started by Chuck Dillon, Feb 6, 2004.

  1. Chuck Dillon

    Chuck Dillon Guest

    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.
    Chuck Dillon, Feb 6, 2004
    #1
    1. Advertising

  2. Chuck Dillon

    Andrew Hobbs Guest

    "Chuck Dillon" <> wrote in message
    news:c00q0b$1oc$...
    >
    >
    > 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.
    >
    Andrew Hobbs, Feb 7, 2004
    #2
    1. Advertising

  3. Chuck Dillon

    Chuck Dillon Guest

    Andrew Hobbs wrote:
    > "Chuck Dillon" <> wrote in message
    > news:c00q0b$1oc$...
    >
    >>
    >>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.
    Chuck Dillon, Feb 9, 2004
    #3
  4. Chuck Dillon

    Dario Guest

    Dario, Feb 9, 2004
    #4
  5. In article <c088nm$n1r$>,
    Dario <> 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
    Steve W. Jackson, Feb 10, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    509
  2. Replies:
    1
    Views:
    633
    Kevin Spencer
    Jan 9, 2006
  3. Ajay Patil
    Replies:
    0
    Views:
    444
    Ajay Patil
    Dec 10, 2003
  4. Abs
    Replies:
    4
    Views:
    8,411
    Jesper Nordenberg
    May 14, 2004
  5. defn noob
    Replies:
    1
    Views:
    382
    Mark Space
    Jun 28, 2008
Loading...

Share This Page