aspose file tools*
The moose likes Beginning Java and the fly likes using   constructors   in  gui   programs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using   constructors   in  gui   programs" Watch "using   constructors   in  gui   programs" New topic
Author

using constructors in gui programs

linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
import java.awt.*;


class MyCanvas extends Canvas

{

public void paint(Graphics g)

{ g.setColor(new Color(255,0,0));

g.drawOval(5,5,100,100);

g.setColor(Color.blue);

g.drawOval(15,10,120,120);



g.setColor(Color.black);

g.drawRect(10,10,30,30);


}

public MyCanvas()
{

Frame f=new Frame("some frame");



f.add(mc);// ------------ ERROR HERE-----------------
f.setSize(300,200);
f.setVisible(true);

}

}


public class TestCanvas9

{
public static void main(String args[])

{MyCanvas mc=new MyCanvas();




}

}


why do I get error
cannot find variable mc at the line f.add(mc);

object mc must be already created b4 constructor MyCanvas runs
so it shud be availabble there

if i replace f.add(mc) by f.add(new MyCanvas())
the compiler gives no error

but there is no proper output

please help
thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Linus,

First of all, mc is a local variable declared in the method "main"; that means that the name "mc" is not available anywhere except in that method. The only way for other code to use the object that "mc" points to would be for main() to pass "mc" as an argument to some other method; then that second method would have access to mc.

But secondly, the constructor MyCanvas, like all methods of MyCanvas, doesn't need a variable to operate on: it automatically has a variable "this" which points to itself. You can use "this" to refer to "this MyCanvas object" in any of the instance methods of the MyCanvas class. Use "this" instead of "mc", and that will fix that problem.

One final note: constructors are for constructing objects. You've written your MyCanvas constructor, though, to create JFrame, set it up, and add a MyCanvas to it; that kind of stuff really doesn't belong in MyCanvas. Instead, I'd say everything you've got in the MyCanvas constructor actually belongs in main().

[Jess in Action][AskingGoodQuestions]
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
thanks Ernest Friedman-Hill

'this' magically worked

I haven't understood well when to use a constructor in a java program
I have observed they are used in awt gui programs

but why does f.add(new MyCanvas());
not work
this object is created in MyCanvas constructor itself
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If you call "new MyCanvas() " in MyCanvas' constructor, then while that MyCanvas object is being constructed, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again; and in that constructor, it will call "new MyCanvas()" again ...

and eventually either the universe explodes, or you get a StackOverflowError, whichever comes first
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
got it

any tips on when to use constructors
especially in awt gui programs
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Are you still using AWT? Most people use Swing nowadays.
I would say it is very simple remembering when to write a constructor. Whenever you write a class. Each class should have a constructor. If you never want that constructor used you can always give it private access (but that might cause other problems).
linus dale
Ranch Hand

Joined: Jul 01, 2009
Posts: 44
that's a good idea for constructors

do I need anything apart from jdk1.5 for using Swing
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using constructors in gui programs