The application I am creating has to draw around 20.000 rectangles and 150.000
lines to a screen.
On a pentium system (PIII with 700 MHz) under linux it takes around 4..5 seconds to
draw them in zoom fit mode (I cannot make that much faster). It works similar on
SUN systems (have tested it there, but presently I have no measurements made) -
window VMs are usually faster on the same CPU.

If I zoom in, the amount of objects to draw is usually reduced (not all objects
cross the clipping rectangle), so I check if I have to draw the objects at all. If
I don't have to draw it, I discard the drawing.

Some ways to make it faster while showing zoomed in views:
a) use clipping rectangle and the Shape.contains(..) method to check before you
have to draw the object. This check is performed on the system memory and in case
that the object doesn't overlay your clipping re3ctangle, you don't have to draw
something --> drawing speed up enourmously!
The clipping system usually avoid drawing the objects, if they are not seen, but
then this goes through the rendering pipe, and the more objects are pushed into
this one, the slower it is.

b) don't use the draw(Shape) function from Java2D. They are around 3..4 times
slower than drawLine, drawRect,... (well it has some side-effect, because of using
int values instead of floats, but hmmm - peed is more important).

c) use double buffering (in this case it is called backing store, because you use a
image that has the same size as your visible area of the component). You draw into
a double buffer and at the end the buffer is copied to the screen.
Drawing into system memory is much more efficient than drawing directly to the
graphics memory.
Note: there is a side-effect here. If you are working remotely this can affect
drawing speed, because it copies a full image over to the graphics buffer, even if
only one pixel has changed! (means high amount of data transfer for only a small
change). If you are working locally - the double buffering is the way to use!
This problem is fixed in java 1.4 (as far as I heared).

d) switch of anti aliasing, this is usually slowing down the rendering process (but
the quality is going down too a bit).

e) for my rectangles and lines I 'overloaded' (or created) my own contains
function, which is a bit faster than the one from the original classes. But I found
out that the additional time for the contains() checks is only some percentages.
Not really worth the investigations.

After I used all these techniques I can redraw in a zoom in view very fast (less
than some ms) - and I even see lots of lines and rectangles on a screen.

I heared the new Java 1.4 has an improved rendering pipeline, so it should be even
faster there?!?! (haven't tried the beta yet).

Hope this helps a bit.
Karsten
PS: if you use a double buffering system - the screen should never ever flicker.
Try to overload the update() function to avoid clearing the screen. Typically a
double buffering drawing will copy the full image over the old image and the only
thing that is cleared before drawing is the image, not the screen!

> Hi,
>
> I am drawing scientific graphs consisting of a large number (over 1000) of
> unconnected lines.  The user can zoom in using a JSlider.  It takes around 50
> milliseconds to perform a zoom, and this flickers when the slider is dragged.  I
> tried double buffering and this increased the time it took to draw ten fold.
>
> Should I be able to get better performance with java 2D.  Does anyone have any
> suggestions about how to improve my code.  Any help would be greatly
> appreciated.
>
> I am running it on a Sun Ultra 10 under solaris.
>
> I include my code for doing the drawing.
>
> void drawGraph(Graphics g)
>   {
>     g2d = (Graphics2D)g;
>     int i;
>     at = new AffineTransform();
>     g2d.translate(xPos, yPos);
>     at.setToScale(factor, factor);
>     g2d.transform(at);
>     g2d.translate(-xPos, -yPos);
>
> // Draw Grid
>     g2d.setColor(Color.red);
>     for(i=0; i<20; ++i)
>     {
>         j = i*size;
>         g2d.drawLine(i*size,0,i*size,(19 * size));
>     }
>
>     for(i=0; i<20; ++i)
>     {
>         j =  i*size;
>         g2d.drawLine(0,i*siz,(19 * size),i*size);
>     }
>
>     g2d.setColor(Color.blue);
>
> // Draw Line
>     drawLineToPoints(g2d, xValues1, yValues1, xValues2, yValues2);
>   }
>
> // Function to draw the line
>   void drawLineToPoints(Graphics2D g2d, Vector xValues1, Vector yValues1, Vector
> xValues2, Vector yValues2)
>   {
>         for(int a=0; a<xValues1.size(); ++a)
>         {
>                 g2d.drawLine(getInt(xValues1, (a)),((19 * size + 1) -
> getInt(yValues1, (a))),getInt(xValues2, (a)),((19 * size + 1) - getInt(yValues2,
> (a))));
>         }
>   }
>
> ===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff JAVA2D-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

--
Karsten Trott
Technical Lead, Intuitive Design Interface , Get2Chip.com, Inc.
Staff R&D Engineer
Phone: +49-89-5908-2333
Fax:   +49-89-5908-1328

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to