Zack Newsham
Zack Newsham

Reputation: 2982

Java 2d drawLine slow for long lines

I have a piece of code that needs to draw many thousands of lines (between 6000 and 50000).

An unrelated bug that I have subsequently fixed, exposed something interesting; namely, when these lines are very long (say 30k pixels) the drawLine function takes up 95% of my codes time, compared with when the lines are only 1000 pixels long, when it takes up only 15% of the time.

The lines are the same colour, fixed width (1px)

Why are long line draws so slow? What options do I have for speeding this up, not just for the long lines, are there rendering hints that can be used? I am already running this threaded.

Upvotes: 0

Views: 369

Answers (1)

King Dedede
King Dedede

Reputation: 1010

I believe the java drawline function, when you draw a line into NEW territory (I.E. it's only rendered a 300x300 area but you draw a line out to 200,500), it has to initialize all those pixels to drawable area.

Here's a snippet from a similar answer:

"I suggest you to calculate the visible part of the painting area (using either JComponent's getVisibleRect () method or Graphics g.getClip ().getBounds () method) and limit your paintings with only that area.

That small optimization could speedup component's painting in times if it is really large (for example with 10000x10000 pixels component's area)."

Upvotes: 1

Related Questions