-6

学校でソーシャルゲームを作っている最中、編成画面のために配列を解読して表を作成するプログラムを作ろうとした所、ResourceBundleがnullでセル内部に画像を表示できない状況です。

Cards.javaの最終的なコード:

class csvData{
// 列数
static int NUMBER = 5+1+10+3+2+5+3+3;//chara
static String[][] charlist = csvlist_hen.setload("_chara.csv",NUMBER);
static String[][] wepidlist = csvlist_hen.setload("_wep.csv",13);
static String[][] abidlist = csvlist_hen.setload("_ability.csv",14);
}

class cmain{
    private String here = (getClass().getResource("./")).toString();//※追加箇所
    private Image cimage;
    private int x = 0;
    private int y = 0;
    public Image getcimage(){
        if(this.cimage != null){ 
            return this.cimage;
        }else{
            return null;
        }
    }
    public int getx(){
        return this.x;
    }
    public int gety(){
        return this.y;
    }
    cmain(){
        try {
            this.cimage = new Image(here + "../card/null_smn_64.png");//※修正箇所
        }catch (Exception e){
            System.out.println("(@・^∧・'@)" + e);
        }
        this.x = 0;
        this.y = 0;
    }
    public void setcimage(String imghead,String x,String y){
        if(imghead != null){ 
            try {
                this.cimage = new Image(here + "../card/" + imghead + "_smn_64.png");//※修正箇所
            }catch (Exception e){
                System.out.println("(@・^∧・'@)" + e);
                System.out.println("(@・^∧・'@)" + imghead);
            }
            this.x = Integer.parseInt(x);
            this.y = Integer.parseInt(y);
        }else{
            try {
                this.cimage = new Image(here + "../card/null_smn_64.png");//※修正箇所
            }catch (Exception e){
                System.out.println("(@・^∧・'@)" + e);
            }
            this.x = 0;
            this.y = 0;
        }
    }
}
public class Cards {
private String cardid;
private String cname;
private String Ptype;
private String atk;
private String hp;
private String rensya;
private String spd;
private String wep;
private String ability;
private String length;
private String weptype;
private cmain cmain = new cmain();

//public Cards(String[][] carr,int num) {
Cards(String[][] carr,int num) {
    int _cid = -1;
    this.cname = "null";
    this.Ptype = " ";
    this.atk = "0";
    this.length = "0";
    this.rensya = "0";
    this.hp = "0";
    this.spd = "0";
    this.weptype = " ";
    this.cardid = "null#0#0";
    this.wep = "なし";
    if(carr != null && csvData.charlist != null){ 
        if(carr[num] != null){
            if(carr[num][0] != null && carr[num][1] != null){
                _cid = csvlist_hen.searchCard(csvData.charlist,carr[num][0],carr[num][1]);
            }
        }
    } 
    if(_cid != -1 && csvData.charlist[_cid] != null){ 
        setcname(csvData.charlist[_cid][3]);
    setPtype(csvData.charlist[_cid][4]);
    //5
    setatk(this.atk = csvData.charlist[_cid][6]);//6-7
    //8-9
    setlength(csvData.charlist[_cid][10]);//10-11
    setrensya(csvData.charlist[_cid][12]);//12-13
    sethp(csvData.charlist[_cid][14]);//14-15
    setspd(csvData.charlist[_cid][16]);//16
    setweptype(csvData.charlist[_cid][17]);//17-18 19
    setcardid(csvData.charlist[_cid][20] + "#" + csvData.charlist[_cid][23] + "#" + csvData.charlist[_cid][24]);
    //20-24 25
    setability(csvData.charlist[_cid][26]);//26-27
    setwep("なし");
        this.cmain.setcimage(csvData.charlist[_cid][20],csvData.charlist[_cid][23],csvData.charlist[_cid][24]);
    }
}

public void setcard(String[][] carr,int num) {
    int _cid = -1;
    this.cname = "null";
    this.Ptype = " ";
    this.atk = "0";
    this.length = "0";
    this.rensya = "0";
    this.hp = "0";
    this.spd = "0";
    this.weptype = " ";
    this.cardid = "null#0#0";
    this.wep = "なし";
    if(carr != null && csvData.charlist != null){ 
        if(carr[num] != null){
            if(carr[num][0] != null && carr[num][1] != null){
                _cid = csvlist_hen.searchCard(csvData.charlist,carr[num][0],carr[num][1]);
            }
        }
    } 
    if(_cid != -1 && csvData.charlist[_cid] != null){ 
        setcname(csvData.charlist[_cid][3]);
    setPtype(csvData.charlist[_cid][4]);
    //5
    setatk(this.atk = csvData.charlist[_cid][6]);//6-7
    //8-9
    setlength(csvData.charlist[_cid][10]);//10-11
    setrensya(csvData.charlist[_cid][12]);//12-13
    sethp(csvData.charlist[_cid][14]);//14-15
    setspd(csvData.charlist[_cid][16]);//16
    setweptype(csvData.charlist[_cid][17]);//17-18 19
    setcardid(csvData.charlist[_cid][20] + "#" + csvData.charlist[_cid][23] + "#" + csvData.charlist[_cid][24]);
    //20-24 25
    setability(csvData.charlist[_cid][26]);//26-27
    setwep("なし");
        this.cmain.setcimage(csvData.charlist[_cid][20],csvData.charlist[_cid][23],csvData.charlist[_cid][24]);
    }
}

