0

I have a question regarding Graphics in Java. Maybe it is a basic question. Below you can see my Code to create a Graphics object with my panel in a JFrame. I would like to create a graphics object in the constructor.(If i create the graphics object in a button it works) If I start the application I get immediately a nullpointer exception because of g.setColor(Color.RED);

But I don't understand why I get this exception. I have debugged the programm and the panel is created before I use the .. = panel.getGraphics(); command. So normally my Graphics object should be generated correctly and not null. So it should not be a problem to set a Color. Could you please explain me why I get this exception?

public class G2DTest extends JFrame {

private JPanel contentPane;
private JPanel panel;
Graphics g;


/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                G2DTest frame = new G2DTest();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public G2DTest() {

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 796, 810);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    panel = new JPanel();
    panel.setBackground(Color.WHITE);
    panel.setBounds(80, 87, 309, 234);
    contentPane.add(panel);

    JRadioButton rdbtnRot = new JRadioButton("rot");
    rdbtnRot.setBounds(438, 183, 251, 41);
    contentPane.add(rdbtnRot);
    g = panel.getGraphics();
    g.setColor(Color.RED);

}
Michael
  • 251
  • 1
  • 2
  • 13

1 Answers1

0

Your JPanel is created, true. And your G2DTest (which is a JFrame) is also created.

But until frame.setVisible(true); is called, these objects are just handles to unrealized system objects. The actual operating system windows are not created until the frame (and the child objects) are made visible. Attempting to retrieve a graphic context from a non-realized window results in null.


As an aside, you really should not be create and storing a Graphics context for the life of your G2DTest JFrame.

  • Any painting you do should be contained within calls to #paintComponent(Graphics g), which provides you a graphics context as an argument.
  • The Graphics context you hold onto is a scarce resource, possibly holding "heavy weight resources" like off-screen memory. By holding onto it, your application might not be playing nice with other applications, holding resources it is not using when (say) minimized to an icon.
  • Your application could break when moved from one screen to another on an multi-monitor computer. Each monitor could use a different graphics configuration; your held on to context would not be updated.

There are exceptions: a full screen, exclusive mode game might be able to hold a graphics context for an extended duration. But as a general rule, don't keep holding a reference to the Graphics context in your application.

AJNeufeld
  • 8,526
  • 1
  • 25
  • 44