2017-09-30 8 views
0

(Q/Aへのリンク:Merging two images)(ごめんなさい、これは奇妙な音ですが、もともと「回答」の下に投稿されました。画像の結合エラー

私は、サンプルコードを含み、100以上の投票数/好きなものがあればstackoverflowにあるものすべてを使用しています。ImageIOがスローするものはすべてIOException私はかなり正直にコーディングに新しいです、私はそれをやってきました...私は最初の4つのものが、レゴの嵐に似た単純なブロックプログラミング言語であるスクラッチと呼ばれるものであると信じています。ロボット1。とにかく、3年前、私はついにミニクラフトを改造してJavaに深く入りました。私はまだjavaのにすぎもらっていない:(し、それらを検索しようとしているにもかかわらずであるかの例外を除い全くわからないです。

public void registerIcons(IconRegister iconRegister) 
{ 
    File path = new File("mymod:"); // base path of the images 

    // load source images 
    BufferedImage core = ImageIO.read(new File(path, "cores/CoreOak.png")); 
    BufferedImage cap = ImageIO.read(new File(path, "caps/CapGold.png")); 
    BufferedImage gem = ImageIO.read(new File(path, "overlay2.png")); 

    // create the new image, canvas size is the max. of both image sizes 
    int w = Math.max(core.getWidth(), cap.getWidth()); 
    w = Math.max(w, gem.getWidth()); 
    int h = Math.max(core.getHeight(), cap.getHeight()); 
    h = Math.max(h, gem.getHeight()); 
    BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); 

    // paint both images, preserving the alpha channels 
    Graphics g = combined.getGraphics(); 
    g.drawImage(core, 0, 0, null); 
    g.drawImage(cap, 0, 0, null); 
    g.drawImage(gem, 0, 0, null); 

    // Save as new image 
    ImageIO.write(combined, "PNG", new File(path, "wand/combined.png")); 

    this.itemIcon = iconRegister.registerIcon(texturePath + "combined"); 
} 

コードがあります。例外はBufferedImageのもの、とのImageIOの下にあります。彼らは具体的に "Unhandled exception type IOException"と言っていますが、それはマイクロフトの問題であるかどうかはわかりませんが、一般的にjavaという意味では、この人は私にそれが広すぎるために怒鳴りました。私はそれが含まれるだろうと思っていた。

答えて

0

ので、根本的な問題をここに。

File path = new File("mymod:"); // base path of the images 

"mymod:overlay2.png"はファイルパスではなく、リソースロケーションです。つまり、Minecraftの内部では、jarファイルまたは任意の数のリソースパックzipファイルのうちの1つに存在する可能性のあるファイルを参照する方法を理解するための魔法の文字列です。 ではなく、 a Fileパスです。

jarファイル内のファイルからデータを読み取る場合は、標準のファイルIOでは実行できません。this answerで説明したようにgetResourceAsStream()を使用する必要があります。