Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need the code to get value from the JTextFields and use it in action performed

ID: 3767378 • Letter: I

Question

I need the code to get value from the JTextFields and use it in action performed. The following code is what I have so far:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.ImageIcon;
import javax.sound.sampled.LineUnavailableException;

public class siggen3_al extends JFrame implements ActionListener
{

JTextField dur;
double Ddur = 0.0;
JTextField amp;
double Damp = 0.0;
JTextField freq;
double Dfreq = 0.0;
JTextArea output=new JTextArea();

public static void main(String[] args)
   {
   siggen3_al n = new siggen3_al();
   }

public siggen3_al()
{
   JPanel top;//picture goes here
   top=new JPanel();
   ImageIcon img = new ImageIcon(getClass().getResource("sgnPic/sigGen.png"));
   JLabel mainLabel = new JLabel(img);
   top.add(mainLabel);
   top.setBackground(Color.BLACK);
   add(top,BorderLayout.NORTH);

   JPanel middle = new JPanel();
JPanel tmp = new JPanel();//to put items on, then put panel on

   tmp= new JPanel();
   JLabel Duration=new JLabel("Duration");
    dur=new JTextField(20);   
   tmp.add(Duration);
   tmp.add(dur);
   middle.add(tmp);
  
   tmp= new JPanel();
   JLabel Amplitude=new JLabel("Amplitude");
    amp=new JTextField(20);
   tmp.add(Amplitude);
   tmp.add(amp);
   middle.add(tmp);
  
   tmp= new JPanel();
   JLabel Frequency=new JLabel("Frequency");
    freq =new JTextField(20);
    tmp.add(Frequency);
    tmp.add(freq);
    middle.add(tmp);
  
   output.setPreferredSize(new Dimension(400,400));
   output.setFont(new Font("Courier",0,14));
   middle.add(output);

   JPanel bottom=new JPanel();
   JButton sine=new JButton("Sine");
   JButton square=new JButton("Square");
   JButton sawtooth=new JButton("Sawtooth");
   JButton triangle=new JButton("Triangle");
   JButton generate=new JButton("Generate");
   JButton quit=new JButton("Quit");

   bottom.add(sine);
   bottom.add(square);
   bottom.add(sawtooth);
   bottom.add(triangle);
   bottom.add(generate);
   bottom.add(quit);

   sine.addActionListener(this);
   square.addActionListener(this);
   sawtooth.addActionListener(this);
   triangle.addActionListener(this);
   generate.addActionListener(this);
   quit.addActionListener(this);

   add(middle,BorderLayout.CENTER);//jlabels and jfields
   add(bottom,BorderLayout.SOUTH);//buttons
  
   setTitle("Generator");
   setDefaultCloseOperation(EXIT_ON_CLOSE);//cleans out the memory
   setSize(700,500);
   setLocationRelativeTo(null);//if I place location anywhere else it does not center it
   setVisible(true);  
}

public void actionPerformed(ActionEvent e)
{
int f,d,a;
String b=e.getActionCommand();
if (b.equals("Quit")) {System.exit(0);}

oscillate3_al osc=new oscillate3_al();

try {f = Integer.parseInt(freq.getText());} catch(Exception z) {return; }
try {d = Integer.parseInt(dur.getText());} catch(Exception y) {return; }
try {a = Integer.parseInt(amp.getText());} catch(Exception q) {return; }

output.append(String.format("%10.5d Hz %10.5d s %10.5d AM ",f,d,a));
try
{
osc.generate(f,d,a);
}
catch(LineUnavailableException x){}
}

//IT GOES TOGETHER WITH THE FOLLOWING OSCILLATE CLASS:

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.LineUnavailableException;

public class oscillate3_al
{

public static void main(String[] args){
   oscillate3_al e = new oscillate3_al();
   try { e.generate( 440,1,100); } catch (LineUnavailableException lue) { }
}

public oscillate3_al() { }

public void generate(int Hertz, int duration, int loudness ) throws LineUnavailableException {
int k = 0;
double rate = 44100.0;
   int nSamples = 44100;
AudioFormat audioF;

   double volume;
   volume = 128 * loudness / 100.0;
audioF = new AudioFormat((float)rate,8,1,true,false);
byte[] buff = new byte[44190*duration];
nSamples = 44100 * duration;
   int seconds = nSamples * duration;
   double fudge = Math.PI * 2.0F;
   double time = 0.0;
   double Previous = 0.0;  
   int count=0;
   double widthOfCycle = (double)1.0/(double)(1.0 * Hertz);   
   double widthOfSampling = (double)1.0 / rate;
   double angle=0.0;
   double x = 0.0;

for (int i=0; i<= nSamples ; i++)
   {
       time = widthOfSampling * i;
  
       angle = fudge * time/widthOfCycle;
       double q = Math.sin(angle);
       double amp = q * volume;
       int a = (int) amp;

buff[i] = (byte)a;
   }

SourceDataLine sourceDL = null;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
System.out.printf("There are %d mixers ",mixerInfo.length);

sourceDL = AudioSystem.getSourceDataLine(audioF);

System.out.printf("sourceDataLine = %s ",sourceDL);
sourceDL.open(audioF);
sourceDL.start();

sourceDL.write(buff,0,nSamples);
sourceDL.drain();
sourceDL.stop();
sourceDL.close();
}

I will appreciate a fast answer. I will eventually work with each button to create a wave and make one special wave with a new formula. It is giving e errors I do not even understand. Weird!

Explanation / Answer

* First we declare JTextField like this

JTextField testField = new JTextField(10);

* We can get textfield value in String like this on any button click event.

button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae){
String getValue = testField.getText()
}
})