一旦我在我的代码中使用渐变,在调整大小时没有完成重绘我在调整大小时得到类似的东西(黑色矩形已经调整大小,请参见下面链接中的图像)。当我停止调整大小时,一切都会再次绘制,但只有那时。
如果我不使用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(抱歉我之前没有发布过):
缓冲图像 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);
即使你有很多计算,它几乎每次都会更快!