问题描述
一旦我在我的代码中使用渐变,重新调整就不会在调整大小时完成调整时调整大小(调整大小的黑色矩形,请参阅下面链接中的图像)。当我停止调整大小时,一切都会被重新绘制,但只有这样。
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 the link below). And when i stop resizing, everything is drawn again, but only then.
如果我不使用 g2d.setPaint(渐变);
我有一个快速重绘
if I don't use g2d.setPaint(gradient);
I have a quick redraw
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()
在调整大小时,我尝试重绘()
拖动鼠标时什么都没有。
I tried to repaint()
on resize, I tried to repaint()
when mouse is dragged but nothing.
这里有一些SSCCE(对不起,我之前没有发布):
here is some SSCCE (sorry i didn't post it before):
BufferedImage aa;
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);
aa = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
我仍然需要一个答案,为什么当我调整窗口大小时(当我移动窗口的调整大小角落时)没有完成重绘时
i still need an answer why the redraw isn't done when i resize the window (while i move the resize corner of the window)
好的总结一下:
i尝试了三种主要的Java绘图方法,即BufferStrategy,双Swing缓冲图像和简单摆动没有图像缓冲区。
我发现最快的一个(令人惊讶的是)。
Okay so to sum up :i tried the three main methods of drawing in Java which are BufferStrategy, double Swing buffered image and simple swing with no image buffer.And i found that the faster one is the last one (surprisingly).
现在我正在使用快速的,我发现首先将窗口调整为小尺寸,然后将窗口调整为大尺寸会使问题消失。不要笑这是我的问题,这是一个完全的谜。
这里有一段视频:
Now i'm using the fast one and i found that first resizing the window to a small size then resizing the window to a big size makes the problem disappear. Don't laugh that's my problem and that's a total mystery.here is a video of it : C41142775162.rar
当我调整到小尺寸时会发生什么?我不知道。但如果你知道任何帮助将不胜感激。
what happens when i resize to a small size? i dunno. But if you know any help would be greatly appreciated.
谢谢
杰夫
我也发现最好尽量少用setPaint。你可以运行测试,你会发现不经常使用setPaint()会快得多。例如,而不是使用:
also i found out that it's best to use setPaint as little aas possible. You can run tests and you will see that it's much faster not to use too often setPaint(). for example, instead of using:
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);
即使你有很多计算,它几乎每次都会更快!
even if you have a lot of computations, it will be almost every time faster!
推荐答案
我整理了一个测试,发现 GradientPaint
表现糟糕。平均渲染时间从1.2秒(400x400像素)到20秒以上。
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.
我更改了 GradientPaint
对于 LinearGradientPaint
,发现渲染时间大约是1.3秒。
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...
您可能会发现在后台线程中渲染到后备缓冲区更好,并在完成后将整个图像绘制到屏幕上。这将阻止屏幕变为暂停
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"
这篇关于使用.setPaint(渐变)时调整大小时不重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!