This week's book giveaway is in the HTML/CSS/JavaScript forum.
We're giving away four copies of Practical SVG and have Chris Coyier on-line!
See this thread for details.
Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

paintComponent(Graphics g);

 
Josh King
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add the component to a JFrame
show the frame
java will handle the painting
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Campbell Ritchie
Marshal
Posts: 52664
122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Josh King
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!