KolemanPa
KolemanPa

Reputation: 48

Getting path of ArrayList<image> that is already set and convert to string

My program creates a "Deck" of cards in a List, I want to be able to get the image, find the path to parse it to get the information that is in the image name.

For this, I am using an ArrayList to store the cards.

    List<Image> mainDeck = new ArrayList<Image>();

To load the image, I am using this code

    public List load(List<Image> newDeck) {
        count = 0;
        for (int i = 0; i < 4; i++) {
            for (int k = 0; k < 10; k++) {
                newDeck.add(new Image("images/" + prefix.get(i) + "" + (k + 1) + ".png"));
                count++;
            }// end number card for loop
            for (int l = 0; l < 3; l++) {
                newDeck.add(new Image("images/" + prefix.get(l) + "" + prefixFace.get(l) + ".png"));
                count++;
            }// end face card for loop
        }// end deck for loop

it then gets called and populated with images that work perfectly, I would like to create a matching array filled with Strings that hold the path for the matching Image array.

The names of images are "c1.png", "c2.png", etc, and I just need the number in the pathname

Once I get the array I should be able to parse the data to get the numbers. Any help would be much appreciated.

when using get url, I am getting an error, here is that code

        for (Image card : mainDeck){
        String path = card.getUrl();
        String name = path.substring(path.lastIndexOf("/")+1, path.lastIndexOf("."));
        nameData.put(card, name);

it is not recognizing card.getUrl();

Upvotes: 0

Views: 302

Answers (1)

SDIDSA
SDIDSA

Reputation: 1004

you don't have to create another arrayList for the paths cause the path data is already saved with the images.
if you want to retrieve the path of an image after creating it then you might use the method getUrl() in the Image class, calling getUrl() from an Image object will return the path that you used to create the image when you called the constructor, note that it will only work if you called the Image constructor using a String as the path for the image, it will not work if you used an inputStream to initialize your image, and also that it was defined in java 9, after getting the path you might split it to get the useful data you want, something like

Image image = new Image("file:/C:/Users/zinou/Desktop/edit.png");

String path = image.getUrl();
String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));

System.out.println(name);

and if i were to associate every card with its path, i would use a hashMap as in

List<Image> mainDeck = new ArrayList<Image>();
//population of the list
HashMap<Image, String> nameData = new HashMap<Image, String>();
for (Image card : mainDeck) {
    String path = card.getUrl();
    String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf("."));
    nameData.put(card, name);
}

If you have java 8

you can create a class that extends the Image class and give it an additional attribute (the URL), and add a getter for the URL so you can access it, but then your cards will be instances of the new class you created, so you can get the URL for them, the new class may look like this

public class MyImage extends javafx.scene.image.Image{

    String url;

    public MyImage(String arg0) {
        super(arg0);
        url = arg0;
    }

    public String geturl() {
        return url;
    }

}

Upvotes: 1

Related Questions