这是使用Java2D图形API的正确方法吗?

这是使用Java2D图形API的正确方法吗?,java,swing,awt,2d,jbox2d,Java,Swing,Awt,2d,Jbox2d,我正在为JBox2D模拟创建一个图形前端。模拟以增量方式运行,在更新之间,应该绘制模拟的内容。与游戏类似,只是没有输入 我只需要几何原语来绘制JBox2D模拟。这个API看起来是最简单的选择,但是它的设计有点混乱 目前,我有一个名为Window extending JFrame的类,其中包含另一个名为Renderer的类作为成员。Window类仅初始化自身,并提供一个由主循环调用的updateDisplay方法,该方法在渲染器上调用updateDisplayobjects方法。我自己创建了这两个

我正在为JBox2D模拟创建一个图形前端。模拟以增量方式运行,在更新之间,应该绘制模拟的内容。与游戏类似,只是没有输入

我只需要几何原语来绘制JBox2D模拟。这个API看起来是最简单的选择,但是它的设计有点混乱

目前,我有一个名为Window extending JFrame的类,其中包含另一个名为Renderer的类作为成员。Window类仅初始化自身,并提供一个由主循环调用的updateDisplay方法,该方法在渲染器上调用updateDisplayobjects方法。我自己创建了这两个方法,它们的唯一目的是在渲染器上调用repaint


JPanel应该这样使用吗?或者我应该使用一些更复杂的动画方法,例如在某些后端线程中涉及事件和/或时间间隔吗?

如果您希望按设定的时间间隔安排更新,请为其提供Swing集成服务。计时器定期在EDT上运行其任务,没有显式循环。显式循环将阻止EDT处理事件,从而冻结UI。我对此进行了更深入的解释

最终,在Swing中进行任何类型的绘画时,您仍将做两件事:

替代paintComponent以进行绘图。 根据需要调用“重新绘制”以请求使图形可见。Swing通常仅在需要时重新绘制,例如当其他程序的窗口经过Swing组件顶部时。 如果你在做这两件事,你可能是做对了。Swing实际上没有用于动画的高级API。它的设计主要考虑了图形用户界面组件。它当然可以做一些好的事情,但是您必须像现在这样从头开始编写组件

如果没有书签,则会覆盖一些“幕后”内容

您可以查看JavaFX。我个人不太了解它,但它应该更倾向于动画

作为某种优化,可以做的一件事是在单独的图像上绘制,然后在paintComponent中将图像绘制到面板上。如果绘制时间较长,这一点尤其有用:系统可以安排重新绘制,以便在更可控的情况下进行重新绘制

如果不绘制图像,则需要使用对象构建模型,并每次在paintComponent内绘制所有对象

以下是绘制图像的示例:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

/**
 * Holding left-click draws, and
 * right-clicking cycles the color.
 */
