3

请看我的代码片段,它有什么问题,当 Swing 计时器统计数据重复绘制在 jpnael 上时,它会冻结 GUI?

class WaveformPanel extends JPanel {

        Timer graphTimer = null;
        AudioInfo helper = null;

        WaveformPanel() {
            setPreferredSize(new Dimension(200, 80));
            setBorder(BorderFactory.createLineBorder(Color.BLACK));
            graphTimer = new Timer(15, new TimerDrawing());
        }

        /**
         * 
         */
        private static final long serialVersionUID = 969991141812736791L;
        protected final Color BACKGROUND_COLOR = Color.white;
        protected final Color REFERENCE_LINE_COLOR = Color.black;
        protected final Color WAVEFORM_COLOR = Color.red;

        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            int lineHeight = getHeight() / 2;
            g.setColor(REFERENCE_LINE_COLOR);
            g.drawLine(0, lineHeight, (int) getWidth(), lineHeight);

            if (helper == null) {
                return;
            }

            drawWaveform(g, helper.getAudio(0));

        }

        protected void drawWaveform(Graphics g, int[] samples) {

            if (samples == null) {
                return;
            }

            int oldX = 0;
            int oldY = (int) (getHeight() / 2);
            int xIndex = 0;

            int increment = helper.getIncrement(helper
                    .getXScaleFactor(getWidth()));
            g.setColor(WAVEFORM_COLOR);

            int t = 0;

            for (t = 0; t < increment; t += increment) {
                g.drawLine(oldX, oldY, xIndex, oldY);
                xIndex++;
                oldX = xIndex;
            }

            for (; t < samples.length; t += increment) {
                double scaleFactor = helper.getYScaleFactor(getHeight());
                double scaledSample = samples[t] * scaleFactor;
                int y = (int) ((getHeight() / 2) - (scaledSample));
                g.drawLine(oldX, oldY, xIndex, y);

                xIndex++;
                oldX = xIndex;
                oldY = y;
            }
        }

        public void setAnimation(boolean turnon) {
            if (turnon) {
                graphTimer.start();
            } else {
                graphTimer.stop();
            }
        }

        class TimerDrawing implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent e) {

                byte[] bytes = captureThread.getTempBuffer();

                if (helper != null) {
                    helper.setBytes(bytes);
                } else {
                    helper = new AudioInfo(bytes);
                }
                repaint();
            }
        }

    }

我从其父类调用 WaveFormPanel 的 setAnimation。当动画开始时,它不会绘制任何东西,而是冻结。请给我解决方案。

谢谢你米希尔帕雷克

4

2 回答 2

4

中的java.swingx.Timer调用。那么问题是,什么需要时间来渲染。可能是对它的调用可能是帮助的构建,但我怀疑这只是您尝试绘制的数据的共享量。ActionPerformedEDTcaptureThread.getTempBuffer

最近玩了这个,处理波形需要相当多的时间。

一个建议可能是减少您绘制的样本数量。而不是绘制每一个,可能会根据组件的宽度每隔一秒或四次绘制采样点。你仍然应该得到同样的笑话,但没有所有的工作......

更新

所有样本,2.18 秒

所有样品

每 4 个样本,0.711 秒

在此处输入图像描述

每 8 个样本,0.450 秒

在此处输入图像描述

与其根据计时器进行绘制,不如说您需要根据批量数据进行绘制。

由于您的加载程序线程有一个“块”数据,因此可能会对其进行绘制。

正如 HoverCraftFullOfEels 建议的那样,您可以先将其绘制到 BufferedImage 上,然后再将其绘制到屏幕上......

SwingWorker或许可以为您实现这一目标

更新

这是我用来绘制上述示例的代码。

// Samples is a 2D int array (int[][]), where the first index is the channel, the second is the sample for that channel
if (samples != null) {

    Graphics2D g2d = (Graphics2D) g;

    int length = samples[0].length;

    int width = getWidth() - 1;
    int height = getHeight() - 1;

    int oldX = 0;
    int oldY = height / 2;
    int frame = 0;

    // min, max is the min/max range of the samples, ie the highest and lowest samples
    int range = max + (min * -2);
    float scale = (float) height / (float) range;

    int minY = Math.round(((height / 2) + (min * scale)));
    int maxY = Math.round(((height / 2) + (max * scale)));

    LinearGradientPaint lgp = new LinearGradientPaint(
            new Point2D.Float(0, minY),
            new Point2D.Float(0, maxY),
            new float[]{0f, 0.5f, 1f},
            new Color[]{Color.BLUE, Color.RED, Color.BLUE});
    g2d.setPaint(lgp);
    for (int sample : samples[0]) {

        if (sample % 64 == 0) {

            int x = Math.round(((float) frame / (float) length) * width);
            int y = Math.round((height / 2) + (sample * scale));

            g2d.drawLine(oldX, oldY, x, y);

            oldX = x;
            oldY = y;

        }

        frame++;

    }

}

我使用AudioStream流加载 Wav 文件并生成 2D 样本。

于 2012-08-22T05:25:24.377 回答
3

我猜你的波浪绘图代码,它是从一个paintComponent(...)方法中调用的,它花费的时间比你想象的要长,并且正在捆绑 Swing 绘画和 EDT。

如果这是我的代码,我会考虑将我的波浪绘制到 BufferedImages一次,从这些图像中制作 ImageIcons,然后在我的 Swing Timer 中简单地交换图标。

于 2012-08-22T05:24:16.970 回答