• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

using constructors in gui programs

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it

any tips on when to use constructors
especially in awt gui programs
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's a good idea for constructors

do I need anything apart from jdk1.5 for using Swing
 
reply
    Bookmark Topic Watch Topic
  • New Topic