• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overiding g in a JPanel not drawing Oval  RSS feed

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, here's the code. I cannot see why it won't show a JPanel with.



Feel free to comment on any aspect of what's written, I've been teaching myslef java and don't know if I am formating things correctly or misusing java. I need crtisism!! (: Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Chadd,

Not much wrong here except that the method you're overriding is named "paintComponent()", not "paintComponents()". If you fix that, things should work -- although you probably won't see anything unless you resize the window a smidge. That's because you set the content pane after making the frame visible. Normally you want to do things that require the layout to change before making the frame visible. If you move the "setVisible()" call to the last line of that method, it should work immediately upon launch.

Finally, note that Java nowadays has this great feature called "annotations," which let you (among other things) tell the compiler something about your intentions. You can put the line

@Override

immediately before a method you intend to be overriding. If you make a mistake like this one -- i.e., you misspell the method so it's not really an override -- then the compiler will give you an error message telling you that. Including those "@Override" annotations is a really good habit to get into.
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks very helpfull, I'm finding that using the IDE is almost as hard as getting the Java syntax. It wass telling me that I should be using @override annotation but I am still learning 18 other things and will find time to add more of that in later, however, I see at least one place I will be able to implement it now

thanks for your time

Give yourself a big hug!!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chadd Franck:
I'm finding that using the IDE is almost as hard as getting the Java syntax. It wass telling me that I should be using @override annotation . . .


That is why we usually advise beginners to avoid IDEs. Get a decent text editor, not Notepad, eg gedit (Gnome Linux) Kate (KDE Linux) jEdit (anything with a recent JRE) Notepad2 or Notepad++ (Windows).
Set up the options on the text editor, eg
  • Automatic bracket highlighting
  • Syntax colouring
  • Automatic indentation
  • Tabs replaced by 4 spaces each
  • And see how much easier it is to program.

    The @Override (not @override) annotation is indeed useful, since there are lots of problems caused by tiny spelling errors when trying to override methods, and that annotation helps pick those errors up.
     
    Chadd Franck
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One insteresting fact, the @Override annotation compiles fine when using
    paintComponents here.

    Is this because there is a seperate method called paintComponents in the parent class (JPanel?) or a parent class of that parent(JPanel?)?

    It fails to compile if I change paintComponents to paintComponentsA like;

    [ December 04, 2008: Message edited by: Chadd Franck ]
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Chadd Franck:
    One insteresting fact, the @Override annotation compiles fine when using
    paintComponents here.

    Is this because there is a seperate method called paintComponents in the parent class (JPanel?) or a parent class of that parent(JPanel?)?

    java.awt.Container has a method "public void paintComponents(Graphics g)", so in this case the @Override couldn't have solved it. You've still overridden a method - just the wrong one.
     
    Chadd Franck
    Ranch Hand
    Posts: 50
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okies, thanks for all the help, you guys don't know how much that was frustrating me.

    I think that about sums it all up .
     
    Campbell Ritchie
    Marshal
    Posts: 56584
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Having a paintComponent method and a paintComponents method is bound to cause confusion sooner or later.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!