28697


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

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

[code]

    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;
        } 
    }
    }
    
[/code]

**インポートしたコード:**

[code]

    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.*;
    
[/code]

**メインクラスの.java:**

[code]

    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();
    }
    
    }
    
[/code]


CC BY-SA 3.0
2016-08-31T12:22:54.747
28605
2016-11-13T07:03:30.513
2016-11-13T07:03:30.513
5519
18660
question
java javafx exception
JavaFXで表に画像を表示できなくて困っています。


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

[code]

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

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

[code]

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

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


CC BY-SA 3.0
2016-09-05T08:24:55.277
28697
2016-09-05T08:24:55.277
18660
28605
answer