11 Replies - 3145 Views - Last Post: 09 November 2011 - 08:37 PM Rate Topic: -----

#1 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

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
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
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Drawing a SoundWave line

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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 -

      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.
Was This Post Helpful? 1
  • +
  • -

#3 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

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
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

Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

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 -

g.drawLine(...);

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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
Was This Post Helpful? 1
  • +
  • -

#6 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

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);
	}

}

Was This Post Helpful? 0
  • +
  • -

#7 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

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?
Was This Post Helpful? 1
  • +
  • -

#8 laxman2809  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 101
  • Joined: 03-September 11

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
Was This Post Helpful? 0
  • +
  • -

#9 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

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.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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  
         }  


Was This Post Helpful? 0
  • +
  • -

#11 Veitch  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 27
  • View blog
  • Posts: 59
  • Joined: 05-November 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

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.

This post has been edited by Veitch: 09 November 2011 - 01:54 AM

Was This Post Helpful? 1
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Drawing a SoundWave line

Posted 09 November 2011 - 08:37 PM

With a JScrollPane it is not always the case :)

http://www.dreaminco...ly/page__st__15
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1