import javax.swing.*; import java.awt.*; public class Game extends JFrame{ public Game(){ setResizable(false); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setSize(300, 300); setLocationRelativeTo(null); setVisible(true); } public void paint(Graphics g){ g.setColor(Color.BLACK); g.fillOval(100, 100, 100, 100); } public static void main(String[] args) { Game game = new Game(); game.paint(null); } } 2 answers
Regarding the NPE exception, you can and should read here: What is a Null Pointer Exception and how to fix it?
It makes no sense to copy such a large text.
awt can say the following about awt and the paint method: the awt engine calls the paint method every time the operating system reports that something needs to be painted on the canvas. It is called when a window is first created, or when we minimize / maximize a window, resize it with a mouse, etc. Such messages are sent constantly, implicitly, by the virtual machine. And, as a result, there is no need to call it.
Those. in your case, just write
public static void main(String[] args) { Game game = new Game(); } A window will be created and paint will automatically pick up.
You just do not need to call paint (), it is called implicitly by the virtual machine.
- Thank you very much! - Wuzaza
null. - Alexander Petrov