class PaintAnyTime {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PaintAnyTime();
            }
        });
    }

    Color[]    colors = {Color.red, Color.blue, Color.black};
    int  currentColor = 0;
    BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
    Graphics2D  imgG2 = img.createGraphics();

    JFrame frame = new JFrame("Paint Any Time");
    JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Creating a copy of the Graphics
            // so any reconfiguration we do on
            // it doesn't interfere with what
            // Swing is doing.
            Graphics2D g2 = (Graphics2D) g.create();
            // Drawing the image.
            int w = img.getWidth();
            int h = img.getHeight();
            g2.drawImage(img, 0, 0, w, h, null);
            // Drawing a swatch.
            Color color = colors[currentColor];
            g2.setColor(color);
            g2.fillRect(0, 0, 16, 16);
            g2.setColor(Color.black);
            g2.drawRect(-1, -1, 17, 17);
            // At the end, we dispose the
            // Graphics copy we've created
            g2.dispose();
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(img.getWidth(), img.getHeight());
        }
    };

    MouseAdapter drawer = new MouseAdapter() {
        boolean rButtonDown;
        Point prev;

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                prev = e.getPoint();
            }
            if (SwingUtilities.isRightMouseButton(e) && !rButtonDown) {
                // (This just behaves a little better
                // than using the mouseClicked event.)
                rButtonDown  = true;
                currentColor = (currentColor + 1) % colors.length;
                panel.repaint();
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (prev != null) {
                Point  next = e.getPoint();
                Color color = colors[currentColor];
                // We can safely paint to the
                // image any time we want to.
                imgG2.setColor(color);
                imgG2.drawLine(prev.x, prev.y, next.x, next.y);
                // We just need to repaint the
                // panel to make sure the
                // changes are visible
                // immediately.
                panel.repaint();
                prev = next;
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                prev = null;
            }
            if (SwingUtilities.isRightMouseButton(e)) {
                rButtonDown = false;
            }
        }
    };

    PaintAnyTime() {
        // RenderingHints let you specify
        // options such as antialiasing.
        imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        imgG2.setStroke(new BasicStroke(3));
        //
        panel.setBackground(Color.white);
        panel.addMouseListener(drawer);
        panel.addMouseMotionListener(drawer);
        Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
        panel.setCursor(cursor);
        frame.setContentPane(panel);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
如果例程是长时间运行的,并且重绘可能同时发生,那么也可以使用双缓冲。绘图是对一个独立于所示图像的图像进行的。然后,绘制例程完成后,图像参照将交换,以便无缝更新

例如,您通常应该在游戏中使用双缓冲。双缓冲防止图像以部分状态显示。例如,如果在游戏循环中使用背景线程而不是计时器,并且在游戏进行绘制时重新绘制,则可能会发生这种情况。如果没有双缓冲,这种情况将导致闪烁或撕裂

默认情况下,Swing组件是双缓冲的,因此,如果所有图形都发生在EDT上,则无需自己编写双缓冲逻辑。Swing已经做到了

下面是一个稍微复杂一些的示例,它显示了一个长时间运行的任务和一个缓冲区交换:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;

/**
 * Left-click to spawn a new background
 * painting task.
 */
class DoubleBuffer {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DoubleBuffer();
            }
        });
    }

    final int  width = 640;
    final int height = 480;

    BufferedImage createCompatibleImage() {
        GraphicsConfiguration gc =
            GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration();
        // createCompatibleImage creates an image that is
        // optimized for the display device.
        // See http://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsConfiguration.html#createCompatibleImage-int-int-int-
        return gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    }

    // The front image is the one which is
    // displayed in the panel.
    BufferedImage front = createCompatibleImage();
    // The back image is the one that gets
    // painted to.
    BufferedImage  back = createCompatibleImage();
    boolean  isPainting = false;

    final JFrame frame = new JFrame("Double Buffer");
    final JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Scaling the image to fit the panel.
            Dimension actualSize = getSize();
            int w = actualSize.width;
            int h = actualSize.height;
            g.drawImage(front, 0, 0, w, h, null);
        }
    };

    final MouseAdapter onClick = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (!isPainting) {
                isPainting = true;
                new PaintTask(e.getPoint()).execute();
            }
        }
    };

    DoubleBuffer() {
        panel.setPreferredSize(new Dimension(width, height));
        panel.setBackground(Color.WHITE);
        panel.addMouseListener(onClick);
        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    void swap() {
        BufferedImage temp = front;
        front = back;
        back = temp;
    }

    class PaintTask extends SwingWorker<Void, Void> {
        final Point pt;

        PaintTask(Point pt) {
            this.pt = pt;
        }

        @Override
        public Void doInBackground() {
            Random rand = new Random();

            synchronized(DoubleBuffer.this) {
                Graphics2D g2 = back.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                                    RenderingHints.VALUE_STROKE_PURE);
                g2.setBackground(new Color(0, true));
                g2.clearRect(0, 0, width, height);
                // (This computes pow(2, rand.nextInt(3) + 7).)
                int  depth = 1 << ( rand.nextInt(3) + 7 );
                float  hue = rand.nextInt(depth);
                int radius = 1;
                int c;
                // This loop just draws concentric circles,
                // starting from the inside and extending
                // outwards until it hits the outside of
                // the image.
                do {
                    int rgb = Color.HSBtoRGB(hue / depth, 1, 1);
                    g2.setColor(new Color(rgb));

                    int x = pt.x - radius;
                    int y = pt.y - radius;
                    int d = radius * 2;

                    g2.drawOval(x, y, d, d);

                    ++radius;
                    ++hue;
                    c = (int) (radius * Math.cos(Math.PI / 4));
                } while (
                       (0 <= pt.x - c) || (pt.x + c < width)
                    || (0 <= pt.y - c) || (pt.y + c < height)
                );

                g2.dispose();
                back.flush();

                return (Void) null;
            }
        }

        @Override
        public void done() {
            // done() is completed on the EDT,
            // so for this small program, this
            // is the only place where synchronization
            // is necessary.
            // paintComponent will see the swap
            // happen the next time it is called.
            synchronized(DoubleBuffer.this) {
                swap();
            }

            isPainting = false;
            panel.repaint();
        }
    }
}
绘制例程只是为了绘制垃圾,这需要很长时间:


