This content has been marked as final. Show 3 replies
// Is super because it extends JFrame?Yes.
What does super accomplish? It sets the title?Yes, in the case of JFrame.
//I really don't get what this does. I mean, I understand that it's a method, but what is "Graphics g" in it?The Graphics object you are supposed to paint on.
Could I call it something else, like Graphics mw?Yes.
// Again, this is the other part that bothers me :P Why do we need the "new" operator to call this method?Because it isn't a method, its a constructor.
Also, why don't we have to call the paint method?Because Swing calls it when it wants you to paint. It knows. For example, if your window is behind another one, no painting is required.
Also, Why don't we have to invoke the paint method, and just the Dtypes method?This is not an 'also', this is 'again', and the questions have been answered.
I know super is a method,No it isn't, it is a way of calling a superclass constructor. It corresponds to calling the JFrame constructor JFrame(String), the way you have used it.
I strongly recommend you read the language part of the Java Tutorial before posting again.
Graphics is a class in java.awt package. It is an abstract class and hence it's object cannot be created directly. It is however used in the paint() method which takes java.awt.Graphics as parameter.
This class contains methods to draw shapes just as you have done in your code.
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);
paint() method is automatically invoked. We need not do it manually. It loads when the frame loads.
super("DataTypes:"); means that you are sending a String type parameter to the JFrame's constructor which is here the title of the JFrame. This must be the first statement, that's a rule.
As this code is written in the constructor, it is called when you create an object. You've done it in main()
You can also call Graphics g as Graphics mw, because the reference variable name can be anything, even your own name. But don't forget to look at the rules of naming the variables in Java.
g.setFont() method is present in java.awt.Graphics class takes a java.awt.Font object which further takes the font name, style and size respectively. This is used to set the given font for the string you are drawing on the frame.
g.drawString() method draws a string. It takes String to be drawn as first parameter followed by the x,y co ordinates in the frame.