Results 1 to 4 of 4
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default rewriting methods...?

    So I started learning Swing, and I noticed that when you need to draw something, like a square or a string, you call the paint method. However, rather than just doing paint(), my book explains that the method must be written out, as if someone were writing their own method.
    for example, instead of just putting
    Java Code:
    paint();
    the book says you must do
    Java Code:
      public void paint(Graphics g){
    //code
    }
    This isn't just the book btw, just about every swing tutorial. Now, my question is; why in the world do you use the method like that? I have never done something like that. To me, that looks like someone is writing a method, not calling one. Would this be overwriting the paint method? I am so confused :o. Also, what does it mean for it to take "Graphics g" as its parameter? What does it mean when an object is a parameter?

    Sorry for the bombardment of questions; my thirst for java knowledge demands it


    Thanks to all

    Regards,
    A thoroughly confused novice programmer

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,680
    Blog Entries
    7
    Rep Power
    21

    Default Re: rewriting methods...?

    You should read your book a bit more: you don't override the paint( ... ) method; you have to override the paintComponent( ... ) method; a Graphics object is the thing you paint on. You don't call the paintComponent( ... ) method; it is done for you in the Event Dispatch Thread (EDT) by AWT, and it passes the Graphics thing as a parameter to the method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default Re: rewriting methods...?

    Sounds like you should look at the tutorial about defining a method:
    Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    and calling a method:
    Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Some methods like paintComponet() are called by the JVM and which passes the method references to objects so the method can work with it.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: rewriting methods...?

    I can put this very simply, though it may not be 100% accurate.

    Java Code:
    @Override
    public void paintComponent(Graphics g) {
    }
    is sort of like
    Java Code:
    public static void main(String[] args) {
    }
    Both of these methods get called on automatically, but paintComponent() only gets called on if you are in a JComponent which supports it. Whereas main() gets called on all of the time.
    (I hope I wasn't too inaccurate)

Similar Threads

  1. Rewriting char arrays...
    By radish in forum Advanced Java
    Replies: 5
    Last Post: 08-21-2013, 03:12 PM
  2. url rewriting in jsp to hide the original url
    By sushila in forum New To Java
    Replies: 4
    Last Post: 01-16-2013, 06:33 AM
  3. rewriting a recursive function
    By Wnt2bsleepin in forum New To Java
    Replies: 6
    Last Post: 04-19-2012, 06:48 PM
  4. Arguments for/against rewriting API classes/methods?
    By Diargg in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2012, 04:18 PM
  5. url rewriting
    By jithan in forum Java Servlet
    Replies: 2
    Last Post: 06-23-2008, 07:11 AM

Tags for this Thread

Posting Permissions

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