944,007 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 543
  • Java RSS
Apr 7th, 2009
0

Exceptions thrown

Expand Post »
Hello, this code is throwing and IOException and FileNotFoundException and I am having trouble figuring out why. Where exactly does the song need to go in order for the program to pick it up? Also, is this code for putting the song in the program correct?
Thanks!
Java Syntax (Toggle Plain Text)
  1. //Hangman a game where you test your word-guessing skills against the worlds smartest computer simulation.
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import javax.swing.*;
  6. import javax.swing.event.*;
  7. import sun.audio.AudioPlayer;
  8. import sun.audio.AudioStream;
  9. import sun.audio.AudioData;
  10. import java.util.Random;
  11. import java.util.Locale;
  12. import java.text.*;
  13. import javax.swing.plaf.metal.*;
  14.  
  15.  
  16. public class hangman {
  17. public static void main(String[] args)
  18. {
  19.  
  20. EventQueue.invokeLater(
  21. new Runnable()
  22. {
  23. public void run()
  24. {
  25. hangmanframe frame = new hangmanframe();
  26. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  27. frame.setVisible(true);
  28. try{
  29. UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
  30.  
  31. }
  32. catch (ClassNotFoundException e) {
  33. }
  34.  
  35. catch (UnsupportedLookAndFeelException e) {
  36.  
  37. }
  38.  
  39. catch (Exception e) {
  40.  
  41. }
  42.  
  43.  
  44. }
  45. });
  46. }
  47.  
  48. }
  49. class hangmanframe extends JFrame
  50. {
  51. public hangmanframe()
  52. {
  53. this.setSize(1282, 772);
  54. this.setTitle("Hangman");
  55.  
  56. JMenuBar greenMenuBar = new JMenuBar();
  57. greenMenuBar.setOpaque(true);
  58. greenMenuBar.setBackground(new Color(154, 165, 127));
  59. greenMenuBar.setPreferredSize(new Dimension(200, 20));
  60. JLabel blueLabel = new JLabel();
  61. blueLabel.setPreferredSize(new Dimension(200, 180));
  62. this.setVisible(true);
  63. this.setJMenuBar(greenMenuBar);
  64. this.getContentPane().add(blueLabel, BorderLayout.CENTER);
  65. this.pack();
  66. GUI panel = new GUI(greenMenuBar);
  67. this.add(panel);
  68. }
  69. }
  70. class GUI extends JPanel
  71. {
  72. public GUI(JMenuBar menu)
  73. {
  74. Random generator = new Random();
  75. NumberFormat moneyFormat = NumberFormat.getCurrencyInstance(Locale.US);
  76. int generator1;
  77. generator1 = generator.nextInt(20)+1;
  78. array = new String[20];
  79. array[0] = "Fascist";
  80. array[1] = "Dictator";
  81. array[2] = "Aligator";
  82. array[3] = "Watermelon";
  83. array[4] = "Impinge";
  84. array[5] = "Strenuous";
  85. array[6] = "Condescending";
  86. array[7] = "Allocate";
  87. array[8] = "Rhetoric";
  88. array[9] = "Elementary";
  89. array[10] = "Rocketship";
  90. array[11] = "Laminate";
  91. array[12] = "Exuberance";
  92. array[13] = "Vivacious";
  93. array[14] = "Dismemberment";
  94. array[15] = "Pat Sagack";
  95. array[16] = "Double Money";
  96. array[17] = "Watergate";
  97. array[18] = "Utopia";
  98. array[19] = "Indent";
  99. int i = 0;
  100. String lines = "";
  101. while(i < array[generator1].length())
  102. {
  103. lines += "-";
  104. i++;
  105. }
  106. JLabel line = new JLabel(lines);
  107. JLabel word = new JLabel(array[generator1]);
  108. JMenu helpMenu = new JMenu("Help");
  109. JMenuItem instructions = new JMenuItem("Instructions");
  110. panel = new JPanel(new BorderLayout());
  111. menu.add(helpMenu);
  112. helpMenu.add(instructions);
  113. instructions.addActionListener(new helpButtonAction());
  114. //JLabel name = new JLabel("hi");
  115. //panel.add(name, BorderLayout.SOUTH);
  116.  
  117. JButton hangmanButton = new JButton("Good Day");
  118. panel.add(hangmanButton, BorderLayout.SOUTH);
  119. hangmanButton.addActionListener(new guessButtonAction());
  120. waterfall = new ImageIcon("C:/Program Files/jungle_mountains.jpg");
  121. this.add(panel);
  122. repaint();
  123. panel.add(word, BorderLayout.CENTER);
  124. panel.add(line, BorderLayout.NORTH);
  125. this.add(panel);
  126.  
  127. AudioPlayer myBackgroundPlayer = AudioPlayer.player;
  128. AudioStream myBackgroundMusic;
  129. AudioData myData;
  130. AudioStream myLoop;
  131. //use a try block in case the file doesnt exist.
  132. try {
  133. myBackgroundMusic = new AudioStream(new FileInputStream("youtube - gift of gab - way of the light.wav"));
  134. myData = myBackgroundMusic.getData();
  135. //myLoop = new ContinuousAudioDataStream(myData);
  136. }
  137. catch(IOException error) {}
  138.  
  139. //play background music.
  140. //myBackgroundPlayer.start(myLoop);
  141.  
  142. repaint();
  143. }
  144. public void paintComponent( Graphics g )
  145. {
  146. // should be the FIRST line in a paintComponent method,
  147. // calling its superclass' version of this method!
  148. super.paintComponent( g );
  149. waterfall.paintIcon(panel, g, 0, 0);
  150.  
  151. } // e
  152. private class guessButtonAction implements ActionListener
  153. {
  154. public void actionPerformed(ActionEvent event)
  155. {
  156. JOptionPane.showMessageDialog(null, "Hang Man");
  157.  
  158. }
  159. }
  160. private class helpButtonAction implements ActionListener
  161. {
  162.  
  163. public void actionPerformed(ActionEvent event)
  164.  
  165. {
  166.  
  167. panel.remove(hangmanButton);
  168. JLabel label = new JLabel("Welcome to Hangman; a game that tests your skills, not in a numerical way, but in a alphabetical way. The rules are simple, but can be altered in your brain but the premise of the game is easy. \nYou start off by clicking on the start game button. Then you are transported to a new area in the game, the game-area. One you are there you will guess letters according to how much of the hangman has decreased. On that note every time you guess the wrong letter one body part will be taken away from the hangman.");
  169. panel.add(label, BorderLayout.EAST);
  170. JPanel helpPanel = new JPanel();
  171. helpPanel.setLayout (new BorderLayout());
  172.  
  173.  
  174. }
  175. }
  176.  
  177. ImageIcon waterfall;
  178. JButton hangmanButton;
  179. JPanel panel;
  180. String array[];
  181. }
