Results 1 to 4 of 4
Thread: Question on creating instances
- 01-14-2013, 03:21 AM #1
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Question on creating instances
Guys I wanted to know, in a swing/awt program, does awt call the paint method when you are running the program? Is this correct? Is it true that you just have to make an instance to call the constructor, and awt will take over the rest? *I so hope the answer to this is yes*
Last edited by MW130; 01-14-2013 at 04:16 AM.
- 01-14-2013, 03:38 AM #2
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Question on creating instances
guys, I get everything except how the code knows to run. Im so confused as to this. I have another code to show to see if maybe you can explain how these methods are being called? Thanks!
Java Code:import javax.swing.*; import java.awt.*; class Rect{ public static void main(String[] args) { Rect draw = new Rect(); } public Rect() { JFrame frame = new JFrame("Drawing with Alph!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MyComp()); frame.setSize(500,500); frame.setVisible(true); } public class MyComp extends JComponent{ public void paint(Graphics mw) { int length = 200; int width = 130; mw.setColor(Color.red); mw.drawRect(20,30,length,width); mw.fillRect(20,30,length,width); mw.setColor(Color.red); mw.drawOval(250,100, length, width); mw.setColor(Color.cyan); mw.fillOval(249, 99, length, width); } } }
- 01-14-2013, 03:45 AM #3
Senior Member
- Join Date
- Jan 2013
- Posts
- 137
- Rep Power
- 0
Re: Question on creating instances
Guys, I still don't get how these methods are being invoked. I wrote another code, and I still can't seem to figure out how the code is being invoked, or called... Can someone please point me towards where I can learn this or explain it to me thanks guys heres another prog if it matters :P.
Java Code:import javax.swing.*; import java.awt.*; class Rect { public static void main(String[] args) { Rect draw = new Rect(); } public Rect() { JFrame frame = new JFrame("Drawing with Alph!"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new MyComp()); frame.setSize(500, 500); frame.setVisible(true); } public class MyComp extends JComponent { public void paint(Graphics mw) { int length = 200; int width = 130; mw.setColor(Color.red); mw.drawRect(20, 30, length, width); mw.fillRect(20, 30, length, width); mw.setColor(Color.red); mw.drawOval(250, 100, length, width); mw.setColor(Color.cyan); mw.fillOval(249, 99, length, width); } } }
- 01-14-2013, 04:40 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: Question on creating instances
I think you're right about the painting. Swing code will ensure that the method gets called at the appropriate time. This is something your application won't know about because it might involve other things happening on the desktop - like the user dragging a window over your app or whatever. Usually you call the method paintComponent() rather than paint(). paint() will call paintComponent() but may do other things, so paintComponent() is usually what we use.
Basically there are two ways you will get an instance (1) call a constructor or (2) call some method of some class that returns an instance. So you may have expressed it backwards: you don't need an instance to call a constructor, but you will often find yourself calling a constructor to get an instance.
Similar Threads
-
Creating new instances in a loop problems
By monkeyhead in forum New To JavaReplies: 6Last Post: 11-18-2011, 09:14 PM -
Need help creating new instances dynamically. Compilable code inside.
By cedron in forum New To JavaReplies: 6Last Post: 09-12-2011, 02:27 PM -
Question Creating a .jar
By waterpolobro in forum New To JavaReplies: 1Last Post: 05-18-2011, 08:15 PM -
Creating new instances of a Match over and over again
By Che_Is_Alive in forum Advanced JavaReplies: 2Last Post: 11-19-2009, 06:05 PM -
Object Reflection: Creating new instances
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks