这是多态的一个例子吗?

时间:2011-02-11 10:44:34

标签: java

下面的代码是多态的一个例子吗?谢谢

    /**
     * Create a waveform according to audio signal intensity
     */
    class WavPanel extends JPanel {


        List<Byte> audioBytes;
        List<Line2D.Double> lines;


        public WavPanel() {
            super();
            setBackground(Color.black);
            resetWaveform();
        }


        public void resetWaveform() {
            audioBytes = new ArrayList<Byte>();
            lines = new ArrayList<Line2D.Double>();
            repaint();
        }


        public void addAudioByte(byte b) {
            audioBytes.add(b);
        }


        public void createWaveForm() {

            if (audioBytes.size() == 0) {
                return;
            }

            AudioFormat format = audioInputStream.getFormat();


            Dimension d = getSize();
            int w = d.width;
            int h = d.height - 15;


            int[] audioData = null;


            if (format.getSampleSizeInBits() == 16) {
                int nlengthInSamples = audioBytes.size() / 2;
                audioData = new int[nlengthInSamples];

                if (format.isBigEndian()) {
                    for (int i = 0; i < nlengthInSamples; i++) {

                        int MSB = (int) audioBytes.get(2 * i);

                        int LSB = (int) audioBytes.get(2 * i + 1);

                        audioData[i] = MSB << 8 | (255 & LSB);
                    }
                } else {
                    for (int i = 0; i < nlengthInSamples; i++) {
                        int LSB = (int) audioBytes.get(2 * i);
                        int MSB = (int) audioBytes.get(2 * i + 1);
                        audioData[i] = MSB << 8 | (255 & LSB);
                    }
                }
            } else if (format.getSampleSizeInBits() == 8) {
                int nlengthInSamples = audioBytes.size();
                audioData = new int[nlengthInSamples];

                if (format.getEncoding().toString().startsWith("PCM_SIGN")) {
                    for (int i = 0; i < audioBytes.size(); i++) {
                        audioData[i] = audioBytes.get(i);
                    }
                } else {
                    for (int i = 0; i < audioBytes.size(); i++) {
                        audioData[i] = audioBytes.get(i) - 128;
                    }
                }
            }


            int frames_per_pixel = audioBytes.size() / format.getFrameSize() / w;
            byte my_byte = 0;
            double y_last = 0;
            int numChannels = format.getChannels();
            for (double x = 0; x < w && audioData != null; x++) {

                int idx = (int) (frames_per_pixel * numChannels * x);

                if (format.getSampleSizeInBits() == 8) {
                    my_byte = (byte) audioData[idx];
                } else {

                    my_byte = (byte) (128 * audioData[idx] / 32768);
                }


                double y_new = (double) (h * (128 - my_byte) / 256);
                lines.add(new Line2D.Double(x, y_last, x, y_new));
                y_last = y_new;
            }


            repaint();
        }

        public void paint(Graphics g) {

            Dimension d = getSize();

            g.setColor(getBackground());
            g.fillRect(0, 0, d.width, d.height);

            if (audioBytes.size() == 0) {
                return;
            }


            g.setColor(Color.LIGHT_GRAY);
            for (int i = 1; i < lines.size(); i++) {
                Line2D.Double line = lines.get(i);
                g.drawLine((int) line.x1, (int) line.y1, (int) line.x2, (int) line.y2);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

虽然代码在一些地方使用多态(声明接口类型List的字段,并且具有类型为Graphics的方法参数,其将在practive中传递子类实例),代码绝不是多态的示例,因为这些是次要细节,与代码的主要关注点无关,即解析和显示音频数据。

答案 1 :(得分:0)

嗯,它使用多态,因为它重新实现了通过JPanel从JComponent继承的方法paint()。因此,您的WavPanel可以在预期的JPanel(或JComponent或其他基类)的任何地方使用,并且会表现出“多态”paint()行为。

作为多态的一个例子,您的代码需要在派生类中展示不同的实现,例如通过显示一个类层次结构,其中子类重新实现其子类的方法。