gpt4 book ai didi

java - 绘制(paint)法类型转换-性能

转载 作者:太空宇宙 更新时间:2023-11-04 06:24:45 24 4
gpt4 key购买 nike

我看到很多人在 Paint 方法中将 Graphics 类型转换为 Graphics2d 类型,以便在他们绘制的图像上获得更多功能。是否无法在绘制函数之外将图形类型转换为graphics2d类型?对我来说,这引起了性能问题,因为每次调用 Paint 方法(即每一帧)时,您每次都不必要地转换为 Graphics2D 类型。

此外,每当调用paint方法并再次绘制相同的屏幕时,Java是否会进行任何优化?例如,如果您只在屏幕上移动一个元素,那么您通常只需要更新该元素。每次调用paint方法时,java似乎都会重新绘制并重新处理整个屏幕。对我来说,这一切似乎都是不必要的密集处理。

示例代码:

public void paint(Graphics g) {
AffineTransform transformer = new AffineTransform();
transformer.translate(5,5);
transformer.scale(2,2);
Graphics2D g2d = (Graphics2D)g;
g2d.setTransform(transformer);
// draw to g2d.
}

最佳答案

您声明:

To me this screams performance issue because every time the paint method is called (i.e every frame) you are needlessly casting to a Graphics2D type every single time.

这样做的成本很小,所以我不会担心。相反,分析您的程序并专注于分析器显示的重要瓶颈。

Is it not possible cast the graphics type to a graphics2d type outside of the paint function?

没有。

Also, does Java do any optimization whenever the paint method is called and the same screen is being drawn again? For example if you are only moving one element on the screen then you will generally only need to update that element. It seems java redraws and re-processes the whole screen every time the paint method is called. To me this all seems unnecessarily processing intensive

我不这么认为。我所做的是 1) 创建一个 BufferedImage 来显示我的静态图像,然后直接绘制我的动态图像,2) 如果需要,可以使用接受 Rectangle 的 repaint(...) 方法重载来限制绘制的区域。

关于java - 绘制(paint)法类型转换-性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com