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.
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
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
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
Re: Question about Graphics in Swing
Quote:
Originally Posted by
JosAH
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?
Re: Question about Graphics in Swing
You do have it as it is right here. You'll want to book mark the site.
Re: Question about Graphics in Swing
Also, please have a look at this article: Painting in AWT and Swing.
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?
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.
Re: Question about Graphics in Swing
Quote:
Originally Posted by
Fubarable
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?
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.
Re: Question about Graphics in Swing
Quote:
Originally Posted by
MW130
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?
Quote:
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.
Quote:
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.
Re: Question about Graphics in Swing
Quote:
Originally Posted by
Fubarable
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 *_*
Re: Question about Graphics in Swing
Quote:
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).
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.
Re: Question about Graphics in Swing
Ok, I will. Thank you very much to all.
Re: Question about Graphics in Swing