import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Example extends JFrame {
public static void main(String[] args) {
// First, get the data from a sound file in .wav format into your program
// You will have to modify the following line to point to your own file
// String fileName = "C:\\Huhns\\Teaching\\CSCE145\\Code\\Noise3\\preamble.wav";
String fileName = "TokyoNena.wav";
Sound sound1 = new Sound(fileName);
// Next, print the sound to find out its length in samples
System.out.println(sound1);
// The following two method calls get the value of a sound sample at
// index 1000 and then set its value to half of the original.
int index = 1000;
int value = sound1.getSampleValueAt(index);
sound1.setSampleValueAt(index, value/2);//THIS IS THE Y
// The following loop sets every sound sample to be twice its original value
for (int n=0; n < sound1.getNumSamples()-21; n++){
for(int i=1;i<21;i++){
value = value + sound1.getSampleValueAt(n);
}
value = value/21;
sound1.setSampleValueAt(n, value * 2);
}
sound1.play();
JFrame f = new JFrame("Soundwave");
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for(int k =0; k<sound1.getNumSamples();++k){
Point[] node = new Point[sound1.getNumSamples()];
int y = sound1.getSampleValueAt(index);
node [k] = new Point(k,y);
f.drawLine(k,y,15,15);
}
// Listen to the sound
}
}
Drawing a SoundWave line
Page 1 of 111 Replies - 1404 Views - Last Post: 09 November 2011 - 08:37 PM
#1
Drawing a SoundWave line
Posted 07 November 2011 - 02:08 PM
OK so as my title says I am trying to draw a SoundWave line, but am having trouble calling the built in drawLine() at line 47
Replies To: Drawing a SoundWave line
#2
Re: Drawing a SoundWave line
Posted 07 November 2011 - 02:21 PM
It's because a JFrame has no method drawLine(), its the Graphics object that contains these methods.
You should extend JFrame and override the paint() or paintComponent() methods which have this signature -
As you can see you now have access to a Graphics object that will allow you to use the .drawLine() method to draw on your JFrame. You should however probably draw on a panel instead of directly on the JFrame, however it all follows the same principles.
You should extend JFrame and override the paint() or paintComponent() methods which have this signature -
public void paintComponent(Graphics g){
// do something with g
}
As you can see you now have access to a Graphics object that will allow you to use the .drawLine() method to draw on your JFrame. You should however probably draw on a panel instead of directly on the JFrame, however it all follows the same principles.
#3
Re: Drawing a SoundWave line
Posted 07 November 2011 - 02:35 PM
Could you elaborate that somemore? I don't understand what you mean by that.
I added this but I don't what I need to add in the
I have added this in order to create the beginning graph
I added this but I don't what I need to add in the
public void paint(Graphics g){
}
I have added this in order to create the beginning graph
public void GraphDisplay(){
setSize(400,400);
setTitle("SoundWave");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
This post has been edited by laxman2809: 07 November 2011 - 02:36 PM
#4
Re: Drawing a SoundWave line
Posted 07 November 2011 - 02:40 PM
Well to me your existing code seems a bit backwards. You are extending JFrame, but in your main method your are creating a completely separate JFrame.
In the paint method you use the Graphics object along with the many many drawing methods to do whatever drawing you want to accomplish -
In the paint method you use the Graphics object along with the many many drawing methods to do whatever drawing you want to accomplish -
g.drawLine(...);
#5
Re: Drawing a SoundWave line
Posted 07 November 2011 - 09:23 PM
The Graphics class also has a drawPolyline() method
http://download.orac...rawPolyline(int[], int[], int)
might be easier to use that one
http://download.orac...rawPolyline(int[], int[], int)
might be easier to use that one
#6
Re: Drawing a SoundWave line
Posted 08 November 2011 - 06:24 AM
here is my code so far but now I can't get any Y values to appear
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class Example extends JFrame {
public Example(){
super ("Graph Display");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
static int[] x1 = new int[100];
static int[] y1 = new int[100];
static int[] x2 = new int[100];
static int[] y2 = new int[100];
public void paint(Graphics g){
// sound 1
g.setColor(Color.MAGENTA);
for (int i = 0; i < 100; i++){
g.fillOval(x1[i],y1[i], 2,2);
System.out.println(x1[i] + " " + y1[i]);}
//sound 2
g.setColor(Color.CYAN);
for (int i = 0; i < 100; i++){
g.drawLine(x2[i],y2[i], 2,2);
System.out.println(x2[i] + " " + y2[i]);}
}
public static void main(String[] args) {
// First, get the data from a sound file in .wav format into your program
// You will have to modify the following line to point to your own file
// String fileName = "C:\\Huhns\\Teaching\\CSCE145\\Code\\Noise3\\preamble.wav";
String fileName = "TokyoNena.wav";
Sound sound1 = new Sound(fileName);
Sound sound2 = null;
// Next, print the sound to find out its length in samples
System.out.println(sound1);
// The following two method calls get the value of a sound sample at
// index 1000 and then set its value to half of the original.
int index = 1000;
int value = sound1.getSampleValueAt(index);
sound1.setSampleValueAt(index, value/2);//THIS IS THE Y
// The following loop sets every sound sample to be twice its original value
for (int n=0; n < sound1.getNumSamples()-21; n++){
for(int i=1;i<21;i++){
value = value + sound1.getSampleValueAt(n);
}
value = value/21;
sound1.setSampleValueAt(n, value * 2);
}
//sound 1
for (int i = 0; i < 100; i++){ // i is the x coordinate
int[] value1= new int[100];
x1[i] = i;
y1[i] = value1[i];
}
sound1.play();
//sound2.play();
//sound 2
int[] value2 = new int[100];
for (int i = 0; i < 99; i++){ // i is the x coordinate
value2[i] = sound1.getSampleValueAt(i); // value is the y coordinate
x2[i] = i;
y2[i] = value2[i];
}
//Produce graph
Example a = new Example();
a.setVisible(true);
}
}
#7
Re: Drawing a SoundWave line
Posted 08 November 2011 - 06:34 AM
I would like to take a look at the program, but I can't seem to compile that, can you? I get errors saying I can't instantiate the type sound?
#8
Re: Drawing a SoundWave line
Posted 08 November 2011 - 06:40 AM
Yes I can but I have the sound file and classes that for it so you would need all of that if you'd like that I can provide it it will just be a lot of code
#9
Re: Drawing a SoundWave line
Posted 08 November 2011 - 07:00 AM
Ah, I thought you were talking about a sound class which I imported. Sorry I can't be of help. The only advice I can give, if you haven't already tried is to print out a line with the value of the sound sample and see if they are what you expect.
#10
Re: Drawing a SoundWave line
Posted 08 November 2011 - 10:23 AM
One thing for sure
for (int i = 0; i < 100; i++){ // i is the x coordinate
int[] value1= new int[100];
x1[i] = i;
y1[i] = value1[i]; // y[0] through y[99] values will all be 0
}
#11
Re: Drawing a SoundWave line
Posted 09 November 2011 - 01:51 AM
Do not override paint() if you use Swing (what you do). Only override paintComponent()
Reasons are explained very well in this article: http://java.sun.com/...icles/painting/
Edit: Quote from the article
Reasons are explained very well in this article: http://java.sun.com/...icles/painting/
Edit: Quote from the article
Quote
The rules that apply to AWT's lightweight components also apply to Swing components -- for instance, paint() gets called when it's time to render -- except that Swing further factors the paint() call into three separate methods, which are invoked in the following order:
protected void paintComponent(Graphics g)
protected void paintBorder(Graphics g)
protected void paintChildren(Graphics g)
Swing programs should override paintComponent() instead of overriding paint(). Although the API allows it, there is generally no reason to override paintBorder() or paintComponents() (and if you do, make sure you know what you're doing!). This factoring makes it easier for programs to override only the portion of the painting which they need to extend. For example, this solves the AWT problem mentioned previously where a failure to invoke super.paint() prevented any lightweight children from appearing.
protected void paintComponent(Graphics g)
protected void paintBorder(Graphics g)
protected void paintChildren(Graphics g)
Swing programs should override paintComponent() instead of overriding paint(). Although the API allows it, there is generally no reason to override paintBorder() or paintComponents() (and if you do, make sure you know what you're doing!). This factoring makes it easier for programs to override only the portion of the painting which they need to extend. For example, this solves the AWT problem mentioned previously where a failure to invoke super.paint() prevented any lightweight children from appearing.
This post has been edited by Veitch: 09 November 2011 - 01:54 AM
#12
Re: Drawing a SoundWave line
Posted 09 November 2011 - 08:37 PM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|