如果您希望按设定的时间间隔安排更新,请为其提供Swing集成服务。计时器定期在EDT上运行其任务,没有显式循环。显式循环将阻止EDT处理事件,从而冻结UI。我对此进行了更深入的解释

最终,在Swing中进行任何类型的绘画时,您仍将做两件事:

替代paintComponent以进行绘图。 根据需要调用“重新绘制”以请求使图形可见。Swing通常仅在需要时重新绘制,例如当其他程序的窗口经过Swing组件顶部时。 如果你在做这两件事,你可能是做对了。Swing实际上没有用于动画的高级API。它的设计主要考虑了图形用户界面组件。它当然可以做一些好的事情,但是您必须像现在这样从头开始编写组件

如果没有书签,则会覆盖一些“幕后”内容

您可以查看JavaFX。我个人不太了解它,但它应该更倾向于动画

作为某种程度上的优化,可以做的一件事是在一个单独的图像上绘制 然后将图像绘制到paintComponent中的面板上。如果绘制时间较长,这一点尤其有用:系统可以安排重新绘制,以便在更可控的情况下进行重新绘制

如果不绘制图像,则需要使用对象构建模型,并每次在paintComponent内绘制所有对象

以下是绘制图像的示例:

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

/**
 * Holding left-click draws, and
 * right-clicking cycles the color.
 */