    public Image getcimage(){
        Image ci = this.cmain.getcimage();
            System.out.println("(@・^∧・'@)" + ci);
        if(ci != null){ 
            return ci;
        }
        return new Image("../card/null_smn_64.png");//※修正箇所の修正前はこのように"here+"がありませんでした
    }
    public int getx(){
        return this.cmain.getx();
    }
    public int gety(){
        return this.cmain.gety();
    }

public String getcardid() {
    if(this.cardid != null){ 
        return this.cardid;
    }
    return "null#0#0";
}
public String getcname() {
    if(this.cname != null){ 
    return this.cname;
    }
    return "なし";
}
public String getPtype() {
    if(this.Ptype != null){ 
    return this.Ptype;
    }
    return "なし";
}
public String getatk() {
    if(this.atk != null){ 
    return this.atk;
    }
    return "0";
}
public String getspd() {
    if(this.spd != null){ 
    return this.spd;
    }
    return "0";
}
public String getlength() {
    if(this.length != null){ 
    return this.length;
    }
    return "0";
}
public String getrensya() {
    if(this.rensya != null){ 
    return this.rensya;
    }
    return "0";
}
public String gethp() {
    if(this.hp != null){ 
    return this.hp;
    }
    return "0";
}
public String getweptype() {
    if(this.weptype != null){ 
    return this.weptype;
    }
    return "なし";
}
public String getability() {
    if(this.ability != null){ 
    return this.ability;
    } 
    return "なし";
}
public String getwep() {
    if(this.wep != null){ 
    return this.wep;
    }
    return "なし";
}
void setcardid(String title) {
    if(title != null){ 
        this.cardid = title;
    } 
}
void setcname(String title) {
    if(title != null){ 
    this.cname = title;
    } 
}
void setPtype(String title) {
    if(title != null){ 
    this.Ptype = title;
    } 
}
void setatk(String title) {
    if(title != null){ 
    this.atk = title;
    } 
}
void setlength(String title) {
    if(title != null){ 
    this.length = title;
    } 
}
void setrensya(String title) {
    if(title != null){ 
    this.rensya = title;
    } 
}
void sethp(String title) {
    if(title != null){ 
    this.hp = title;
    } 
}
void setspd(String title) {
    if(title != null){ 
    this.spd = title;
    } 
}
void setweptype(String title) {
    if(title != null){ 
    this.weptype = title;
    } 
}
void setability(String title) {
    if(title != null){ 
    this.ability = title;
    }
}
void setwep(String title) {
    if(title != null){ 
    this.wep = title;
    } 
}
}

インポートしたコード:

import java.net.URL;
import java.util.*;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.TableCell;
import javafx.scene.control.cell.*;
import javafx.event.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.*;
import javafx.collections.*;
import javafx.geometry.Rectangle2D;
import javafx.geometry.Pos;
import javafx.beans.value.*;
import javafx.util.*;

import java.io.*;
import java.lang.*;

メインクラスの.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.stage.Stage;

public class hentst extends Application{

private Stage stage;

public static void main(String[] args) {
    // JavaFX の実行
    launch(args);
    //Application.launch(args);
}

@Override
public void start( Stage primaryStage ) throws Exception{
    stage = primaryStage;
    Parent root= FXMLLoader.load(getClass().getResource("hen_.fxml"));
    stage.setScene(new Scene(root, 640, 400));
    stage.show();
}

}
5
  • えぇと、これは英語が読めないと言う質問なのでしょうか?初期化時にリソース先が見つからないとかエラーで発されているんですが……。そもそもクローズされた理由としてあなたが分からない部分が分からないです。エラーに理由が書いてあるので、その発生源のコードのどの部分が分からないのか明確にして質問しましょう。
    – user10259
    2016年9月1日 2:21
  • あらら、imageそのものは外部staticで宣言できないのね。(stackoverflow.com/questions/17454593/…)
    – Laclip
    2016年9月1日 5:25
  • 質問内容がはっきりしない、という問題は解消されたので再オープンしました。が、修正後のコードだけ載せられていても後から見た人には「どんな問題をどう解決できたのか」がわからないのではないでしょうか。今回の経験を共有したいということであれば、「どんな問題が発生している」という質問と「何が原因だった・どうしたら解決した」という回答という形式で投稿してください。そうすれば回答の承認という機能で、解決済みのマークを付けることも可能です。
    – unarist
    2016年9月5日 7:54
  • 保存投稿とはどういうことでしょうか?
    – ra1ned
    2016年9月5日 11:46
  • @Sun 削除しないと決めたら忘れないようにと目印を置いてます
    – Laclip
    2016年9月5日 16:39

1 件の回答 1

0

【原因】
ResourceBundleがnullの時は相対パスが使えませんでした。
それでも相対パスにしないとフォルダを移動した時に修正が大変です。
【解決方法】

private String here = (getClass().getResource("./")).toString();

この様にファイルのある場所をstringで予め保存しておきます。

this.cimage = new Image(here + "../card/null_smn_64.png");

それを相対パスで指定している文字列の手前に付けます。
こうする事で相対パスを指定しても画像が表示できるようになりました。

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。