There is an error in your code here. You set your grafica
to null
the line before you dereference it. This will certainly throw a NullPointerException
. Instead of declaring your own Graphics object, you should use the one passed in to the method you will be using for painting. To do this in Swing, you should implement the paintComponent
method to paint your image, something like this:
public void paintComponent(Graphics grafica) {
grafica.drawImage(imgFondo, 0, 0, this);
}
Note that you don't want to be doing long running tasks like reading in Image files from disk in the painting thread. The above example assumes that you have already loaded the imgFondo
and have it stored such that it is accessible in the paintComponent
method.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…