class PaintAnyTime {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PaintAnyTime();
            }
        });
    }

    Color[]    colors = {Color.red, Color.blue, Color.black};
    int  currentColor = 0;
    BufferedImage img = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB);
    Graphics2D  imgG2 = img.createGraphics();

    JFrame frame = new JFrame("Paint Any Time");
    JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Creating a copy of the Graphics
            // so any reconfiguration we do on
            // it doesn't interfere with what
            // Swing is doing.
            Graphics2D g2 = (Graphics2D) g.create();
            // Drawing the image.
            int w = img.getWidth();
            int h = img.getHeight();
            g2.drawImage(img, 0, 0, w, h, null);
            // Drawing a swatch.
            Color color = colors[currentColor];
            g2.setColor(color);
            g2.fillRect(0, 0, 16, 16);
            g2.setColor(Color.black);
            g2.drawRect(-1, -1, 17, 17);
            // At the end, we dispose the
            // Graphics copy we've created
            g2.dispose();
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(img.getWidth(), img.getHeight());
        }
    };

    MouseAdapter drawer = new MouseAdapter() {
        boolean rButtonDown;
        Point prev;

        @Override
        public void mousePressed(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                prev = e.getPoint();
            }
            if (SwingUtilities.isRightMouseButton(e) && !rButtonDown) {
                // (This just behaves a little better
                // than using the mouseClicked event.)
                rButtonDown  = true;
                currentColor = (currentColor + 1) % colors.length;
                panel.repaint();
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (prev != null) {
                Point  next = e.getPoint();
                Color color = colors[currentColor];
                // We can safely paint to the
                // image any time we want to.
                imgG2.setColor(color);
                imgG2.drawLine(prev.x, prev.y, next.x, next.y);
                // We just need to repaint the
                // panel to make sure the
                // changes are visible
                // immediately.
                panel.repaint();
                prev = next;
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (SwingUtilities.isLeftMouseButton(e)) {
                prev = null;
            }
            if (SwingUtilities.isRightMouseButton(e)) {
                rButtonDown = false;
            }
        }
    };

    PaintAnyTime() {
        // RenderingHints let you specify
        // options such as antialiasing.
        imgG2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        imgG2.setStroke(new BasicStroke(3));
        //
        panel.setBackground(Color.white);
        panel.addMouseListener(drawer);
        panel.addMouseMotionListener(drawer);
        Cursor cursor =
            Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR);
        panel.setCursor(cursor);
        frame.setContentPane(panel);
        frame.pack();
        frame.setResizable(false);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
如果例程是长时间运行的,并且重绘可能同时发生,那么也可以使用双缓冲。绘图是对一个独立于所示图像的图像进行的。然后,绘制例程完成后,图像参照将交换,以便无缝更新

例如,您通常应该在游戏中使用双缓冲。双缓冲防止图像以部分状态显示。例如,如果在游戏循环中使用背景线程而不是计时器,并且在游戏进行绘制时重新绘制,则可能会发生这种情况。如果没有双缓冲,这种情况将导致闪烁或撕裂

默认情况下,Swing组件是双缓冲的,因此,如果所有图形都发生在EDT上,则无需自己编写双缓冲逻辑。Swing已经做到了

下面是一个稍微复杂一些的示例,它显示了一个长时间运行的任务和一个缓冲区交换:

import java.awt.*;
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;

/**
 * Left-click to spawn a new background
 * painting task.
 */
class DoubleBuffer {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DoubleBuffer();
            }
        });
    }

    final int  width = 640;
    final int height = 480;

    BufferedImage createCompatibleImage() {
        GraphicsConfiguration gc =
            GraphicsEnvironment
                .getLocalGraphicsEnvironment()
                .getDefaultScreenDevice()
                .getDefaultConfiguration();
        // createCompatibleImage creates an image that is
        // optimized for the display device.
        // See http://docs.oracle.com/javase/8/docs/api/java/awt/GraphicsConfiguration.html#createCompatibleImage-int-int-int-
        return gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    }

    // The front image is the one which is
    // displayed in the panel.
    BufferedImage front = createCompatibleImage();
    // The back image is the one that gets
    // painted to.
    BufferedImage  back = createCompatibleImage();
    boolean  isPainting = false;

    final JFrame frame = new JFrame("Double Buffer");
    final JPanel panel = new JPanel() {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            // Scaling the image to fit the panel.
            Dimension actualSize = getSize();
            int w = actualSize.width;
            int h = actualSize.height;
            g.drawImage(front, 0, 0, w, h, null);
        }
    };

    final MouseAdapter onClick = new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if (!isPainting) {
                isPainting = true;
                new PaintTask(e.getPoint()).execute();
            }
        }
    };

    DoubleBuffer() {
        panel.setPreferredSize(new Dimension(width, height));
        panel.setBackground(Color.WHITE);
        panel.addMouseListener(onClick);
        frame.setContentPane(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    void swap() {
        BufferedImage temp = front;
        front = back;
        back = temp;
    }

    class PaintTask extends SwingWorker<Void, Void> {
        final Point pt;

        PaintTask(Point pt) {
            this.pt = pt;
        }

        @Override
        public Void doInBackground() {
            Random rand = new Random();

            synchronized(DoubleBuffer.this) {
                Graphics2D g2 = back.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
                                    RenderingHints.VALUE_STROKE_PURE);
                g2.setBackground(new Color(0, true));
                g2.clearRect(0, 0, width, height);
                // (This computes pow(2, rand.nextInt(3) + 7).)
                int  depth = 1 << ( rand.nextInt(3) + 7 );
                float  hue = rand.nextInt(depth);
                int radius = 1;
                int c;
                // This loop just draws concentric circles,
                // starting from the inside and extending
                // outwards until it hits the outside of
                // the image.
                do {
                    int rgb = Color.HSBtoRGB(hue / depth, 1, 1);
                    g2.setColor(new Color(rgb));

                    int x = pt.x - radius;
                    int y = pt.y - radius;
                    int d = radius * 2;

                    g2.drawOval(x, y, d, d);

                    ++radius;
                    ++hue;
                    c = (int) (radius * Math.cos(Math.PI / 4));
                } while (
                       (0 <= pt.x - c) || (pt.x + c < width)
                    || (0 <= pt.y - c) || (pt.y + c < height)
                );

                g2.dispose();
                back.flush();

                return (Void) null;
            }
        }

        @Override
        public void done() {
            // done() is completed on the EDT,
            // so for this small program, this
            // is the only place where synchronization
            // is necessary.
            // paintComponent will see the swap
            // happen the next time it is called.
            synchronized(DoubleBuffer.this) {
                swap();
            }

            isPainting = false;
            panel.repaint();
        }
    }
}
绘制例程只是为了绘制垃圾,这需要很长时间:


