Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CONFUSION in AWT..helpME!

 
golujain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friend
Myself GOLU.I have a doubt that if i subclass a Frame and then add any Component to any of its Border than the drawString()
method of Graphics doesnt work (or better! it doesnt show me the string at the specified location)in the paint().
Help me

GOLU JAIN.
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Golu,
I am a little confused by your question. It sounds like you are subclassing a Frame, then adding components to it using Border layout. Why are you trying to use paint() to draw text? Why not just add Labels at the appropriate locations? For example:

If I have misunderstood your question, I apologize. Please write again and clarify.
Next time, please be sure to register with a proper name. "golujain" does not follow the conventions. See here for details: http://www.javaranch.com/name.jsp
If you do not register properly, your account will be disabled. I would hate to see that happen.
Thank you.
Stephanie
 
Golu_Jain
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephanie,
I actually wanted that i want to add one component to the frame in the center.And then i want to display the string through
the 'drawString()' method of the Graphics .
Is this possible?
and yes ! i have change my username.Thanx for the suggestion.

GAURAV JAIN
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi GAURAV!
To use the drawString() method of Graphics, have you considered extending the Canvas class and using that as your component? Below is a brief example:

Hopefully this is more what you had in mind. If not, let us know. And thank you for changing your user name so promptly!
Stephanie
 
Golu_Jain
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Sorry I am still stuck! What i want is that to use the drawString() method of the frame(i.e. the container)in which I am adding the rest of the components.
The crux is that ,"Wether the string will be visible after adding the components in the container?If not,then why?".
Take into consideration that I want to use the paint() method of the Container (in which i am adding the components).

Golu_Jain
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Golu_Jain,
You say that you want to use drawString() in the paint() method of the Frame, then add components to the Frame. Unfortunately, I do not believe that this will work. That is, it will probably compile, but you will not be able to see your String. Here is why:
The default layout for a frame is BorderLayout. Components added to a BorderLayout take up as much room as they need. If there is only one component, and it is in the center position, it will expand to fill the entire frame. To test this, we make a Frame and add a single button to it in BorderLayout.CENTER. You will see that your button is the size of the entire window.
For example, this program uses the Frame's paint() method and drawString() as you suggest. You can see the String in the Frame:

Now all I do is add a Button to my Frame:

The String is completely covered up by the Button (or whatever component you added). Does this make sense? That is why I suggested using either Labels or a Canvas for displaying your String.
You sound like you really want to use the paint() method of the Frame (Container class). Perhaps I just don't understand your reasons. If you can explain why you need to implement your program this way, I will try to help you.
I hope this makes things clearer.
Stephanie

[This message has been edited by Stephanie Grasson (edited January 10, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic