This discussion is archived
3 Replies Latest reply: Jan 14, 2013 8:21 PM by DarrylBurke RSS

A Question on Graphics in Swing

983190 Newbie
Currently Being Moderated
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.
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 paint 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
  • 1. Re: A Question on Graphics in Swing
    EJP Guru
    Currently Being Moderated
    // 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.
  • 2. Re: A Question on Graphics in Swing
    Gowtham Gutha Newbie
    Currently Being Moderated
    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.

    Gowtham Gutha.
  • 3. Re: A Question on Graphics in Swing
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Cross posted
    http://www.java-forums.org/new-java/67601-question-about-graphics-swing.html

    db

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points