Staredit Network > Forums > Technology & Computers > Topic: Help With Java
Help With Java
Dec 6 2008, 12:25 am
By: KilaByte  

Dec 8 2008, 8:09 am KilaByte Post #21



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:

Code
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.

Dec 8 2008, 8:10 am DT_Battlekruser Post #22



Quote
To DTBK: It's an objected oriented language! ._.

It seems pointless to me to write out a class definition for something you will instantiate once and only once and never extend or otherwise use as a class-object reference. I guess if you like the organization it probably gives you greater freedom to group and move your listeners inside your code, so I guess it has its uses. I just hate coming up with and/or using names like SaveButtonListener13 for my classes.

Quote
Do I still need the paintComponent(Graphics g) method if I am directly using Graphics.drawImage()?

Huh? You should be calling drawImage inside of the paintComponent method for whatever Swing component you are painting to. This goes for just about every case, even if you are doing active rendering with getGraphics().




None.

Dec 8 2008, 8:19 am KilaByte Post #23



So..........

say I have this

Code
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:

Code
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:70)




None.

Dec 8 2008, 8:35 am DT_Battlekruser Post #24



Quote
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?




None.

Dec 8 2008, 8:40 am KilaByte Post #25



Quote from DT_Battlekruser
Quote
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:

Code
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.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[01:19 pm]
Vrael -- IM GONNA MANUFACTURE SOME SPORTBALL EQUIPMENT WHERE THE SUN DONT SHINE BOY
[01:35 am]
Ultraviolet -- Vrael
Vrael shouted: NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
Gonna put deez sportballs in your mouth
[2024-5-01. : 1:24 pm]
Vrael -- NEED SOME SPORTBALL> WE GOT YOUR SPORTBALL EQUIPMENT MANUFACTURING
[2024-4-30. : 5:08 pm]
Oh_Man -- https://youtu.be/lGxUOgfmUCQ
[2024-4-30. : 7:43 am]
NudeRaider -- Vrael
Vrael shouted: if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
Yeah I'm not a big fan of Westernhagen either, Fanta vier much better! But they didn't drop the lyrics that fit the situation. Farty: Ich bin wieder hier; nobody: in meinem Revier; Me: war nie wirklich weg
[2024-4-29. : 6:36 pm]
RIVE -- Nah, I'm still on Orange Box.
[2024-4-29. : 4:36 pm]
Oh_Man -- anyone play Outside the Box yet? it was a fun time
[2024-4-29. : 12:52 pm]
Vrael -- if you're gonna link that shit at least link some quality shit: https://www.youtube.com/watch?v=uUV3KvnvT-w
[2024-4-29. : 11:17 am]
Zycorax -- :wob:
[2024-4-27. : 9:38 pm]
NudeRaider -- Ultraviolet
Ultraviolet shouted: NudeRaider sing it brother
trust me, you don't wanna hear that. I defer that to the pros.
Please log in to shout.


Members Online: Roy, jun3hong