lundi 9 mars 2015

Tileing image on JPanel, java



I have a method to set the 'texture' of a JPanel, however it is throwing a NullPointerException, and i cannot figure out why.


Method:



void setTexutre(Image tileImage) {
Graphics g = panel.getGraphics();
int width = (int) getBounds().getWidth();
int height = (int) getBounds().getHeight();
int imageW = tileImage.getWidth(panel);
int imageH = tileImage.getHeight(panel);
for (int x5 = 0; x5 < width; x5 += imageW) {
for (int y5 = 0; y5 < height; y5 += imageH) {
g.drawImage(tileImage, x5, y5, panel);
}
}
panel.paint(g);
}


The NullPointerException is thrown when i call "g.drawImage(tileImage, x5, y5, panel);"


and yes, the image is a real image, i have checked. In the wethod above "panel" is defined as a new JPanel, and intializes normally when i do not call the method.


Thanks for any help!




Aucun commentaire:

Enregistrer un commentaire