Results 1 to 16 of 16
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By MW130

Thread: Question about Graphics in Swing

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

    Default Question about Graphics in Swing

    Hey guys I just began to start learning to make GUI's, and made a working windows that displays variable type values, but I need help understanding a part of it, and will mark it with comments in the code. Thanks in advance.


    Java Code:
    import java.awt.*;
    import javax.swing.*;
    public class Dtypes extends JFrame {
    public Dtypes() {
    super("DataTypes:"); // Is super because it extends JFrame? What does super accomplish? It sets the title? Very confused on this :/
    setSize(1000,1000);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void paint(Graphics g) {   //I really don't get what this does. I mean, I understand that it's a method, but what is "Graphics g" in it? Could I call it something  //else, like Graphics mw? Please explain what the function of this is. 
    g.setFont(new Font("Cambria", Font.PLAIN, 16));  
    g.drawString("These are the int types in java, and how much they hold", 20, 30);
    g.drawString("Byte: " + Byte.MIN_VALUE + "to" + Byte.MAX_VALUE, 20, 50);
    g.drawString("Short: " + Short.MIN_VALUE + "to:" + Short.MAX_VALUE, 20, 70);
    g.drawString("Int: " + Integer.MIN_VALUE + "to: " + Integer.MAX_VALUE, 20, 90);
    g.drawString("Long: " +Long.MIN_VALUE + "to: " + Long.MAX_VALUE , 20, 110);
    g.drawString("Float: " + Float.MIN_VALUE + "to: " + Float.MAX_VALUE , 20, 130);
    g.drawString("Double: " + Double.MIN_VALUE + "to: " + Double.MAX_VALUE, 20, 150);
    } 
    public static void main(String[] args) {
    new Dtypes();  // Again, this is the other part that bothers me :P Why do we need the "new" operator to call this method? Also, why don't we have to call the Graphics  //G method?
     }
    
    }
    Also, Why don't we have to invoke the paint method, and just the Dtypes method? Thank you so much guys
    p.s I know super is a method, but is it an extension of JFrame? What does it do? Tysm
    Last edited by MW130; 01-12-2013 at 06:33 PM.

  2. #2
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    Well, I found that you can change the "g" there, but still don't get what "Graphics" does. I see that it's a parameter, but that's about it lol. Thanks

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question about Graphics in Swing

    What does the API documentation have to tell about the Graphics parameter? (don't tell us you're trying to program in Java without having the API documentation at hand).

    kind regards,

    Js
    cenosillicaphobia: the fear for an empty beer glass

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

    Default Re: Question about Graphics in Swing

    Quote Originally Posted by JosAH View Post
    What does the API documentation have to tell about the Graphics parameter? (don't tell us you're trying to program in Java without having the API documentation at hand).

    kind regards,

    Js
    :o. I actually don't have it :( I am doing it from a book. Where can I get it? But can you please help me understand this please?

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

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

    Default Re: Question about Graphics in Swing

    Also, please have a look at this article: Painting in AWT and Swing.

  7. #7
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    But Furable, Can you just tell me why we don't need to invoke the paint method here, and why it says new Dtypes, and not just Dtypes? Please?

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

    Default Re: Question about Graphics in Swing

    Regarding paint, I again refer you to the tutorials as there's much more to learn than your questions, and you really need to read it. As for new Dtypes(), that is called because all the main method needs to do here is call Dtypes constructor to get the ball rolling, and so there is no need to declare a Dtype variable.

  9. #9
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    Quote Originally Posted by Fubarable View Post
    Regarding paint, I again refer you to the tutorials as there's much more to learn than your questions, and you really need to read it. As for new Dtypes(), that is called because all the main method needs to do here is call Dtypes constructor to get the ball rolling, and so there is no need to declare a Dtype variable.
    I know, but why does it have "new"? I already declared it as a method, so it doesn't need new...? Why does it use new?

  10. #10
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    Fubarable, I read all the tutorials you gave me. I still really don't understand why Super is used, and why you need to say "new" Dtypes(); instead of just Dtypes(). Also, Why is not the paint method invoked? I am only confused on that, but nothing else. From your tutorials, I understand the Graphics call.

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

    Default Re: Question about Graphics in Swing

    Quote Originally Posted by MW130 View Post
    Fubarable, I read all the tutorials you gave me. I still really don't understand why Super is used
    To call the super method to do all that it needs to do, including painting the component. If you've read the tutorials then you also know that you shouldn't be drawing in the paint method of a JFrame, right?

    and why you need to say "new" Dtypes(); instead of just Dtypes().
    Again, you need to create a Dtype instance. Dtype() without the new just doesn't make any sense. Go back to the introduction to objects section of the tutorials -- the section on how to call a class's constructor to see why you need the new.

    Also, Why is not the paint method invoked?
    If you read the tutorials that I've linked to, then you already know that paint is invoked, just not by you. Please read again.

  12. #12
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    Quote Originally Posted by Fubarable View Post
    To call the super method to do all that it needs to do, including painting the component. If you've read the tutorials then you also know that you shouldn't be drawing in the paint method of a JFrame, right?


    Again, you need to create a Dtype instance. Dtype() without the new just doesn't make any sense. Go back to the introduction to objects section of the tutorials -- the section on how to call a class's constructor to see why you need the new.


    If you read the tutorials that I've linked to, then you already know that paint is invoked, just not by you. Please read again.
    But methods don't need new to be called. I can call a method just by simply typing it's name and parameters..., also, When I read them , nowhere did I see that paint is implicity called. I'm so confused *_*

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Question about Graphics in Swing

    But methods don't need new to be called. I can call a method just by simply typing it's name and parameters...
    Dtypes() is not a method, it's a constructor.

    Have a read of Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) (and especially the pages that follow on methods and constructors).

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

    Default Re: Question about Graphics in Swing

    I think we've mentioned this before, but I'll say it again as it bears repeating. You can't just pick up Java from the middle but really need to start at the beginning and progress step-wise. I suggest that you start at the first of the tutorials and work your way down, either that or buy a decent intro text book and work through it a chapter at a time. If you don't do this and build your knowledge base, you (and we) are in for a lot of frustration.
    MW130 likes this.

  15. #15
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Question about Graphics in Swing

    Ok, I will. Thank you very much to all.
    Fubarable likes this.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default Re: Question about Graphics in Swing

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Graphics.drawImage() question
    By sakratt in forum AWT / Swing
    Replies: 0
    Last Post: 03-28-2012, 10:52 PM
  2. Graphics question
    By Guy in forum New To Java
    Replies: 6
    Last Post: 07-19-2011, 07:23 AM
  3. Question on Graphics etc...
    By loopsnhoops in forum Advanced Java
    Replies: 2
    Last Post: 02-10-2011, 11:10 PM
  4. java graphics question
    By bobCallahan24 in forum AWT / Swing
    Replies: 1
    Last Post: 01-15-2010, 06:04 AM
  5. swing mixed with awt graphics
    By weddy69 in forum AWT / Swing
    Replies: 2
    Last Post: 08-31-2009, 08:01 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
  •