Java awt/swing AffineTransformation 在形状周围移动

发布于 2024-10-03 06:14:31 字数 819 浏览 5 评论 0原文

一旦一次绘制了数百个圆圈,一个动画圆圈的程序就无法流畅地绘制它们。建议使用仿射变换来复制形状。这段代码被重构为使用graphics2D,可以工作,但不会导致任何性能提升,因为它仍然填充数百个椭圆。如何正确使用仿射变换来填充形状一次,然后复制/移动它?


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);

    for (int i = 0; i < gameLogic.getParticleArrSize(); i++) {
        Graphics2D g2 = (Graphics2D) g;
        Color color = new Color(6,6,6);
        Ellipse2D oval = new Ellipse2D.Double(
                gameLogic.getParticleXCoor(i),
                gameLogic.getParticleYCoor(i),
                gameLogic.getParticleSize(i),
                gameLogic.getParticleSize(i));
        g2.setPaint(color);
        g2.fill(oval);
        g2.translate(15, 15);
        g2.fill(oval);
  }

}

A program that animates circles is not drawing them fluidly once several hundred are drawn at once. It was suggested to use affine transformation to copy the shapes. This code, refactored to use graphics2D, works, but doesn't cause any performance boost since it is still filling hundreds of ovals. How to use affinetransformation properly to fill a shape once and then copy/move it?


    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        setBackground(Color.WHITE);

    for (int i = 0; i < gameLogic.getParticleArrSize(); i++) {
        Graphics2D g2 = (Graphics2D) g;
        Color color = new Color(6,6,6);
        Ellipse2D oval = new Ellipse2D.Double(
                gameLogic.getParticleXCoor(i),
                gameLogic.getParticleYCoor(i),
                gameLogic.getParticleSize(i),
                gameLogic.getParticleSize(i));
        g2.setPaint(color);
        g2.fill(oval);
        g2.translate(15, 15);
        g2.fill(oval);
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤蝉 2024-10-10 06:14:31

知道哪个更快的唯一方法是分析两个或多个实现。作为一个具体示例,此动力学模型显示了梯度的轻微优势 em> 模式,使用 drawImage(),在 Color 模式下,使用 fill(),如 paintComponent() DisplayPanel 方法。在这种情况下,AffineTransform 有利于预渲染更复杂的渐变图像。

附录:

我不知道如何正确实现AffineTransform来移动/复制椭圆......

我怀疑 AffineTransform 是解决方法。相反,将对象创建移出循环,如 @camickr 建议的那样。在示例中,请注意 Ensemble 仅需要一个 <代码>Ellipse2D;它重复使用setFrame()。此外,每个粒子已经知道它的颜色。最后,观察示例如何测量绘制时间。

The only way to know which is faster is to profile two or more implementations. As a concrete example, this kinetic model shows a slight advantage for Gradient mode, using drawImage(), over Color mode, using fill(), as seen in the paintComponent() method of DisplayPanel. In this context, AffineTransform is beneficial in pre-rendering the more complex gradient image.

Addendum:

I don't know how to properly implement AffineTransform to move/copy ovals…

I doubt that AffineTransform is the cure. Instead, move the object creation out of the loop, as @camickr suggests. In the example, note how an Ensemble only needs one Ellipse2D; it uses setFrame() repeatedly. Also, each Particle already knows its Color. Finally, observe how the example measures paint time.

本王不退位尔等都是臣 2024-10-10 06:14:31

怀疑这会产生很大的差异,但是:

  1. 不需要在循环内创建新的 Color 对象
  2. 不是保留多个 x、y、宽度、高度值数组,而是保留 Ellipse2D 对象的 ArrayList,这样您就不必继续重新创建那些物体。

我还见过一个使用 5000 个球没有问题的例子。它使用 fillOval(...) 方法。不知道这是否会有所作为。

Doubt it will make a big difference but:

  1. There is no need to create new Color objects inside the loop
  2. Instead of keeping multiple arrays of x, y, width, height values keep an ArrayList of Ellipse2D object so you don't have to keep recreating those objects either.

I've also seen an example that uses 5000 balls without a problem. It uses the fillOval(...) method. Don't know if that will make a difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文