你能告诉我我哪里错了吗?

时间:2019-08-22 01:56:43

标签: java javafx

我在src中有PNG文件夹。 我有具有HashMap的ImageStore类 在主类中为图像分配字符串 我正在尝试从ImageStore的HashMap中检索图像并将其分配给cardClass.Java中的每个卡 但是我的问题是在cardClass中检索图像时,它返回null。

主要:

public class Main extends Application {
 @Override
   public void start(Stage primaryStage) throws Exception {
   Parent root = 
   FXMLLoader.load(getClass().getResource("sample.fxml"));
   primaryStage.setTitle("Card");
   primaryStage.setScene(new Scene(root, 600, 600));
   primaryStage.show();
   loadimages();
   }


    public static void main(String[] args) {
        launch(args);
    }
//In load images I am loading the images and mapping with string for ImageStore HashMap
    public void loadimages() {
        String[] rank = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
        String[] suit = {"Heart", "Diamond", "Spade", "Club"};
        for (int i = 0; i < suit.length; i++) {
            //  ImageStore.cardimagedeck.put(rank[j]+suit[i],new Image("@PNG/"+rank[j]+suit[i]+".png"));
            for (int j = 0; j < rank.length; j++) {
                File file = new File("Users/Nithyaprakash/Desktop/JAVAFX/Card/src/sample/PNG/" + rank[j] + suit[i] + ".png");
                Image image = new Image(file.toURI().toString());
                if (image != null) {
                    ImageStore.cardimagedeck.put(rank[j] + suit[i], image);

                }
            }

        }

    }
}

DeckOfCards:

  public DeckOfCards()
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<12;j++)
            {


                    deck.add(new cardClass(j,i));
                }
            }
        }

CardClass:

public class cardClass extends ImageView {
    enum suit{Heart,Diamond,Spade,Club}
    enum rank
    {Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King,Ace}

    private rank cardrank;
    private suit cardsuit;
    private Image cardimage;
    public cardClass(int r,int s)
    {
        this.cardrank= rank.values()[r];
        this.cardsuit=suit.values()[s];
       cardimage=ImageStore.cardimagedeck.get(cardrank.name()+cardsuit.name());
    }

    @Override
    public String toString() {
        return  cardrank +
                " of " + cardsuit ;
    }
}

ImageStore类:

public class ImageStore {
    static HashMap<String, Image> cardimagedeck=new HashMap<>();


    public static HashMap<String, Image> getCardimagedeck() {
        return cardimagedeck;
    }

    public static void setCardimagedeck(HashMap<String, Image> cardimagedeck) {
        ImageStore.cardimagedeck = cardimagedeck;
    }

}

在这里您可以检查左面板的PNG文件夹和每张卡的图像。

0 个答案:

没有答案