Do I still need the paintComponent(Graphics g) method if I am directly using Graphics.drawImage()?
I am getting a null pointer exception when trying to draw the image.
This is what the command looks like, it causes no compile time errors:
g.drawImage(image, xCoord, yCoord, null);
only causes this error when it comes to this command:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
None.
So..........
say I have this
private Graphics g;
private Image img;
private int xCoords = 100;
private int yCoords = 100;
private JPanel panel;
public static void main(String args[])
{
img = LoadImage();
}
public void paintComponent(Graphics g)
{
g.drawImage(img, xCoords, yCoords, null);
}
Then I call
panel.paintComponent(g);
errr.... right?
(Assume LoadImage() successfully gets and returns a valid Image object.)
It gives me this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:70)
None.
Then I call
panel.paintComponent(g);
You shouldn't need to call paintComponent. If you override it in the component you want to paint in, Swing should call it automatically once that component has been added to a JFrame and the frame is visible.
Out of curiosity, where were you getting 'g' from anyway?g is a graphics object?
Idk I guess it could be h,j,k,l,m,n,o,p or whatever I wanted. g just seemed like what everyone uses. and it's easy to remember.
Yeah, I got the image to show up. Problem is although I did this:
panel.drawImg(); //This calls the method to draw the image. Panel is a JPanel component.
It appears directly on the frame behind the panel that I added it to! Is there a way to make it appear overtop of the Component I draw it on?
And as for the coordinates, are the relative to the Component I paint it on or to the frame.
(My guess is java doesn't give a coordinate system to any components except the frame.)
Also, whats the difference between Active Rendering and... uh... err... Normal Rendering?
Whats Active Rendering used for.
Post has been edited 2 time(s), last time on Dec 8 2008, 8:57 am by KilaByte.
None.