The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Re: I get no error, my window opens, but the graphics don't show

The beauty and "automatic" function of the paintComponent() method is when it overrides the parent's paintComponent() method, and usually the parent is a JComponent or a child of a JComponent (hence the name, paintComponent()). You have not yet learned the finer points of painting or drawing in/on Swing components, so I recommend you read the Custom Painting Tutorial. It's quite good and should open your eyes to new things.

Re: I get no error, my window opens, but the graphics don't show

Originally Posted by MW130

I wrote a program to display a rectangle within a window, but it won't show. Is it because I didn't call the paint() method?

No, paint/paintComponent is not generally invoked by the programmer .... but by the framework!
And there is another important question. Your myGame class is not a "component" (doesn't extend for example JComponent or JPanel) and thus a paintComponent method is totally pointless because the GUI framework cannot handle it.

Re: I get no error, my window opens, but the graphics don't show

Originally Posted by MW130

So, I have to extend JComponent?

Or typically JPanel. There are both conceptual and technical differences between JComponent and JPanel. The latter is a "generic" container that is typically used for layout of N components but can also be used for custom painting.