Results 1 to 6 of 6
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default paintComponent vs paintComponents

    hey ive imported the java.awt.*; and im trying to use the paintComponent method such as below

    PHP Code:
    public void paintComponent(Graphics g){
    
        super.paintComponent(g);
    
    }
    however my IDE (Netbeans 6.8) doesn't seem to reconise super.paintComponent(g); and instead only allows super.paintComponents(g);
    could someone explain to me whats going on please?

    normally i wouldnt really mind, except however paintComponents doesnt seem to work when im drawing objects, i get nothing on the screen.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Add a @Override before the overridden method to verify the super class has that method.

    paintComponent() is a Swing method. Use paint() in AWT.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My guess (and without more code, this is just a guess) is that you're trying to add this method to a root container such as a JApplet or a JFrame. If so, don't do this but rather do your painting in a JPanel that has a paintComponent override (using the @Override annotation as Norm recommends above).

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    ok it worked when i extended JPanel rather than JApplet, and then i went back to extending JApplet and used the @Overide and your right, JApplet doesnt have the method PaintComponent to overide.
    so instead of using paintComponent i used paint which is a method in JApplet which can be overwritten. Thanks for the help guys

    Just a quick question though, im i losing any functionality using paint rather than paintComponent? (cause if i am i was thinking i could somehow put the JPanel inside the JApplet and then somehow invoke the paintComponent method, if at all possible)
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    hey nevermind, i did some research and maanged this put a panel inside a Applet and thus using the original paintComponent. i havent looked into whether or not theres a difference between paintComponent and paint because cba lol, but anyways thanks for help and the @Override thingy.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're doing the right thing as you don't want to override a JApplet's paint method and paint directly to the applet. You want to paint into a JPanel (as I suggested above) and add that JPanel to the JApplet's contentPane.

Similar Threads

  1. Trouble with paintComponent()
    By ShirlyPunk in forum AWT / Swing
    Replies: 17
    Last Post: 03-13-2010, 04:27 PM
  2. paintComponent is not working
    By spazattack in forum New To Java
    Replies: 1
    Last Post: 12-18-2009, 03:48 AM
  3. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 02:43 AM
  4. Problem going outside paintComponent
    By Thez in forum Java 2D
    Replies: 9
    Last Post: 12-08-2007, 04:59 PM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •