Saving and printing Images from a 2D array in Java
up vote
-1
down vote
favorite
I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.
I wrote it so the rows are the cards and the columns are the suits.
Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.
An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg
I don't know why my code isn't working.
I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:
card[i][j] = tk.getImage(url);
But something tells me that's not the only thing wrong...
Can someone help me? Thanks!
The source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class GameTemplate extends JPanel {
static Image card = new Image [13][4];
public static void main (String args) {
Toolkit tk = Toolkit.getDefaultToolkit();
// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3
// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12
int name = new int [13][4];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}
GameTemplate run = new GameTemplate ();
run.image(args);
}
public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}
}
}
java arrays image static
add a comment |
up vote
-1
down vote
favorite
I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.
I wrote it so the rows are the cards and the columns are the suits.
Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.
An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg
I don't know why my code isn't working.
I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:
card[i][j] = tk.getImage(url);
But something tells me that's not the only thing wrong...
Can someone help me? Thanks!
The source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class GameTemplate extends JPanel {
static Image card = new Image [13][4];
public static void main (String args) {
Toolkit tk = Toolkit.getDefaultToolkit();
// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3
// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12
int name = new int [13][4];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}
GameTemplate run = new GameTemplate ();
run.image(args);
}
public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}
}
}
java arrays image static
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
2
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
getResource
returnsnull
if the resource can't be found. Sotk.getImage
is called withnull
as argument.which causes aNullPointerException
along the way,
– QBrute
Nov 10 at 22:58
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.
I wrote it so the rows are the cards and the columns are the suits.
Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.
An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg
I don't know why my code isn't working.
I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:
card[i][j] = tk.getImage(url);
But something tells me that's not the only thing wrong...
Can someone help me? Thanks!
The source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class GameTemplate extends JPanel {
static Image card = new Image [13][4];
public static void main (String args) {
Toolkit tk = Toolkit.getDefaultToolkit();
// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3
// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12
int name = new int [13][4];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}
GameTemplate run = new GameTemplate ();
run.image(args);
}
public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}
}
}
java arrays image static
I'm writing a test code for a poker game which assigns each image of a card to a space in a 2d array.
I wrote it so the rows are the cards and the columns are the suits.
Hearts = 0, Diamonds = 1, Spades = 2, Clubs = 3 and then Ace to King going from 0-12.
An example would be 10 of Spades would be [9][2]. And the file name would be 92.jpg
I don't know why my code isn't working.
I followed my work template except changed it from 1d to a 2d array.
Right now the following line is throwing a NullPointerException:
card[i][j] = tk.getImage(url);
But something tells me that's not the only thing wrong...
Can someone help me? Thanks!
The source code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
public class GameTemplate extends JPanel {
static Image card = new Image [13][4];
public static void main (String args) {
Toolkit tk = Toolkit.getDefaultToolkit();
// HEARTS: 0
//DIAMONDS: 1
//SPADES: 2
//CLUBS: 3
// ACES: 0 TWOS: 1 THREES: 2 FOURS: 3 FIVES: 4 SIXES: 5 SEVENS: 6 EIGHTS: 7 NINES: 8 TENS: 9 JACKS: 10 QUEENS: 11 KINGS: 12
int name = new int [13][4];
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
URL url = GameTemplate.class.getResource(name[i][j] + ".jpg"); // Loads card images into game
card[i][j] = tk.getImage(url); // assigns card images to the array
}
}
GameTemplate run = new GameTemplate ();
run.image(args);
}
public void image(Graphics g) {
for (int i=0; i < 13; i++) {
for (int j = 0; j < 4; j++) {
g.drawImage(card[i][j], i*194, j*50, this); // display the image
}
}
}
}
java arrays image static
java arrays image static
edited Nov 10 at 23:41
asked Nov 10 at 22:25
Matthew Smith
118
118
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
2
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
getResource
returnsnull
if the resource can't be found. Sotk.getImage
is called withnull
as argument.which causes aNullPointerException
along the way,
– QBrute
Nov 10 at 22:58
add a comment |
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
2
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
getResource
returnsnull
if the resource can't be found. Sotk.getImage
is called withnull
as argument.which causes aNullPointerException
along the way,
– QBrute
Nov 10 at 22:58
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
2
2
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
getResource
returns null
if the resource can't be found. So tk.getImage
is called with null
as argument.which causes a NullPointerException
along the way,– QBrute
Nov 10 at 22:58
getResource
returns null
if the resource can't be found. So tk.getImage
is called with null
as argument.which causes a NullPointerException
along the way,– QBrute
Nov 10 at 22:58
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53244028%2fsaving-and-printing-images-from-a-2d-array-in-java%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
Possible duplicate of What is a NullPointerException, and how do I fix it?
– Joe C
Nov 10 at 22:27
A little bit, the thing is I don't know if that's it or there's more going on that I'm unaware of. If you could just help me out a little that would be great :)
– Matthew Smith
Nov 10 at 22:35
2
It looks like you may need to learn to use a debugger. Please help yourself to some complementary debugging techniques. If you still have issues afterwards, please edit your question to be more specific with what help you need.
– Joe C
Nov 10 at 22:44
getResource
returnsnull
if the resource can't be found. Sotk.getImage
is called withnull
as argument.which causes aNullPointerException
along the way,– QBrute
Nov 10 at 22:58