-1

Is the following code an example of polymorphism ? Thank you

    /**
     * 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);
            }
        }
    }
}
MAX
  • 1
  • You should strip your example to the essential structure to make it easier to understand your question. Do I really need to know, your panel is about waveforms? – Tilman Vogel Feb 11 '11 at 10:58
  • [Yes/no questions about an example are not a good fit for this site](http://meta.stackoverflow.com/questions/258630/where-is-the-line-for-yes-no-questions). Answers to such questions are rarely useful to anyone except the original asker. The purpose of this site is to create a useful repository of high quality questions with answers. Instead of asking "is this an example of _X_", ask "what is _X_". What makes you think this code is **not** an example of polymorphism? – Raedwald Feb 26 '16 at 13:17

2 Answers2

2

While the code uses polymorphism in a few places (declaring fields of the interface type List, and having a method parameter of type Graphics, which will in practive be passed a subclass instance), the code is by no means an example of polymorphism, because these are minor details and have little to do with the main concern of the code, which is parsing and displaying audio data.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Well, it uses polymorphism because it reimplements the method paint() inherited from JComponent via JPanel. Thus your WavPanel can be used wherever a JPanel (or JComponent or other base classes) is expected and would exhibit "polymorphic" paint() behaviour.

For being an example of polymorphism, your code would need to exhibit different implementation in derived classes, e.g. by displaying a class hierarchy in which a subclass reimplements a method of its subperclass.

Tilman Vogel
  • 9,337
  • 4
  • 33
  • 32