2017-01-08 7 views
-1

次のJavaコード(Jarファイルに保存されている)はコンピュータ上で動作しますが、別のコンピュータに展開すると、別のホストにjarを配備すると音声/画像が表示されない

アイデア?

import java.awt.Color; 
import java.awt.Font; 
import java.util.*; 
import java.io.File; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.swing.*; 
import javax.swing.plaf.ColorUIResource; 


public class SlotMachines 
{ 
    public static void main (String[] args) 
    { 

     UIManager.put("OptionPane.background",new ColorUIResource(250,175,0)); 
     UIManager.put("Panel.background",new ColorUIResource(0,0,0)); 
     String ask; 
     JLabel lbl = new JLabel("Input your cash?"); 
     lbl.setFont(new Font("Georgia", Font.BOLD, 16)); 
     lbl.setForeground(Color.WHITE); 
     ask = JOptionPane.showInputDialog(null, lbl); 
      double money = Double.parseDouble(ask); 
     int again; 
     int rerun = 1; 
     Random generate = new Random(); 
     while (rerun == 1) //rerun while loop 
     { 
      JLabel pnl = new JLabel("Would you like to play Slots?"); 
      pnl.setFont(new Font("Georgia", Font.BOLD, 16)); 
      pnl.setForeground(Color.WHITE);  
      again = JOptionPane.showConfirmDialog (null, pnl); 

      int bet = 2; 

      if (again == JOptionPane.NO_OPTION) 
      { 
      JLabel lab = new JLabel("Your cash stays at: " + money); 
      lab.setFont(new Font("Georgia", Font.BOLD, 16)); 
      lab.setForeground(Color.WHITE); 
      JOptionPane.showMessageDialog (null, lab); 
      } 

      while (again == JOptionPane.YES_OPTION) 
      { 
       File Winner = new File("jackpot.au"); 
       File Mega = new File("mega.au"); 
       File lose = new File("lose.au"); 
       if (money >= 2) 
       { 
        money -= bet; 
        int[] arr = new int[3]; 
        for (int i = 0; i < arr.length; i++) 
        { 
         ImageIcon icon = new ImageIcon("slotmachine.jpg"); 
         arr[i] = generate.nextInt(10);    
         JLabel label = new JLabel("[" + arr[i] + "] "); 
         label.setFont(new Font("Impact", Font.BOLD, 125)); 
         label.setForeground(Color.ORANGE); 
         JOptionPane.showMessageDialog (null, label, " ", JOptionPane.INFORMATION_MESSAGE, icon); 

        } 
        if (arr[0] == arr[1] && arr[1] == arr[2]) 
        { 

         JLabel win = new JLabel("JACKPOT!!! BET X 5!"); 
         win.setForeground(Color.CYAN); 
         PlaySound(Mega); 
         win.setFont(new Font("Arial", Font.BOLD, 140)); 
         JOptionPane.showMessageDialog (null, win); 
         money += bet*5; 
        } 
        else 
        { 
         if (arr[0] == arr[1] || arr[0] == arr[2] || arr[1] == arr[2]) 
        { 
          JLabel small = new JLabel("Rolling In cash! Doubles!"); 
          small.setForeground(Color.magenta); 
          PlaySound(Winner); 
          small.setFont(new Font("Arial", Font.BOLD, 20)); 
          JOptionPane.showMessageDialog (null, small); 
          money += bet*2; 
        } 
        } 

        JLabel cash = new JLabel("Your cash: $" + (int)money); 
        cash.setForeground(Color.GREEN); 
        cash.setFont(new Font("Georgia", Font.BOLD, 18)); 
        JOptionPane.showMessageDialog (null, cash); 

        if (money >= 2) 
        { 
         JLabel big = new JLabel("Would you like to spin again?"); 
         big.setForeground(Color.WHITE); 
         big.setFont(new Font("Georgia", Font.BOLD, 16)); 
         again = JOptionPane.showConfirmDialog (null, big); 

         if (again != JOptionPane.YES_OPTION) 
        { 
          JLabel hell = new JLabel("You came out of hell with: $" + (int)money); 
          hell.setForeground(Color.RED); 
          hell.setFont(new Font("Georgia", Font.BOLD, 18)); 
          JOptionPane.showMessageDialog (null, hell); 
          rerun = 0; 
        } 
        } 

        } 
        else 
        { 
         JLabel sorry = new JLabel("Sorry you can't afford to spin, your card is at: $" + money); 
         sorry.setForeground(Color.WHITE); 
         sorry.setFont(new Font("Georgia", Font.BOLD, 16)); 
         PlaySound(lose); 
         JOptionPane.showMessageDialog (null, sorry); 
         again = JOptionPane.NO_OPTION; 
        } 

      } 
      rerun = 0; 
     } 
    } 
    static void PlaySound(File Sound) 
    { 
     try{ 
      Clip clip = AudioSystem.getClip(); 
      clip.open(AudioSystem.getAudioInputStream(Sound)); 
      clip.start(); 

     }catch(Exception e) 
     { 

     } 
    } 
} 
+0

それは、画像や音声ファイルが既にローカルマシンにされている可能性がある場所でのクラスファイルは、デフォルトで見つけたので、それが動作? jarファイルにjarファイルにオーディオファイルと画像ファイルを含めるだけでなく、jarファイルを 'jar -xf'で展開すると、ファイルはクラスに関連するパスに抽出されますクラスファイルがそれらを見つけることができるようなファイル?コード内でこれらのリソースへの絶対パスまたは相対パスを作成していないため、コードでは、クラスファイル自体と同じパッケージの場所(同じディレクトリ)にあると想定しています。 – clearlight

+0

https://docs.oracle.com/javase/tutorial/deployment/jar/build.html – clearlight

答えて

0

二つの原因が考え:

  1. あなたは別のコンピュータにあなたとあなたのassestsを取るのを忘れていました。

  2. jarファイルにassestをパッケージ化しましたが、作業ディレクトリにないクラスパス上にあるので、ClassLoader.getResource()を使用する必要があります。このような何か:

    public static void playSound(String pathToMusicOnClasspath) { 
        try { 
         Clip clip = AudioSystem.getClip(); 
         clip.open(AudioSystem.getAudioInputStream(ClassLoader.getSystemResource(pathToMusicOnClasspath))); 
         clip.start(); 
        } catch(Exception ignore) {} 
    } 
    

    はこのようにそれを呼び出す:

    playSound("jackpot.au"); 
    

    これは、あまりにも、あなたのImageIconに行われるべきです。

    ImageIcon icon = new ImageIcon(ClassLoader.getSystemResource("slotmachine.jpg")); 
    
+0

もう少し詳しく説明できますか。私はJavaに新しい(ish)です、そして、私はこれをどこに追加するのか、それを行う方法を正確に知りません。 –

+0

男例を追加しようとしていました... – glee8e

+0

ClassLoader型のgetSystemResource(String)メソッドは、引数(File)には適用されません –

関連する問題