다음 코드가 다형성의 예입니까? 코드가 몇 군데 (인터페이스 유형 List
의 필드를 선언하고, practive에 서브 클래스의 인스턴스를 전달됩니다 유형 Graphics
하는 방법 매개 변수를 갖는), 코드가에서 다형성을 사용하는 동안 당신에게이 다형성의 예입니까?
/**
* 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);
}
}
}
}
질문을 이해하기 쉽게 예제를 필수 구조로 옮겨야합니다. 내가 정말로 알아야 할 것은, 당신의 패널은 파형에 관한 것입니까? –
[예/예에 대한 질문이이 사이트에 적합하지 않습니다] (http://meta.stackoverflow.com/questions/258630/where-is-the-line-for-yes-no-questions). 이러한 질문에 대한 답변은 원래의 질문자를 제외한 모든 사람에게 거의 도움이되지 않습니다. 이 사이트의 목적은 답변과 함께 고품질 질문의 유용한 저장소를 만드는 것입니다. "이게 _X_의 예"라고 묻는 대신 "what is _X_"을 물어보십시오. 이 코드가 ** 다형성의 예가 아니라고 생각하는 이유는 무엇입니까? – Raedwald