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.
[2026-6-01. : 4:03 pm]
IskatuMesk -- no Hello World? No Hello . jpeg? Not even ddosing some government website? smh my head
[2026-5-31. : 10:02 pm]
Symmetry -- I was really just figuring out how to compile
[2026-5-31. : 10:02 pm]
Symmetry -- I didn't make it do anything
[2026-5-31. : 9:17 pm]
Ultraviolet -- hell yeah. did you have trouble making the button functional, or just haven't gotten there yet?
[2026-5-31. : 8:29 pm]
Symmetry -- I made the marine shoot lasers and gave the medic a nonfunctional button
[2026-5-31. : 8:07 pm]
Ultraviolet -- Symmetry
Symmetry shouted: I did my first EUD thing today. Feels like me finding arsenal at age 8 again
haha for sure. in some ways it's even more exciting because you can play EUD maps on regular battle.net, don't need to convince someone to download your mod to play with you. what did you do?
[2026-5-31. : 6:47 pm]
Symmetry -- I did my first EUD thing today. Feels like me finding arsenal at age 8 again
[2026-5-31. : 3:20 pm]
l)ark_ssj9kevin -- le reddit
[2026-5-31. : 2:55 pm]
Moose -- >reddit :rip2:
[2026-5-31. : 2:07 pm]
IskatuMesk -- reddit 💀
Please log in to shout.


Members Online: NudeRaider