对于紧密耦合的模拟,javax.swing.Timer是一个不错的选择。让计时器的侦听器调用您的paintComponent实现,如图所示和引用的示例所示

对于松散耦合的模拟,让模型在SwingWorker的后台线程中演化,如图所示。在适当的时候调用publish


选择部分取决于模拟的性质和模型的占空比。

对于紧密耦合的模拟,javax.swing.Timer是一个不错的选择。让计时器的侦听器调用您的paintComponent实现,如图所示和引用的示例所示

对于松散耦合的模拟,让模型在SwingWorker的后台线程中演化,如图所示。在适当的时候调用publish


选择在一定程度上取决于模拟的性质和模型的占空比。

为什么不直接使用测试床上的东西呢?它已经做了一切。只需绘制JPanel、控制器和调试图。它使用Java 2D绘图

请参见此处,了解执行缓冲渲染的JPanel:

下面是调试图:

查看TestbedMain.java文件,了解如何启动普通测试床,并删除您不需要的内容:

编辑: 免责声明:我维护jbox2d


以下是测试床框架的软件包:

TestbedMain.java位于j2d文件夹中,如下所示:
为什么不直接使用测试床上的东西呢?它已经做了一切。只需绘制JPanel、控制器和调试图。它使用Java 2D绘图

请参见此处,了解执行缓冲渲染的JPanel:

下面是调试图:

查看TestbedMain.java文件,了解如何启动普通测试床,并删除您不需要的内容:

编辑: 免责声明:我维护jbox2d


以下是测试床框架的软件包:

TestbedMain.java位于j2d文件夹中,如下所示:

我现在不会研究JavaFX,因为我的时间不够了,但谢谢。我正在做这两件事。此外,每次更新模拟时都应该重新绘制窗口,所以现在我可以不使用计时器。这非常慢,除非你手动进行双缓冲,FYI@DanielMurphy当然可以进行优化,但这不是问题所在。请注意,默认情况下,所有Swing组件都是双缓冲的,因此更优雅的优化是对图像进行自定义图像绘制,并在重新绘制时在面板上绘制图像。没有必要增加两倍缓冲区。实际上,这正是我的意思。图像就是缓冲区:我用一些代码示例更新了我的答案,展示了@DanielMurphy和我简要讨论的内容。第二个是更优化的,与使用现有库相比,这确实可能被认为是乏味的。我现在不会研究JavaFX,因为我的时间不够,但谢谢。我正在做这两件事。此外,每次更新模拟时都应该重新绘制窗口,所以现在我可以不使用计时器。这非常慢,除非你手动进行双缓冲,FYI@DanielMurphy当然可以进行优化,但这不是问题所在。请注意,默认情况下,所有Swing组件都是双缓冲的,因此更优雅的优化是对图像进行自定义图像绘制,并在重新绘制时在面板上绘制图像。没有必要增加两倍缓冲区。实际上,这正是我的意思。图像就是缓冲区:我用一些代码示例更新了我的答案,显示了@Da的内容
尼尔墨菲和我简单地讨论了一下。第二个是更优化的,与使用现有的库相比,这确实可能被认为是乏味的。通常当你推广你所属的东西时,指出你的附属关系是一种很好的礼仪。哇,如果它真的有效,那就太棒了,谢谢!你能提供一些关于我应该如何使用这个代码的更多信息吗?也许是一个简单的例子?我找不到TestbedMain.java。这是测试床框架的包:TestbedMain.java在j2d文件夹中,这里:通常当你在推广你所属的东西时,指出你的从属关系是一种很好的礼仪。哇,如果它真的有效,那将是非常棒的,谢谢!你能提供一些关于我应该如何使用这个代码的更多信息吗?也许是一个简单的例子?我找不到TestbedMain.java。这是testbed框架的包:TestbedMain.java在j2d文件夹中,这里: