Solved

DrawString( ) not happening in Frame Window

Posted on 2000-04-17
6
218 Views
Last Modified: 2006-11-17
Hi,

I am a beginner with Java programming. Got a few stoppers here. Here is my program

import java.awt.*;

public class BaseFrame2 extends Frame
{
    TextDialog dlg;
      String message = "This is a Window";
      BaseFrame2(String title)
      {
            super(title);
            setFont(new Font("Helvetica",Font.BOLD,12));
            dlg = new TextDialog(this,"Enter Text",true);
            dlg.resize(150,100);

            MenuBar mb = new MenuBar();
            Menu m = new Menu("Colors");
            m.add(new MenuItem("Red"));
            m.add(new MenuItem("Green"));
            m.add(new MenuItem("Blue"));
            m.add(new MenuItem("-"));
            m.add(new CheckboxMenuItem("Black"));

            Menu mf= new Menu("Font");
            mf.add(new MenuItem("Regular"));
            mf.add(new MenuItem("Bold"));
            mf.add(new MenuItem("Italic"));
            m.add(mf);
            m.add(new MenuItem("Set Text"));

            mb.add(m);
            setMenuBar(mb);
      }

      public void paint(Graphics g)
        {
          g.drawString(message,20,20);
      }

From my main applet class on a Button Click Action I am popping up the above defined Frame. But I am not able see the message "This is a window" drawn into the Frame. What could be wrong? I tried even explicitly calling repaint()
in the constructor. But still I am not able to see the message drawn into my Frame Window. Why is this happening? Hope one of you there can help me..
Is there any settings I have to give to see texts drawn into my Frame.

Thank you very much..
Prince
0
Comment
Question by:princejose
6 Comments
 
LVL 1

Expert Comment

by:phutson
ID: 2724170
I don't see your validate()? Is it in the applet?


fyi from the api

validate

public void validate()

     Validates this container and all of its subcomponents.

     AWT uses validate to cause a container to lay out its subcomponents again after the components it contains have been
     added to or modified.
     Overrides:
          validate in class Component
     See Also:
          validate(), Component.invalidate()

-Philip
0
 
LVL 3

Accepted Solution

by:
falter earned 100 total points
ID: 2724323
Hu princejose,

i tried your code your message is behind the menubar.
Try something like setSize(400,400) in the constructor of the BaseFrame2 and something like
g.drawString(200,200) at the pain method.

And you will see your message.
If not play around with x and y in drawString.

regards
jf
0
 
LVL 4

Expert Comment

by:kylar
ID: 2724939
at 20,20 the String is probably being hidden inside the insets somewhere. try what falter said and see if that works.

Kylar
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:Ash_
ID: 2725436
princejose,

like falter said, you are not setting the size of the frame at all.
preferably, call pack() in the constructor ( does the laying out & calculation of sizes). But for this, you need to add components (not use paint(). For example

BaseFrame2()
{
//previous code
//add new WhateverDisplay();
}

class WhateverDisplay
{
  public void paint(Graphics g)
  {
  //blah blah
  }
  public Dimension getPreferredSize()
  {
  //return size u want( preferably calculate it dynamically)
}
0
 
LVL 1

Expert Comment

by:samers
ID: 2725641
try to pring in another x,y axis

for example 100,100
and see what happen
0
 

Author Comment

by:princejose
ID: 2725651
Hi Falter,

 I was of the impression that the pixels are counted from beneath the menubar. But what you said is correct. When I changed the position of the string it is visible.

  Thank you very much. Thanks to all who had taken their time to respond to my query.

Prince

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
Systems talking to each other 5 109
eclipse java  build path 6 20
GUI builder for Eclipse? 8 10
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now