1

I am having issues with the speed of the drawline method of the Graphics class. I am using it to draw a line graph to the screen from a linked list. Once the list is large enough (around 150000 values) it takes a lot longer to loop through the entire list and redraw all the lines. I am wondering what I can do to improve the performance of the program either by optimizing the 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));
            for (Line line : lines) {
                   scaledG.drawLine((int)line.x1, (int)line.y1, (int)line.x2, (int)line.y2);      
                   }
           scaledG.dispose();
  }
2
  • Not the answer you hope, but I think you should consider drawing only one point out of 10, maybe more (when point count is very large). The rational behind this is that drawing a graph with 150000 points on a screen that have ~10³ pixels is a bit useless, and doesn't help the user to read data. (Of course it's depends on the use case, maybe it's not applicable in your specific case). Anyway, you may have to use an other, fast, graphic library (opengl?).
    – johan d
    Jul 22, 2015 at 14:33
  • I ended up changing the list it draws to only contain the vertices of the graph once the size of the list becomes to large to draw smoothly. It basically changes the resolution of the graph. Now it will draw only the critical information needed without the gobly goup. Thank you for the input. Jul 22, 2015 at 19:27

2 Answers 2

4

As your question is quite broad, it's hard to give a definitive answer, so I'll just give you some general approaches I would check out for improving rendering performance.

  1. Caching static parts of the rendering: One very helpful trick when drawing a large number of shapes is caching the "static" parts of the shapes by rendering them once into a BufferedImage, then rendering that image together with the "dynamic" shapes onto the Graphics object.

    To apply it, the crucial issue is whether you can know which of your lines will be static and which need to be rendered dynamically. If you can't assume anything on the lines, this approach doesn't apply -- you'll need to render everything. However, if you know that, e.g., the first N lines don't change for most of the renderings, it could be worth checking out.

    A minor drawback of this approach is that the dynamic parts are always rendered on top of the static parts. In many applications, for example editing shapes, this does not matter, in others, it does.

  2. "Level of Detail"/smoothing approach: If your line (segments) form a polyline or polygon, consider whether you can interpolate or completely ignore some of the segments. A crude approach would be to check whether the transformed (pixel) endpoints of the following segment differ from the transformed (pixel) endpoint of the current segment; if not, the segment would make no difference to the resulting image, and you can skip it.

    However, don't use this algorithm which is just for illutrating the general idea; there are many more and much better algorithms for "reducing" or "smoothing" polylines and polygons, see for example SimpliPoly or David Eberly's polyline reduction for starters.

    Of course, this all depends on how "smooth" your lines are, and if they are connected or not. For geographical data (coastlines, rivers, etc.), this optimization can compress your data to fractions of the original size; for completely random and unconnected lines, this won't help.

  3. Provide a quick preview with asynchronous incremental refinement: If both of the above approaches do not apply, or still don't suffice, there may be no effective way to substantially speed up applications (see below). In this case, I would try to provide some kind of quick preview instantaneously, which is then refined asynchronously (SwingWorker or another computation thread). As soon as one of the lines is changed, you stop the refinement rendering, provide the next preview, and restart the refinement. While this does not really speed up rendering (on the contrary :), it does make your application subjectively more responsive, and this is often much more important than pure clock cycles.

    In your example, you could simple start by rendering the first N thousand lines. Then you start your "render remaining" thread, which incrementally draw more lines as long as the lines do not change; however, the user can change parameters of your lines before all lines are rendered, if he realizes that this wasn't what he wanted.

    The main downside of this approach is that it adds complexity. Multithreading requires careful synchronization to prevent race issues, and is always prone to nasty bugs. However, it's often amazing how effective this approach is.

  4. Low-level optimizations and profiling: Last but not least, you can always try to strip of all unnecessary computations. Don't set any RenderingHints. Don't use transformations of the Graphics object, do it yourself -- in your example, simply multiply the line coordinates, maybe cache the scaled coordinates if the scaling remains constant most of the time (a variant of #1). Maybe even rasterize the lines yourself into some pixel buffer, create an image from it and render that. Profile the rendering and see where you can shave off a few clocks.

    However, while this may improve the performance, it is A) hard to maintain and B) won't solve the general problems, just push the frontier a little further.

You can, of course, combine these approaches.

If all of these do not apply or help, your only option is to switch to a programming language that allows more direct hardware (graphics card) communication than Java.

3
  • Regarding 1, if you cache static renderings into BufferedImages, you can composite in any order you wish so the the drawback you mention really doesn't exist.
    – Charlie
    Jul 22, 2015 at 22:07
  • @Charlie You can't interleave static with dynamic shapes if you only use one buffer though. The drawback exists if formulated that way.
    – johan d
    Jul 23, 2015 at 7:41
  • Yeah. definitely. Once you set aside one buffer, adding more is obvious and trivial. I'm not sure the caveat is worth mentioning here.
    – Charlie
    Jul 23, 2015 at 15:48
0

Don't dispose() a Graphics object that is passed into a painting method. Only dispose() Graphics objects that you create.

Try creating a BufferedImage at normal scale, so the painting is only done once not ever time the paintComponent() method is invoked. Then in the paintComponent() method you can scale the image when you paint it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.