使用.setPaint(渐变)时调整大小时不重绘

时间:2012-08-20 12:05:02

标签: java swing image-resizing repaint paintcomponent

一旦我在我的代码中使用渐变,重新调整就不会在调整大小时完成调整时调整大小(调整大小的黑色矩形,请参阅下面链接中的图像)。当我停止调整大小时,一切都会被重新绘制,但只有这样。

如果我不使用g2d.setPaint(gradient);,我会快速重绘

http://gui-builder.com/C41142775162.rar

public void paintComponent(Graphics g)  
{  

        super.paintComponent(g);  

        Graphics2D g2d = (Graphics2D)g;  

        //sample of the code  
        GradientPaint gradient = new GradientPaint(startX, startY, greyColor1, endX, endY, new Color(120,120,120));  
        g2d.setPaint(gradient);  
        g.drawLine(i, startY, i, endY);  
}  

我尝试repaint()调整大小时,我试着repaint()拖动鼠标但什么都没有。


这里有一些SSCCE(抱歉我之前没有发布):

BufferedImage aa;

@Override  
public void paintComponent(Graphics g)  
{  
        super.paintComponent(g);  

        Graphics gr = aa.getGraphics();  
        Graphics2D g2d = (Graphics2D)gr;  

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
                    RenderingHints.VALUE_ANTIALIAS_ON);  


        for (int i = 0; i < this.getWidth(); i++)  
        {  
            LinearGradientPaint lgp = new LinearGradientPaint(
            new Point2D.Float(0, 0),
            new Point2D.Float(0, this.getHeight()),
            new float[] {0f, 0.5f, 1f},
            new Color[] {Color.BLUE, Color.RED, Color.BLUE}
            ); 
            g2d.setPaint(lgp);  
            gr.drawLine(i, 0, i, this.getHeight());  
        }  
        g.drawImage(aa, 0, 0, frame);  
}  

并在你的构造函数中:

aa = new BufferedImage(100,100,BufferedImage.TYPE_INT_ARGB);

我还需要一个答案,为什么当我调整窗口大小时(当我移动窗口的调整大小角落时)没有完成重绘时


好的总结一下: 我尝试了使用Java绘制的三种主要方法,即BufferStrategy,双Swing缓冲图像和没有图像缓冲区的简单摆动。 而且我发现最快的一个(令人惊讶的是)。

现在我正在使用快速的一个,我发现首先将窗口调整为小尺寸然后将窗口调整为大尺寸会使问题消失。不要笑这是我的问题,这是一个完全的谜。 这是一段视频:C41142775162.rar

当我调整到小尺寸时会发生什么?我不知道。但如果你知道任何帮助将不胜感激。

感谢

杰夫


我也发现最好尽可能使用setPaint。你可以运行测试,你会发现不经常使用setPaint()会快得多。例如,而不是使用:

LinearGradientPaint gradient1 = new LinearGradientPaint(
        new Point2D.Float(0, 0),
        new Point2D.Float(0, 10),
        new float[] {0f, 1f},
        new Color[] {new Color(40,40,40), new Color(110,110,110)}
    );
LinearGradientPaint gradient2 = new LinearGradientPaint(
        new Point2D.Float(0, 10),
        new Point2D.Float(0, 20),
        new float[] {0f, 1f},
        new Color[] {new Color(110,110,110), new Color(190,190,190)}
    );
LinearGradientPaint gradient3 = new LinearGradientPaint(
        new Point2D.Float(0, 20),
        new Point2D.Float(0, 30),
        new float[] {0f, 1f},
        new Color[] {new Color(190,190,190), new Color(250,250,250)}
    );                        
for (int i = 0; i < this.getWidth(); i++)
{
    g2d.setPaint(gradient1);
    gr.drawLine(i, 0, i, 10);
    g2d.setPaint(gradient2);
    gr.drawLine(i, 10, i, 20);
    g2d.setPaint(gradient3);
    gr.drawLine(i, 20, i, 30);                            
}

使用:

LinearGradientPaint gradient1 = new LinearGradientPaint(
        new Point2D.Float(0, 0),
        new Point2D.Float(0, 10),
        new float[] {0f, 1f},
        new Color[] {new Color(40,40,40), new Color(110,110,110)}
    );
LinearGradientPaint gradient2 = new LinearGradientPaint(
        new Point2D.Float(0, 10),
        new Point2D.Float(0, 20),
        new float[] {0f, 1f},
        new Color[] {new Color(110,110,110), new Color(190,190,190)}
    );
LinearGradientPaint gradient3 = new LinearGradientPaint(
        new Point2D.Float(0, 20),
        new Point2D.Float(0, 30),
        new float[] {0f, 1f},
        new Color[] {new Color(190,190,190), new Color(250,250,250)}
    );                         

g2d.setPaint(gradient1);
for (int i = 0; i < this.getWidth(); i++)
     gr.drawLine(i, 0, i, 10);

g2d.setPaint(gradient2);
for (int i = 0; i < this.getWidth(); i++)
    gr.drawLine(i, 10, i, 20);

g2d.setPaint(gradient3);
for (int i = 0; i < this.getWidth(); i++)
    gr.drawLine(i, 20, i, 30); 

即使你有很多计算,它几乎每次都会更快!

2 个答案:

答案 0 :(得分:2)

我整理了一个测试,发现GradientPaint表现糟糕。平均渲染时间从1.2秒(400x400像素)到20秒以上。

我为GradientPaint更改了LinearGradientPaint,发现渲染时间大约是1.3秒。

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...

对不起,我的样本不是很令人兴奋......

enter image description here

您可能会发现更好地渲染到后台线程中的后备缓冲区,并在完成后将整个图像绘制到屏幕上。这将阻止屏幕变为“暂停”

答案 1 :(得分:1)

如果这是applet,请将该内容放入

public void start()
{
}

public void stop()
{
}