-3

I have this code that I was trying to get the graphic to appear after it was clicked and I was trying to find out how to do this but every time I try and run it, it gives an error and I do not know how to fix to get it to work.

Exception in thread "main" java.lang.NullPointerException
    at memor.main(memor.java:131)

Graphics g= pan.getGraphics();  

Code

import java.awt.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.GridLayout;

import javax.swing.JButton;

public class memor extends JFrame
{
public static void main(String args[]){

final JPanel pan;
GridLayout h=new GridLayout(3,3);

pan =new JPanel(h);
JButton button1= new JButton("1");
pan.add(button1);
if (button1.isEnabled()){
Graphics g= pan.getGraphics();
g.setColor(new Color(156, 93, 82));
g.fill3DRect(21,3,7,12, true);
g.setColor(new Color(156,23,134));
g.fillOval(1,15,15,15);
g.fillOval(16,15,15,15);
g.fillOval(31,15,15,15);
g.fillOval(7,31,15,15);
g.fillOval(22,31,15,15);
g.fillOval(16,47,15,15);
}}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    `Graphics g= pan.getGraphics();` That is not the correct way to go about [custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/). Further, it is tricky to combine custom painting with components as you seem to want. Also, use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. – Andrew Thompson Oct 02 '13 at 20:11
  • 1
    Why should we bother helping you if you simply choose to ignore advice: [previous similar question](http://stackoverflow.com/questions/19128648/how-to-initialize-graphics-because-it-says-it-was-not-initialized). If advice confuses you, then please ask for clarification in a comment, but don't blindly ignore it, keep making the same mistakes, and then wonder why your code isn't working. – Hovercraft Full Of Eels Oct 02 '13 at 20:17
  • 1
    This is the third time I've seen this piece of code and at least the third time I've seen people (including myself) try to explain to you this is not how you do custom painting or handle `Graphics`. Check out [performing custom painting](http://docs.oracle.com/javase/tutorial/uiswing/painting/) for more details. Voting to close... – MadProgrammer Oct 02 '13 at 20:37

1 Answers1

4

The Graphics object in a JComponent is initialized when the component has to be drawn. This means that you have to render the JPanel before you can access the Graphics object.

So, you have to add the JPanel to a JFrame (or maybe a JWindow), set the window visible, then you'll be able to use the component's Graphics object.

Anyway custom drawings should be made overriding the JPanel's paintComponent method.

BackSlash
  • 21,927
  • 22
  • 96
  • 136
  • *"set the window visible, then you'll be able to use the component's Graphics object."* But the exact way they are using the `Graphics` object in that example is wrong, and will be erased in the next `paint(..)`. Glad to see you added the 3rd paragraph though.. +1 – Andrew Thompson Oct 02 '13 at 20:14
  • @AndrewThompson Thank you, i added the 3rd paragraph to say that this is not the correct way to do it, basically the first two paragraphs solve the problem of getting a null `Graphics` object, but the correct way is to use the `paintComponent` :) – BackSlash Oct 02 '13 at 20:15
  • @AndrewThompson: chit, she was told all this [previously](http://stackoverflow.com/questions/19128648/how-to-initialize-graphics-because-it-says-it-was-not-initialized) but chose to ignore the advice. – Hovercraft Full Of Eels Oct 02 '13 at 20:16
  • 2
    @HovercraftFullOfEels Makes me wonder why these types even bother asking questions. Do they intend to keep asking them until they hear the answer they want? ..get free code? ..get a prize for 'most repetitive question'? :( – Andrew Thompson Oct 02 '13 at 20:19