Java applet只是部分绘制

时间:2012-11-02 23:17:22

标签: java drawing buffer render paint

我是Java applet编程的新手,所以请原谅我这是一个非常基本的问题,但是我已经广泛搜索了它,并且只发现了半相关的问题和解决方案。

我正在编写一些几何算法的简单演示,当我repaint()时,只有一些我的图形基元被渲染到屏幕上。每当我的applet重绘时,我的线条和椭圆的一个看似随机的子集被绘制。唯一的模式是 渲染的基元始终是从图形的开头。 I.E,有时会绘制0-2的原语,有时是0-5,有时是整批。

我想指出的是,据我所知,这不是经典的"闪烁"这可以通过双缓冲来解决。根据我的理解,闪烁是指在短时间内您可以在完成渲染之前看到部分渲染的小程序。但是,在我的情况下,如果它没有完成渲染,那么永远不会完成,除非我redraw()再次获得幸运。我尝试过双缓冲:

public void update(Graphics g) {
    Graphics offgc;
    Image offscreen = null;
    Dimension d = size();

    // create the offscreen buffer and associated Graphics
    offscreen = createImage(d.width, d.height);
    offgc = offscreen.getGraphics();
    // clear the exposed area
    offgc.setColor(getBackground());
    offgc.fillRect(0, 0, d.width, d.height);
    offgc.setColor(getForeground());
    // do normal redraw
    paint(offgc);
    // transfer offscreen to window
    g.drawImage(offscreen, 0, 0, this);
}

但它似乎没有任何帮助。如果它有用,可以在这里看看发生了什么。这就是它应该是这样的:

full

但大部分时间看起来像这样:

partial1

或者这个:

partial2

提前致谢!

1 个答案:

答案 0 :(得分:1)

这不是双缓冲应该如何工作,不是油漆过程如何工作。

  • 请勿覆盖update
  • 尽可能覆盖paint顶级容器(如Applet / JApplet / Frame / JFrame)
  • 使用可以渲染的“绘画”面板,最好是JPanel。 Swing组件提供双缓冲支持
  • 应在绘画周期的外侧绘制双缓冲区,并且仅在需要时进行更新,这样可以使整个绘制过程更快,因为您没有不必要地重新渲染内容。
  • 当需要更新缓冲区时,首先渲染到临时缓冲区,这样可以确保在更新时可能发生的任何重新渲染都不会过早地反映回屏幕...

enter image description here

public class TestPaintGeometry {

    public static void main(String[] args) {
        new TestPaintGeometry();
    }

    public TestPaintGeometry() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ShowPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class ShowPane extends JPanel {

        private GeometryPane geoPane;

        public ShowPane() {
            setLayout(new BorderLayout());

            geoPane = new GeometryPane();
            JButton redrew = new JButton("Redraw");
            add(geoPane);
            add(redrew, BorderLayout.SOUTH);
            redrew.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    geoPane.redrew();
                }
            });
        }            
    }

    public class GeometryPane extends JPanel {

        private BufferedImage buffer;

        public void redrew() {
            Path2D.Float path = new Path2D.Float();
            int width = getWidth();
            int height = getHeight();

            int points = Math.max(10, (int) Math.round(Math.random() * 100));
            for (int index = 0; index < points; index++) {
                int x = (int) Math.round(Math.random() * width);
                int y = (int) Math.round(Math.random() * height);
                if (index > 0) {
                    path.lineTo(x, y);
                } else {
                    path.moveTo(x, y);
                }
            }

            BufferedImage tmp = createCompatibleImage(width, height);
            Graphics2D g2d = tmp.createGraphics();
            g2d.setColor(Color.BLACK);
            g2d.draw(path);
            g2d.dispose();

            buffer = tmp;
            repaint();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (buffer != null) {
                int x = (getWidth() - buffer.getWidth()) / 2;
                int y = (getHeight() - buffer.getHeight()) / 2;
                g.drawImage(buffer, x, y, this);
            }
        }
    }

    public static GraphicsConfiguration getGraphicsConfiguration() {
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
    }

    public static BufferedImage createCompatibleImage(int width, int height) {
        return createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    }

    public static BufferedImage createCompatibleImage(int width, int height, int transparency) {
        BufferedImage image = getGraphicsConfiguration().createCompatibleImage(width, height, transparency);
        image.coerceData(true);
        return image;
    }
}

这允许您将GeometryPane部署到JFrameJAppelt,因为它不受其继承的遗留约束......

相关问题