Similar Threads
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 7th, 2009
0

Re: Exceptions thrown

Where exactly do you get those exceptions. A FileNotFoundException means exactly that; the file you are trying to access doesn't exist.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Apr 7th, 2009
0

Re: Exceptions thrown

it was thrown here: myBackgroundMusic = new AudioStream(new FileInputStream("youtube - gift of gab - way of the light.wav"));
myData = myBackgroundMusic.getData();
myLoop = new ContinuousAudioDataStream(myData);

But the file does exist, but I think maybe I'm not putting it in the right place. Where does it need to be put?
Also, what is an IOException?
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008
Apr 7th, 2009
0

Re: Exceptions thrown

it was thrown here: myBackgroundMusic = new AudioStream(new FileInputStream("youtube - gift of gab - way of the light.wav"));
myData = myBackgroundMusic.getData();
myLoop = new ContinuousAudioDataStream(myData);

But the file does exist, but I think maybe I'm not putting it in the right place. Where does it need to be put?
Also, what is an IOException?
IOException -> Input-OutputException.

since I don't know the AudioStream class, and since you don't specify what packages you're using, I don't really know where it's thrown.
Reputation Points: 935
Solved Threads: 356
Posting Maven
stultuske is online now Online
2,508 posts
since Jan 2007
Apr 7th, 2009
0

Re: Exceptions thrown

Try using the whole path of the file
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Apr 7th, 2009
0

Re: Exceptions thrown

These are the packages:
sun.audio.AudioPlayer;
sun.audio.AudioStream;
sun.audio.AudioData;

Thanks for your help!
Reputation Points: 20
Solved Threads: 1
Junior Poster
christiangirl is offline Offline
108 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to write a customize JFrame?
Next Thread in Java Forum Timeline: changing jframe's icon





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC