Results 1 to 9 of 9
Thread: graphics null pointer exception
- 03-27-2011, 01:17 PM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
graphics null pointer exception
Hello,
Iam trying to make a program that displays 2 eyes, and according to the mouse movement to look up, down, left, or right
i am first drawing the eyes, here is my code
the thing im having is it is highlighting on the first g. statement, fromg.setColor(..) i removed it and then it highlighted over g.drawOval(...) with a null pointer exceptionJava Code:import java.awt.*; import javax.swing.*; import javax.swing.event.*; public class EyeGUI { private JFrame frame; private JPanel eyePanel; public EyeGUI () { frame = new JFrame("The eyes have it"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(280, 200)); eyePanel = new JPanel (); eyePanel.setBackground(Color.GRAY); Graphics g = eyePanel.getGraphics(); g.setColor(Color.BLACK); g.drawOval(40, 50, 100, 100); g.drawOval(140, 50, 100 ,100); frame.setVisible(true); } public static void main (String[] args) { new EyeGUI (); } }
what could be my problem, did i miss to call a method i should have or what?
Thanks in advance
- 03-27-2011, 01:24 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Extend a JPanel and override its paintComponent(Graphics g) method; do all your drawing in that method. That way the EDT (Event Dispatch Thread) decides when your component needs to be drawn and it creates the correct Graphics object for you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-27-2011, 01:28 PM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Okay,
Thanks Jos for the help
- 03-27-2011, 01:29 PM #4
And never use getGraphics() of a Component in client code.
db
- 03-27-2011, 02:01 PM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
@Jos: should I like create a new class for constructing the shapes, and extend it JPanel and then create its constructor in my code?
If yes, in the class which extends JPanel i should only write the codes for the paintComponent, right?
@db: Lol, i just learnt my lesson now
Thnx for the help
- 03-27-2011, 02:07 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Just extend a JPanel as follows:
... and instantiate an EyePanel where you're now instantiating an ordinary JPanel.Java Code:public class EyePanel extends JPanel { // your constructors go here // override this method: public void paintComponent(Graphics g) { // draw the eye here } }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-27-2011, 03:53 PM #7
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
I did as Jos said, and heres my code
but still im getting he null pointer error at the g.setColor(...) in the paintComponent methodJava Code:import java.awt.*; import java.awt.image.*; import javax.swing.*; import javax.swing.event.*; public class EyeGUI extends JPanel { private JFrame frame; private JPanel eyePanel; private Graphics g; public EyeGUI () { frame = new JFrame("The eyes have it"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(new Dimension(280, 200)); // BufferedImage image = new BufferedImage (280, 200, BufferedImage.TYPE_INT_ARGB); // g = image.getGraphics(); // g.setColor(Color.WHITE); // g.fillOval(25, 35, 115, 115); // g.fillOval(140, 35, 115, 115); // g.setColor(Color.BLACK); // g.drawOval(25, 35, 115, 115); // g.drawOval(140, 35, 115, 115); // g.setColor(Color.BLACK); // g.fillOval(70, 80, 20, 20); // g.fillOval(170, 80, 20, 20); // // JLabel lbl = new JLabel (); // lbl.setIcon(new ImageIcon(image)); // eyePanel = new JPanel (); // eyePanel.add(lbl); Graphics g = eyePanel.getGraphics(); paintComponent(g); frame.add(eyePanel); frame.pack(); frame.setVisible(true); } public void paintComponent (Graphics g) { g.setColor(Color.WHITE); g.fillOval(25, 35, 115, 115); g.fillOval(140, 35, 115, 115); g.setColor(Color.BLACK); g.drawOval(25, 35, 115, 115); g.drawOval(140, 35, 115, 115); } public static void main (String[] args) { new EyeGUI (); } }
-
You're still using getGraphics, what Darryl told you not to do! And you're calling paintComponent directly, something you should never do. Please read the graphics tutorials as they will show you how to do this correctly since your assumptions are incorrect (as all of ours are when we start), and you need to start from ground up to learn Swing graphics (we all do).
For e.g., please check out:
Painting in Swing
2D Graphics
Click on the links above for two good tutorials on this subject.Last edited by Fubarable; 03-27-2011 at 04:07 PM.
- 03-27-2011, 04:05 PM #9
Senior Member
- Join Date
- Nov 2010
- Posts
- 155
- Rep Power
- 3
Similar Threads
-
Null Pointer Exception
By jonytek in forum New To JavaReplies: 5Last Post: 03-02-2011, 07:16 AM -
Null pointer exception
By samuel.roshni in forum Java ServletReplies: 14Last Post: 01-22-2011, 02:25 PM -
Null pointer exception
By talha06 in forum JDBCReplies: 5Last Post: 07-14-2009, 01:12 AM -
Null Pointer Exception
By andre1011 in forum Advanced JavaReplies: 4Last Post: 02-07-2009, 03:30 AM -
getting a null pointer exception
By Rjava in forum XMLReplies: 4Last Post: 07-16-2008, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks