Results 1 to 16 of 16
Thread: Question about Graphics in Swing
- 01-12-2013, 07:30 PM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
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? } }
p.s I know super is a method, but is it an extension of JFrame? What does it do? TysmLast edited by MW130; 01-12-2013 at 07:33 PM.
- 01-12-2013, 07:44 PM #2
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
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
- 01-12-2013, 07:55 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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,
JsBuild a wall around Donald Trump; I'll pay for it.
- 01-12-2013, 07:56 PM #4
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
-
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.
- 01-12-2013, 08:38 PM #7
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
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.
- 01-13-2013, 12:40 AM #9
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
- 01-13-2013, 01:00 AM #10
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
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
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().
Also, Why is not the paint method invoked?
- 01-13-2013, 01:14 AM #12
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
- 01-13-2013, 02:00 AM #13
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
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...
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.
- 01-13-2013, 07:41 AM #15
Senior Member
- Join Date
- Jan 2013
- Posts
- 317
- Rep Power
- 9
Re: Question about Graphics in Swing
Ok, I will. Thank you very much to all.
- 01-15-2013, 06:21 AM #16
Re: Question about Graphics in Swing
Cross posted
https://forums.oracle.com/forums/thr...92607#10792607
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Graphics.drawImage() question
By sakratt in forum AWT / SwingReplies: 0Last Post: 03-28-2012, 11:52 PM -
Graphics question
By Guy in forum New To JavaReplies: 6Last Post: 07-19-2011, 08:23 AM -
Question on Graphics etc...
By loopsnhoops in forum Advanced JavaReplies: 2Last Post: 02-11-2011, 12:10 AM -
java graphics question
By bobCallahan24 in forum AWT / SwingReplies: 1Last Post: 01-15-2010, 07:04 AM -
swing mixed with awt graphics
By weddy69 in forum AWT / SwingReplies: 2Last Post: 08-31-2009, 09:01 AM
Bookmarks