Link to home
Start Free TrialLog in
Avatar of Virupaksha
Virupaksha

asked on

text is disappearing

i have written the following code to draw some text in the graph, but the text is disappearing just after displaying.

Font labelFont     = new Font("Helvetica",Font.BOLD,12);
Graphics g = getGraphics();
setOpaque(true);
Graphics2D     g2D          = (Graphics2D) g;
TextLayout layout = new TextLayout(viewSpecs.getName(),labelFont,g2D.getFontRenderContext());
 layout.draw(g2D,available.width/4,available.height/2+(int)origin.getY());
     updateUI();
Avatar of Mayank S
Mayank S
Flag of India image

The update () method is probably doing so.... I guess its clearing the text.

Mayank.
Avatar of Virupaksha
Virupaksha

ASKER

actually the above code is not wriiten in the paint() method. i tried with and without updateUI(),repaint(), also setOpaque().
actually the above code is not wriiten in the paint() method. i tried with and without updateUI(),repaint(), also setOpaque().
What is the code in your paint () method?? Also, is the above code in your init () method? (I'm assuming that you're using Applets. If not, then please correct me..)

Mayank.
there is no paint method for this class, actually i am calling some class to draw bars in the graph, so that class is containg paint() method. i cannot draw the text there because it will be for one bar, but i want it for the whole graph, that's why i am calling getGraphics() after drawing bars.
Can you please post a part of your code so that it is a little clearer....

Mayank.
i am sending the code for the class where i am using the above code.

public class TDrawBarGraphView extends TDrawView {    TViewSpecs               viewSpecs;
    TGraduationHorzComponent     gradComp;
     TData traceData;
     TEventBar eventBar;
     cyclesClass cyclesArray[];

    public TDrawBarGraphView(TViewSpecs viewSpecs, TData td, TEventBar tb, cyclesClass ca[]){
     super( viewSpecs.getName() );
     this.setName(viewSpecs.getName());
     this.viewSpecs = new TViewSpecs(viewSpecs);
     traceData = td;
     eventBar = tb;
     cyclesArray = ca;
     gradComp = new TGraduationHorzComponent(new TViewSpecs(viewSpecs), traceData, eventBar, cyclesArray, (TDrawView)this);
     setLayout(new TBarGraphLayout(this, gradComp));
     //add(gradComp);

    }

    public TViewSpecs getViewSpecs(){
     return(viewSpecs);
    }

/*This method creates the bar graph components based on the viewspecs information */
    public void setupGraphComp(){

     Point          origin          = new Point(gradComp.getOrigin());
     Dimension     available     = new Dimension(gradComp.getAvailable());
     double          currXPos     = origin.getX();
     double          horzGap          = available.width / viewSpecs.getCount();
     boolean          drawEnclRect     = true;
     double          maxYVal          = (double)viewSpecs.getMaxY();


     if( horzGap < 4 )
         drawEnclRect = false;

    TGraphComponentSpecs    spec        = viewSpecs.getCompSpecs(0);
     double w = spec.width;
    if(w != 0){
    viewSpecs.getCompSpecs(0).width = viewSpecs.getCompSpecs(1).width;
    }

     for(int i=0; i < viewSpecs.getCount(); i++){
         TGraphVertComponent     graphComp;
         TGraphComponentSpecs specs = viewSpecs.getCompSpecs(i);
         double width = (available.width * specs.width)/
          (viewSpecs.getMaxX() - viewSpecs.getMinX() + w);
         double height = available.height;

         specs.drawEnclRect = drawEnclRect;
         graphComp = new TGraphVertComponent(new Point((int)currXPos, 0),
                              new Dimension((int)specs.width, (int)height),
                              new TGraphComponentSpecs(specs), true, maxYVal, viewSpecs);
         add(graphComp);
         currXPos += width;
     }

     Font          labelFont     = new Font("Helvetica",Font.BOLD,12);
     Graphics g = getGraphics();
     //setOpaque(true);
     Graphics2D     g2D          = (Graphics2D) g;
     TextLayout layout = new TextLayout(viewSpecs.getName(),labelFont,g2D.getFontRenderContext());
     layout.draw(g2D,available.width/4,available.height/2+(int)origin.getY());
     //updateUI();
     //repaint();

    }


}
Ok, so is the problem occuring even after commenting the updateUI () part? Where is the setupGraphComp () method called?

Mayank.
it is called in the same class (ie TDrawBarGraphView),sorry i forgot to tell you that i am not using applet
Ok, maybe in some other method which you haven't shown here. It is obviously a problm of refrehsing or reloading which is occuring due to some method invocation occuring after the text is displaying, which is again clearing it. You will have to go through the hard process of checking the flow of control and determining the sequence of all method-calls.

Mayank.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mayank and objects thanks for your comments.
i solved that problem by creating some component which draws the text, then i added that component to the above class, it worked.
Good :-)
hi objects i want to ask you one question, you mentioned about paintComponent() method but that is protected, how to call or implement that.
Just override it the same way you override paint()
Virupaksha:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept objects' comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jimmack
EE Cleanup Volunteer