Change jLabel image to image corresponding with Array Path?
I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:
switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();
However when I compile this with the rest of my code, I get this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)
How do I get this to work?
java arrays jpanel
add a comment |
I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:
switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();
However when I compile this with the rest of my code, I get this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)
How do I get this to work?
java arrays jpanel
Rather then use a relative pass, use an absolute pack from the top of your package ie/resources/images/etc
. Also remember, usinggetResource
denotes that the images are within the classpath/embedded in the Jar
– MadProgrammer
Nov 12 at 21:11
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
Your images need to be placed in yoursrc
directory (under some folder structure), then you can reference them via that folder structure usinggetResource
– MadProgrammer
Nov 12 at 21:30
add a comment |
I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:
switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();
However when I compile this with the rest of my code, I get this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)
How do I get this to work?
java arrays jpanel
I'm working on a school project in which we have to make a card game using Java. I've been working on this for a bit (using GUI I've got my selection screen up and whatnot), and am currently writing code to flip cards over when you press a button (think the card game WAR). The player's cards are stored in a two dimensional array (cardDeck[playernum][cardnum]). The cardDeck is created from a custom Card class that I created, and I'm able to return the values of the card number and suit. I made some folders in the project directory corresponding to each suit, within each there are 13 image files, labeled 1.png to 13.png respectively. I want to be able to call an image based on what the user's top card is (for example, the return values are 9 and hearts, so I want to find the 9 of hearts in my image directory). I've tried using the following code for player 1's turn:
switch (playerTurn)
case 1:
Card value = playerDecks[0][0];
int cardVal = value.returnUltimate();
String cardSu = value.returnCardSuite();
ImageIcon newIcon = new ImageIcon(getClass().getResource("../../card game icons/" + cardSu + "/" + cardVal + ".png"));
jLabel3.setIcon(newIcon);
jLabel3.revalidate();
However when I compile this with the rest of my code, I get this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at cards.wargame.jButton1ActionPerformed(wargame.java:151)
at cards.wargame.access$000(wargame.java:14)
at cards.wargame$1.actionPerformed(wargame.java:66)
How do I get this to work?
java arrays jpanel
java arrays jpanel
asked Nov 12 at 20:45
Ethan Leitch
1
1
Rather then use a relative pass, use an absolute pack from the top of your package ie/resources/images/etc
. Also remember, usinggetResource
denotes that the images are within the classpath/embedded in the Jar
– MadProgrammer
Nov 12 at 21:11
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
Your images need to be placed in yoursrc
directory (under some folder structure), then you can reference them via that folder structure usinggetResource
– MadProgrammer
Nov 12 at 21:30
add a comment |
Rather then use a relative pass, use an absolute pack from the top of your package ie/resources/images/etc
. Also remember, usinggetResource
denotes that the images are within the classpath/embedded in the Jar
– MadProgrammer
Nov 12 at 21:11
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
Your images need to be placed in yoursrc
directory (under some folder structure), then you can reference them via that folder structure usinggetResource
– MadProgrammer
Nov 12 at 21:30
Rather then use a relative pass, use an absolute pack from the top of your package ie
/resources/images/etc
. Also remember, using getResource
denotes that the images are within the classpath/embedded in the Jar– MadProgrammer
Nov 12 at 21:11
Rather then use a relative pass, use an absolute pack from the top of your package ie
/resources/images/etc
. Also remember, using getResource
denotes that the images are within the classpath/embedded in the Jar– MadProgrammer
Nov 12 at 21:11
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
Your images need to be placed in your
src
directory (under some folder structure), then you can reference them via that folder structure using getResource
– MadProgrammer
Nov 12 at 21:30
Your images need to be placed in your
src
directory (under some folder structure), then you can reference them via that folder structure using getResource
– MadProgrammer
Nov 12 at 21:30
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269825%2fchange-jlabel-image-to-image-corresponding-with-array-path%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269825%2fchange-jlabel-image-to-image-corresponding-with-array-path%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Rather then use a relative pass, use an absolute pack from the top of your package ie
/resources/images/etc
. Also remember, usinggetResource
denotes that the images are within the classpath/embedded in the Jar– MadProgrammer
Nov 12 at 21:11
@MadProgrammer I tried setting to "/home/ethan/netbeansprojects/cards/card game icons/......" but then got a different error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at javax.swing.ImageIcon.<init>(ImageIcon.java:217) Not sure what I'm doing wrong
– Ethan Leitch
Nov 12 at 21:22
Your images need to be placed in your
src
directory (under some folder structure), then you can reference them via that folder structure usinggetResource
– MadProgrammer
Nov 12 at 21:30