Jump to content
  • Advertisement
Sign in to follow this  
99rock99

[Java] Transverse wave superposition

This topic is 2603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a program that has two sine waves, one on top of the other and a third which shows the resultant superposition of the sine wave at that current time. So far I have all sine waves printed out and they animate. The third one also prints out. I am just having trouble with the superposition. It seems like no matter what I try it doesn't seem to be correct. Here is my code:






import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class TransverseWaveGraphics extends JPanel implements ActionListener{

private int [] xaxisWave1 = new int [3249];
private int [] yaxisWave1 = new int [3249];
private int [] xaxisWave2 = new int [3249];
private int [] yaxisWave2 = new int [3249];
private int [] xaxisWave3 = new int [3249];
private int [] yaxisWave3 = new int [3249];
private int [] originalyWave1 = new int [3249];
private int [] originalyWave2 = new int [3249];
private int differenceWave1;
private int differenceWave2;
private int averageWaveLength = 0;
private Timer timer = new Timer (50, this);

private int wave1Length = 10, wave2Length = 15, wave3Length;
private int timePassed = 0;


public void TransverseWave (){


repaint();

}

public void paintComponent (Graphics g){
super.paintComponent (g);
//draws waves

for (int i = 0; i < 3249; i++){
xaxisWave1 = (int) Math.round(Math.toRadians((i) * wave1Length * 2.5));
originalyWave1 = (int) (Math.sin(Math.toRadians (i + timePassed)) * -40);
yaxisWave1 = (((int)( (Math.sin(Math.toRadians(i + timePassed)) * -1) * 40) + 50));

}
for (int i = 0; i < 3249; i++){
xaxisWave2 = (int) Math.round(Math.toRadians((i) * wave2Length * 2.5));
originalyWave2 = (int) (Math.sin(Math.toRadians (i))* -40);
yaxisWave2 = (((int)( (Math.sin(Math.toRadians(i + timePassed)) * -1) * 40) + 160));

}
averageWaveLength = (wave1Length + wave2Length) / 2;
for (int i = 0; i < 3249; i++){
xaxisWave3 = (int) Math.round(Math.toRadians((i) * 25));
yaxisWave3 = (originalyWave1 - originalyWave2) + 290;
}
g.drawLine(0, 50, 600, 50);
g.drawLine(0, 160, 600, 160);
g.drawLine(0, 290, 600, 290);
g.drawPolyline (xaxisWave1, yaxisWave1, 3249);
g.drawPolyline (xaxisWave2, yaxisWave2, 3249);
g.drawPolyline (xaxisWave3, yaxisWave3, 3249);
}



public void reDrawGraphics1 (int waveLength1){

wave1Length = waveLength1;
for (int i = 0; i < 3249; i++){
xaxisWave1 = (int) Math.round(Math.toRadians((i) * (wave1Length * 2.5)));
yaxisWave1 = (((int)( (Math.sin(Math.toRadians(i + timePassed)) * -1) * 40) + 50));

}

repaint();
}






public void reDrawGraphics2 (int waveLength2){

wave2Length = waveLength2;
for (int i = 0; i < 3249; i++){
xaxisWave2 = (int) Math.round(Math.toRadians((i) * wave2Length * 2.5));
yaxisWave2 = (((int)( (Math.sin(Math.toRadians(i + timePassed)) * -1) * 40) + 50));

}
repaint();
}





public void actionPerformed(ActionEvent arg0) {
timePassed+= 10;
repaint();

}

private int wave1Speed, wave2Speed;

public void startAnimation(int waveLength1, int waveLength2) {
wave1Speed = waveLength1; wave2Speed = waveLength2;
timer.start ();

}
public void stopanimation (){
timer.stop();

}


}








Could you please correct what I am doing wrong? Thanks




Here is the code to launch the graphics









import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import java.awt.event.*;


public class TransverseWaveProgram extends JPanel implements ActionListener, ChangeListener{

private int waveLength1 = 10;
private int waveLength2 = 15;
private JSlider waveSlider1;
private JSlider waveSlider2;
private TransverseWaveGraphics twg = new TransverseWaveGraphics ();
private JLabel wave1LengthTxt;
private JLabel wave2LengthTxt;


public TransverseWaveProgram (){
JFrame transverseWaveFrame = new JFrame ("Transverse Wave Superposition - Adam");


twg.TransverseWave();
JPanel topPanel = new JPanel ();

JPanel wave1Pane = new JPanel (); wave1Pane.setLayout (new GridLayout (3,1));
JLabel wave1Title = new JLabel ("Wave 1 Length"); wave1Pane.add(wave1Title);
waveSlider1 = new JSlider (5, 15); wave1Pane.add(waveSlider1); waveSlider1.addChangeListener(this); waveSlider1.setMajorTickSpacing(5); waveSlider1.setPaintTicks(true);
wave1LengthTxt = new JLabel ("Length = " + waveLength1); wave1Pane.add(wave1LengthTxt);

JPanel wave2Pane = new JPanel (); wave2Pane.setLayout (new GridLayout (3,1));
JLabel wave2Title = new JLabel ("Wave 2 Length"); wave2Pane.add (wave2Title);
waveSlider2 = new JSlider (10, 20); wave2Pane.add (waveSlider2); waveSlider2.addChangeListener(this); waveSlider2.setMajorTickSpacing(5); waveSlider2.setPaintTicks(true);
wave2LengthTxt = new JLabel ("Length = " + waveLength2); wave2Pane.add (wave2LengthTxt);

JPanel buttonsPane = new JPanel (); buttonsPane.setLayout (new GridLayout (2,1));
JButton startButton = new JButton ("Start"); buttonsPane.add(startButton); startButton.addActionListener(this); startButton.setActionCommand("startButton");
JButton stopButton = new JButton ("Stop"); buttonsPane.add(stopButton); stopButton.addActionListener (this); stopButton.setActionCommand("stopButton");

topPanel.add(wave1Pane); topPanel.add(wave2Pane); topPanel.add(buttonsPane);
transverseWaveFrame.add(topPanel, BorderLayout.NORTH);
transverseWaveFrame.setSize(600, 500);
transverseWaveFrame.setVisible(true);
transverseWaveFrame.setResizable (false);
transverseWaveFrame.add (twg, BorderLayout.CENTER);
}




public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if ("startButton".equals(e.getActionCommand())){
twg.startAnimation (waveLength1, waveLength2);

waveSlider1.setEnabled (false);
waveSlider2.setEnabled (false);
}
if ("stopButton".equals (e.getActionCommand())){
twg.stopanimation();

waveSlider1.setEnabled (true);
waveSlider2.setEnabled (true);
}









}

public static void main (String [] args){
TransverseWaveProgram twp = new TransverseWaveProgram ();
}




@Override
public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
JSlider source = ((JSlider)e.getSource());
String object = source.toString();
if (waveSlider1 == (source)){
waveLength1 = (int)source.getValue();
twg.reDrawGraphics1(waveLength1);
wave1LengthTxt.setText ("Length = " + waveLength1);

}
if (waveSlider2 == (source)){
waveLength2 = (int)source.getValue();
twg.reDrawGraphics2(waveLength2);
wave2LengthTxt.setText ("Length = " + waveLength2);

}

}

}








Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!