Note that there are some explanatory texts on larger screens.

plurals
  1. POLoading Images from a Jarfile
    primarykey
    data
    text
    <p>Alright so here is my problem and my question to you.....</p> <p>I have a game in which it needs to load images from the jar file (all the images are packed into the jar file like so): I first have the unzip of the jar file: <img src="https://i.stack.imgur.com/7ruhX.png" alt="Unziped Jar File"></p> <p>Then it goes to: <img src="https://i.stack.imgur.com/10o6R.png" alt="cards folder"></p> <p>Then inside each of those folder i have something that looks like this: <img src="https://i.stack.imgur.com/gapLk.jpg" alt="what the other folders look like also"></p> <p>Now reminder above is the Jarfile GameClient.jar unziped and you see where the cards are in it.</p> <p>So here is my code for trying to load each and every one of those images into memory</p> <pre><code> private void addCardsAndChangeSize() throws IOException { String tempString; ImageIcon tempIcon; allCards.clear(); ClassLoader cldr = this.getClass().getClassLoader(); URL imageURL; for(int i = 0; i &lt; all.length; i++) { for(int x = 0; x &lt; clubs.length; x++) { tempString = all[i][x]; tempString = "/cards/"+cardFolders[i]+"/"+tempString; imageURL = cldr.getResource(tempString); tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false); tempString = all[i][x]; tempIcon.setDescription(tempString); allCards.add(tempIcon); } } backCard = resizeImage(new ImageIcon(cldr.getResource(back)),70,70,false); } </code></pre> <p>So i call that in the contructor to load all the images into an ArrayList here is the whole class if you want to know what i do.</p> <pre><code>package global; import java.awt.*; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.Random; import javax.imageio.ImageIO; import javax.swing.*; public class Cards { private ImageIcon backCard = new ImageIcon("cards/backCard.jpg"); private String back = "/cards/backCard.jpg"; private String[] cardFolders = { "clubs","diamonds","hearts","spades" }; private String[] clubs = { "aceClubs.jpg","eightClubs.jpg","fiveClubs.jpg","fourClubs.jpg","jackClubs.jpg", "kingClubs.jpg","nineClubs.jpg","queenClubs.jpg","sevenClubs.jpg","sixClubs.jpg", "tenClubs.jpg","threeClubs.jpg","twoClubs.jpg" }; private String[] diamonds = { "aceDia.jpg","eightDia.jpg","fiveDia.jpg","fourDia.jpg","jackDia.jpg","kingDia.jpg", "nineDia.jpg","queenDia.jpg","sevenDia.jpg","sixDia.jpg","tenDia.jpg","threeDia.jpg", "twoDia.jpg" }; private String[] hearts = { "aceHearts.jpg","eightHearts.jpg","fiveHearts.jpg","fourHearts.jpg","jackHearts.jpg", "kingHearts.jpg","nineHearts.jpg","queenHearts.jpg","sevenHearts.jpg","sixHearts.jpg", "tenHearts.jpg","threeHearts.jpg","twoHearts.jpg" }; private String[] spades = { "aceSpades.jpg","eightSpades.jpg","fiveSpades.jpg","fourSpades.jpg","jackSpades.jpg", "kingSpades.jpg","nineSpades.jpg","queenSpades.jpg","sevenSpades.jpg","sixSpades.jpg", "tenSpades.jpg","threeSpades.jpg","twoSpades.jpg" }; private String[][] all = { clubs,diamonds,hearts,spades }; private ArrayList&lt;ImageIcon&gt; allCards = new ArrayList&lt;ImageIcon&gt;(); public Cards() { try { addCardsAndChangeSize(); shuffle(); } catch(Exception e) {e.printStackTrace();} } /** * @param x Cards name with extension * @return Face Value of Card */ public static int getFaceValue(String x) { int face = 0; switch(x) { case "aceClubs.jpg": case "aceDia.jpg": case "aceHearts.jpg": case "aceSpades.jpg": face = 1; break; case "eightClubs.jpg": case "eightDia.jpg": case "eightHearts.jpg": case "eightSpades.jpg": face = 8; break; case "fiveClubs.jpg": case "fiveDia.jpg": case "fiveHearts.jpg": case "fiveSpades.jpg": face = 5; break; case "fourClubs.jpg": case "fourDia.jpg": case "fourHearts.jpg": case "fourSpades.jpg": face = 4; break; case "jackClubs.jpg": case "jackDia.jpg": case "jackHearts.jpg": case "jackSpades.jpg": case "kingClubs.jpg": case "kingDia.jpg": case "kingHearts.jpg": case "kingSpades.jpg": case "queenClubs.jpg": case "queenDia.jpg": case "queenHearts.jpg": case "queenSpades.jpg": case "tenClubs.jpg": case "tenDia.jpg": case "tenHearts.jpg": case "tenSpades.jpg": face = 10; break; case "twoClubs.jpg": case "twoDia.jpg": case "twoHearts.jpg": case "twoSpades.jpg": face = 2; break; case "threeClubs.jpg": case "threeDia.jpg": case "threeHearts.jpg": case "threeSpades.jpg": face = 3; break; case "sixClubs.jpg": case "sixDia.jpg": case "sixHearts.jpg": case "sixSpades.jpg": face = 6; break; case "sevenClubs.jpg": case "sevenDia.jpg": case "sevenHearts.jpg": case "sevenSpades.jpg": face = 7; break; case "nineClubs.jpg": case "nineDia.jpg": case "nineHearts.jpg": case "nineSpades.jpg": face = 9; break; } return face; } //shuffles all the cards in the deck private void shuffle() { long seed = System.nanoTime(); Collections.shuffle(allCards, new Random(seed)); } /** * Chooses a card at random from the deck. Also removes that card when chosen * @return randomly chosen card */ public ImageIcon getRandomCard() { int index = ((int)Math.random() * allCards.size()); return allCards.remove(index); } /** * @return Image of the back of a card */ public ImageIcon getBackCard() {return backCard;} public static ImageIcon parseImage(String x) { if(x.contains("Dia")) return new ImageIcon("cards/diamonds/"+x); else if(x.contains("Clubs")) return new ImageIcon("cards/clubs/"+x); else if(x.contains("Hearts")) return new ImageIcon("cards/hearts/"+x); else if(x.contains("Spades")) return new ImageIcon("cards/spades/"+x); return null; } //adds all the cards and adds a description to them loaded into memory private void addCardsAndChangeSize() throws IOException { String tempString; ImageIcon tempIcon; allCards.clear(); ClassLoader cldr = this.getClass().getClassLoader(); URL imageURL; for(int i = 0; i &lt; all.length; i++) { for(int x = 0; x &lt; clubs.length; x++) { tempString = all[i][x]; tempString = "/cards/"+cardFolders[i]+"/"+tempString; imageURL = cldr.getResource(tempString); tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false); tempString = all[i][x]; tempIcon.setDescription(tempString); allCards.add(tempIcon); } } backCard = resizeImage(new ImageIcon(cldr.getResource(back)),70,70,false); } //resizes images public ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max) { Image image = imageIcon.getImage(); Image newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH); int width1 = newimg.getWidth(null); if ((max &amp;&amp; width1 &gt; width) || (!max &amp;&amp; width1 &lt; width)) newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH); return new ImageIcon(newimg); } </code></pre> <p>}</p> <p>I keep getting null pointer exception on the <code>tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false);</code></p> <p>Can anyone tell me why this isn't loading the images properly? Reminder i am pre-loading them into memory. My question is not how to make this better i am just simply asking what am i doing wrong with the class loader and stuff like that......</p> <p>I am somewhat new at java and i know some of this code will look horrible to you but please again i am not asking how to make this code "pro" im just looking for the explanation why its giving me null.</p> <p>Thanks!</p> <p>P.S. If you need more pictures of info or w.e. i will try to put that up right away</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload