aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "paintComponent(Graphics g);" Watch "paintComponent(Graphics g);" New topic
Author

paintComponent(Graphics g);

Josh King
Greenhorn

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: 9990
    
    7

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
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
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
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

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
Greenhorn

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: paintComponent(Graphics g);
 
Similar Threads
I can't check for collision
Problem with Graphics
Swing probelm
repaint() method problem
Help with graphics