graphics2d - Java Graphics drawline performance issues -


i having issues speed of drawline method of graphics class. using draw line graph screen linked list. once list large enough (around 150000 values) takes lot longer loop through entire list , redraw lines. wondering can improve performance of program either optimizing drawline method, or abandoning it.

@override protected void paintcomponent(graphics g) {     ((graphics2d) g).setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on);      ((graphics2d) g).setrenderinghint(renderinghints.key_stroke_control, renderinghints.value_stroke_pure);     ((graphics2d) g).setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_speed);                   graphics2d scaledg = (graphics2d) g.create();         g.dispose();         super.paintcomponent(scaledg);         affinetransform scaletransform = new affinetransform();         scaletransform.scale(graphscale, 1);         scaledg.settransform(scaletransform);         scaledg.setcolor(new color(242,100,66));             (line line : lines) {                    scaledg.drawline((int)line.x1, (int)line.y1, (int)line.x2, (int)line.y2);                          }            scaledg.dispose();   } 

as question quite broad, it's hard give definitive answer, i'll give general approaches check out improving rendering performance.

  1. caching static parts of rendering: 1 helpful trick when drawing large number of shapes caching "static" parts of shapes rendering them once bufferedimage, rendering image "dynamic" shapes onto graphics object.

    to apply it, crucial issue whether can know of lines static , need rendered dynamically. if can't assume on lines, approach doesn't apply -- you'll need render everything. however, if know that, e.g., first n lines don't change of renderings, worth checking out.

    a minor drawback of approach dynamic parts rendered on top of static parts. in many applications, example editing shapes, not matter, in others, does.

  2. "level of detail"/smoothing approach: if line (segments) form polyline or polygon, consider whether can interpolate or ignore of segments. crude approach check whether transformed (pixel) endpoints of following segment differ transformed (pixel) endpoint of current segment; if not, segment make no difference resulting image, , can skip it.

    however, don't use algorithm illutrating general idea; there many more , better algorithms "reducing" or "smoothing" polylines , polygons, see example simplipoly or david eberly's polyline reduction starters.

    of course, depends on how "smooth" lines are, , if connected or not. geographical data (coastlines, rivers, etc.), optimization can compress data fractions of original size; random , unconnected lines, won't help.

  3. provide quick preview asynchronous incremental refinement: if both of above approaches not apply, or still don't suffice, there may no effective way substantially speed applications (see below). in case, try provide kind of quick preview instantaneously, refined asynchronously (swingworker or computation thread). 1 of lines changed, stop refinement rendering, provide next preview, , restart refinement. while not speed rendering (on contrary :), make application subjectively more responsive, , more important pure clock cycles.

    in example, simple start rendering first n thousand lines. start "render remaining" thread, incrementally draw more lines long lines not change; however, user can change parameters of lines before lines rendered, if realizes wasn't wanted.

    the main downside of approach adds complexity. multithreading requires careful synchronization prevent race issues, , prone nasty bugs. however, it's amazing how effective approach is.

  4. low-level optimizations , profiling: last not least, can try strip of unnecessary computations. don't set renderinghints. don't use transformations of graphics object, -- in example, multiply line coordinates, maybe cache scaled coordinates if scaling remains constant of time (a variant of #1). maybe rasterize lines pixel buffer, create image , render that. profile rendering , see can shave off few clocks.

    however, while may improve performance, a) hard maintain , b) won't solve general problems, push frontier little further.

you can, of course, combine these approaches.

if of these not apply or help, option switch programming language allows more direct hardware (graphics card) communication java.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -