一旦我在代码中使用了渐变,在调整大小时就不会完成重新绘制,而在调整大小时 会得到类似的信息(已调整大小的黑色矩形,请参见下面的链接中的图像)。当我停止调整大小时,一切都会 再次绘制,但只有那时。
如果我不使用,g2d.setPaint(gradient);我会快速重画
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()拖动鼠标时却 什么也没有。
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); }
use :
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多秒。
我换了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...
抱歉,我的样品不是很令人兴奋……
您可能会发现,最好改为在后台线程中渲染到后台缓冲区,并在完成后将整个图像绘制到屏幕上。这将阻止屏幕“暂停”