File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes paintComponent(Graphics g); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "paintComponent(Graphics g);" Watch "paintComponent(Graphics g);" New topic

paintComponent(Graphics g);

Josh King

Joined: Jul 04, 2012
Posts: 2
Ive been confused a litttle about the paintcomponent that i cant run it. i have the code below

i think that you can call on the methodyou self but i wanted to see the rectangle in a realtime GUI but obvisly i have no main method.
i just wanted to know how would i be able to run this small graphic???
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
add the component to a JFrame
show the frame
java will handle the painting
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

Welcome to the Ranch.

In future, while posting code, please UseCodeTags. I have added them for you this time. As you can see the code tags, make the code much more easier to read and understand.

Moving to a more suitable forum...

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
And make the paintComponent() method protected, rather than public. It ought not to be called from any of your code; you leave the JVM to call it. And lots of people seem to ask the same question.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Additionally, as far as possible don't name your own classes the same as a JDK class, and especially not a JDK class that you use in your code.

Your class extending JPanel should not be named Graphics.

luck, db
There are no new questions, but there may be new answers.
Josh King

Joined: Jul 04, 2012
Posts: 2
Thanks guys for the tips didnt even know i had to have the mthod protected but i named it Graphics just for example. i really didnt think i get a response so thnanks.
I agree. Here's the link:
subject: paintComponent(Graphics g);
It's not a secret anymore!