Crenox

Java Sound Code

Apr 27th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5. public class Soundplayer extends JApplet implements ActionListener
  6. {
  7. JRadioButton radio1, radio2, radio3;
  8. ButtonGroup group1;
  9. JPanel panel1;
  10. JLabel label1, label2, label3;
  11. JButton button4,button5,button6;
  12. AudioClip clip1,clip2,clip3;
  13.  
  14.  
  15. public void init()
  16. {
  17.  
  18. setSize(400,250);
  19. Container cp;
  20. cp = getContentPane();
  21. cp.setLayout(new GridLayout(1,1));
  22.  
  23.  
  24. radio1 = new JRadioButton();
  25. radio2 = new JRadioButton();
  26. radio3 = new JRadioButton();
  27.  
  28. label1 = new JLabel("Charmander");
  29. label2 = new JLabel("Bulbasaur");
  30. label3 = new JLabel("Squirtle");
  31.  
  32. button4 = new JButton("Play");
  33. button5 = new JButton("Stop");
  34. button6 = new JButton("Loop");
  35.  
  36. clip1 = getAudioClip(getDocumentBase(), "ashchar.wav");
  37. clip2 = getAudioClip(getDocumentBase(), "ashbsaur.wav");
  38. clip3 = getAudioClip(getDocumentBase(), "ashsqrtl.wav");
  39.  
  40.  
  41.  
  42.  
  43. ButtonGroup group1 = new ButtonGroup();
  44.  
  45. group1.add(radio1);
  46. group1.add(radio2);
  47. group1.add(radio3);
  48.  
  49. panel1 = new JPanel();
  50.  
  51. cp.add(panel1);
  52.  
  53. panel1.setLayout(null);
  54.  
  55. panel1.add(radio1);
  56. panel1.add(radio2);
  57. panel1.add(radio3);
  58. panel1.add(label1);
  59. panel1.add(label2);
  60. panel1.add(label3);
  61. panel1.add(button4);
  62. panel1.add(button5);
  63. panel1.add(button6);
  64.  
  65. radio1.setBounds(50,50,50,50);
  66. radio2.setBounds(50,100,50,50);
  67. radio3.setBounds(50,150,50,50);
  68. label1.setBounds(100,50,100,50);
  69. label2.setBounds(100,100,100,50);
  70. label3.setBounds(100,150,100,50);
  71. button4.setBounds(200,65,150,20);
  72. button5.setBounds(200,115,150,20);
  73. button6.setBounds(200,165,150,20);
  74.  
  75. radio1.addActionListener(this);
  76. radio2.addActionListener(this);
  77. radio3.addActionListener(this);
  78. button4.addActionListener(this);
  79. button5.addActionListener(this);
  80. button6.addActionListener(this);
  81.  
  82.  
  83.  
  84. }
  85.  
  86. public void actionPerformed (ActionEvent e)
  87. {
  88.  
  89. if (e.getSource() == button4)
  90. {
  91. if(radio1.isSelected())
  92. {
  93. clip1.play();
  94. }
  95. else if(radio2.isSelected())
  96. {
  97. clip2.play();
  98. }
  99. else if (radio3.isSelected())
  100. {
  101. clip3.play();
  102. }
  103.  
  104. }
  105.  
  106. if (e.getSource() == button5)
  107. {
  108. if(radio1.isSelected())
  109. {
  110. clip1.stop();
  111. }
  112. else if(radio2.isSelected())
  113. {
  114. clip2.stop();
  115. }
  116. else if (radio3.isSelected())
  117. {
  118. clip3.stop();
  119. }
  120.  
  121. }
  122.  
  123. if (e.getSource() == button6)
  124. {
  125. if(radio1.isSelected())
  126. {
  127. clip1.loop();
  128. }
  129. else if(radio2.isSelected())
  130. {
  131. clip2.loop();
  132. }
  133. else if (radio3.isSelected())
  134. {
  135. clip3.loop();
  136. }
  137.  
  138. }
  139.  
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment