no repaint while resizing when using .setPaint(gradient)

前端 未结 2 2006
野趣味
野趣味 2021-01-25 00:43

As soon a I use gradients in my code, the repaint isn\'t done while resizing I get something like that while resizing (black rectangles where it has been resized, see image in t

2条回答
  •  逝去的感伤
    2021-01-25 01:27

    I put together a test and found that the GradientPaint has woeful performance. Taking an average render time from 1.2 seconds (at 400x400 pixles) out to 20+ seconds.

    I changed the GradientPaint for a LinearGradientPaint and found that the render time was around 1.3 seconds instead.

    LinearGradientPaint lgp = new LinearGradientPaint(
                    new Point2D.Float(0, minY),
                    new Point2D.Float(0, maxY),
                    new float[] {0f, 0.5f, 1f},
                    new Color[] {Color.BLUE, Color.RED, Color.BLUE}
                    );
    g2d.setPaint(lgp);
        // Render all your samples, don't reapply or change you paint...
    

    Sorry, my sample isn't very exciting...

    enter image description here

    You may find it better to render to a backing buffer in a background thread instead and paint the whole image to the screen once it's complete. This will stop the screen from becoming "paused"

提交回复
热议问题