Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

text is disappearing

Posted on 2003-03-24
18
Medium Priority
?
192 Views
Last Modified: 2010-03-31
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();
0
Comment
Question by:Virupaksha
  • 7
  • 6
  • 2
  • +2
17 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194114
The update () method is probably doing so.... I guess its clearing the text.

Mayank.
0
 

Author Comment

by:Virupaksha
ID: 8194146
actually the above code is not wriiten in the paint() method. i tried with and without updateUI(),repaint(), also setOpaque().
0
 

Author Comment

by:Virupaksha
ID: 8194147
actually the above code is not wriiten in the paint() method. i tried with and without updateUI(),repaint(), also setOpaque().
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8194161
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.
0
 

Author Comment

by:Virupaksha
ID: 8194181
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194207
Can you please post a part of your code so that it is a little clearer....

Mayank.
0
 

Author Comment

by:Virupaksha
ID: 8194294
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();

    }


}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194353
Ok, so is the problem occuring even after commenting the updateUI () part? Where is the setupGraphComp () method called?

Mayank.
0
 

Author Comment

by:Virupaksha
ID: 8194435
it is called in the same class (ie TDrawBarGraphView),sorry i forgot to tell you that i am not using applet
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8194455
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.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 8197997
All your painting should be done in the paintCOmponent() method, as it is the class that is responsible for painting the component when required.
You should not use the getGraphics() method to paint to your component.
0
 

Author Comment

by:Virupaksha
ID: 8201325
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8201356
Good :-)
0
 

Author Comment

by:Virupaksha
ID: 8217122
hi objects i want to ask you one question, you mentioned about paintComponent() method but that is protected, how to call or implement that.
0
 
LVL 92

Expert Comment

by:objects
ID: 8221209
Just override it the same way you override paint()
0
 

Expert Comment

by:CleanupPing
ID: 9058521
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.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9731942
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
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 4 hours left to enroll

571 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