Problems using Graphics with Panels in the Java Swing Library?

You're getting NPE because g is not set, therefore, it's null Furthermore, you shouldn't be doing the drawing in the constructor. Overload paintComponent(Graphics g) instead public class Gallow extends JPanel { public paintComponent(Graphics g){ g. FillOval(10, 20, 40, 25); } } I'd also look into BufferedImage.

You're getting NPE because g is not set, therefore, it's null. Furthermore, you shouldn't be doing the drawing in the constructor. Overload paintComponent(Graphics g) instead.

Public class Gallow extends JPanel { public paintComponent(Graphics g){ g. FillOval(10, 20, 40, 25); } } I'd also look into BufferedImage.

I see, so im guessing the paintComponent is called each time a JPanel is created. – Tomek Nov 11 '08 at 2:00 Actually, it's called each time the component is redrawn. Swing doesn't remember the appearance of each component, rather each component is responsible for drawing and redrawing itself as needed.

– sblundy Nov 11 '08 at 2:03.

A couple of things: Don't forget to add the panel to the JFrame. And override the paint() method of JPanel for your custom painting. You do not need to declare a Graphics object since the JPanel's paint method will have a reference to one in any case.

Package hangman2; import java.awt. *; import javax.swing. *; public class Hangman2 extends JFrame{ private GridLayout alphabetLayout = new GridLayout(2,2,5,5); private Gallow gallow = new Gallow(); public Hangman2() { setLayout(alphabetLayout); add(gallow, BorderLayout.

CENTER);//here setSize(1000,500); setVisible( true ); } public static void main( String args ) { Hangman2 application = new Hangman2(); application. SetDefaultCloseOperation( JFrame. EXIT_ON_CLOSE ); } } package hangman2; import java.awt.

*; import javax.swing. *; public class Gallow extends JPanel { public Gallow(){ super(); } public void paint(Graphics g){ g. FillOval(10, 20, 40, 25); } }.

You should override paintComponent, not paint – Steve McLeod Sep 4 '09 at 11:03.

I am new to the Java swing library so I could be doing something very dumb. Either way here are my two classes I am just playing around for now and all I want to do is draw a damn circle (ill want to draw a gallow, with a hangman on it in the end). The NullPointerException comes in at the g.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions