not the everyday flickering

I am having a problem with flickering.
But its not the kind of flickering that commonly
occurs in animation (I dont think)

I have a graphing application that is set up like this:
i have a graph class that draws some things that are
common in all the graphs (headings, axis, etc)

I subclass that with a couple other graph classes
(LineGraph, BarGraph, etc)

The problem appears in the LineGraph.  The graph consists of
dollar amounts on the Y axis and Weeks or months on the X axis
I have a button on the upper right corner of the graph that
toggles between showing the weekly (or monthly) totals and not
showing them (I use setText for the button to change text).

The flickering doesnt start until the i move to a different graph
and come back or when i change the values to graph
(when repaint() is called the second time)
The flickering doesnt stop until i end the application (unlike
animation where it stops if the animation stops)
When the user tries to click on the button while its flickering
it does one of three things...
1) doesnt work (wont even recognize that its been clicked) 50%
2) works (like normal) 45%
3) gives me a nasty Windows error (im on Win95)  5%

I have tried overriding the update() method

If someone has any idea what could be happening (or give me something
to try) I would be extremely greatful

Let me know if you need more info
the code is fairly long but ill post it if you think its necessary
(and promise not to laugh)


Who is Participating?

First of all, I'm not quite sure what your button do. It simply toggle the text
on the bars or the axis labels?

Are you creating a new graph window each time you go to a different graph?
It takes time to generate a new object and repaint it. What you might want
to do is to set up a couple of graph panels that are predrawn. When you want
to switch a graph just bring the desired window to the front. Swapping visibility
is a lot faster than creating a new window for each graph and it's also less
memory intensive.

Consider using double buffering, this solves most of the flickering problems.
What you do is you create a new image (that is not visibile at first), and use
this image to draw your graph. After the image is drawn in the background,
you simply swap the two images to bring the drawn image to the front. This
eliminates flickering because the object is not displayed until the drawing is
it with the current displayed image
Feel free to post the code, I think it'll help us visualize it.
Of course, if it is very very long, then send in a psedocode
and the block where you do the actual drawing and switching
between graphs.
conickAuthor Commented:
I cut down 400 lines to 80 but I think it gives youthe basic idea.  
I dont create a new object everytime I press the button but merely call repaint
there is no flickering if i leave off the button
Does that mean there is something wrong with the wayI create the button?
When switching between graphs there is very little flickering if any
(it was just when I added the totals button that started the problem
Here is the code: (I hope its not TOO gross)
//omitted -- imports
public class LineGraph extends Graph {
      //ommitted -- non button related class variables
    Button totalButton;
    boolean isTotalShowing,totalsOnly= false;
        public LineGraph(String filename, int field) throws IOException {
                totalButton= new Button("Show Totals");
            totalButton.addActionListener(new ActionListener()  {
                  public void actionPerformed(ActionEvent e)  {
                        if (isTotalShowing)  {
                              totalButton.setLabel("Show Totals");
                        else if (!isTotalShowing)  {
                              totalButton.setLabel("No Totals");
      //omitted-- getting data from files and storing in a vector of arrays
      //          filling arrays for actual points and labels
        public void update(Graphics g)  { paint(g);} //feeble attempt to fix flicker
        public void paint(Graphics g) {
              //omitted setting sizes and starting points depending on window size
              //omitted -- draw Header
            imgBuff= createImage(h.width,h.height);  //Image that rotates side text
            offImg= imgBuff.getGraphics();           //Graphics object (poorly named)
            //omitted -- setting color for this graphic-- similar to offGraphics
            gfxBuff = createImage(d.width, d.height);//Image that holds graph  
            offGraphics = gfxBuff.getGraphics();     //Graphics object
            offGraphics.fill3DRect(0,0, d.width, d.height,true);
            gfxBuff2= createImage(l.width, l.height); //Image that holds legend
            offGraphics2= gfxBuff2.getGraphics();     //Graphics Object
            //omitted -- setting color for this graphic-- similar to offGraphics
            //omitted --find min and max value for scaling
            //omitted -- Do Tick Marks
                //omitted -- Do vertical heading and rotate 90 degrees (in Filter class
              //omitted -- Draw Lines (they appear whether totals are displayed or not)
              //Draw total line if button is toggled on
              if (isTotalShowing)  {
                    startY= (int) (xaxis-(scale*totalVal[0]));
                    startX= XMIN;
                    if (totalVal[0] != 0.0)
                    for (int i=0;i<numTimeVals;i++)  {
                          if (totalVal[i] != 0.0)  {
                                endY= (int) (xaxis-(totalVal[i]*scale));
                                endX= (int) (XMIN + offset*i);
                                if (isNotFirst)
              //omitted -- Do horizontal tick marks and time labels to offGraphics
              //omitted -- draw Legend Here to offGraphics2
            //button gets added here -- is this the problem??
            //button sits on upper right corner of graph (within 3D rectangle)
              offGraphics.drawImage(imgBuff2,5,0,null); //draws Y-axis heading
            g.drawImage(gfxBuff, XIMG, YIMG, null);   //draws Graph
            g.drawImage(gfxBuff2, XIMG2, YIMG2,null); //draws Legend
            g.drawImage(gfxBuff3, XIMG3, YIMG3,null); //draws top heading

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

conickAuthor Commented:
that text area didnt format that very well
Sorry.. let me know if you have any questions

conickAuthor Commented:
As you can see from the code
I create 3 different images offscreen and then call repaint() when they need to be recreated

I do not create a new instance of the graph after the button is clicked
everything else but the button is included in the offscreen images
maybe that has something to do with my problem?

I dont know how to add an AWT component (the button) to the offscreen image (it seems to want only graphics objects)
Ive only used either graphics or AWT components... Ive never mixed the two .. there could be something wrong with how Im doing that...
I really need to get this resolved... (its driving me crazy)
I will open the question up again to other experts unless you (sailwind) can come up with
something else to try (I havent heard from you for a couple days)
Ill up the point total a bit

Go for it. I've been trying to work up a solution for a couple of days, but
haven't been able to come up with anything specific yet. If you could,
can you mail me the entire code? It would help me a lot to not have to
guess what other parameters you have used. Please mail to (dummy address for safety reasons)

conickAuthor Commented:
Just a comment to make sure the thread doesnt get autograded to a C :(

Thanks for taking a look at the file, sailwind
Let me know if you have any comments or suggestions

Thanks again

Sorry conick, I've been really busy with the project that I'm working on lately.
I will try to look at it some more when time becomes available. Good luck!
conickAuthor Commented:
No problem

I havent had time to work on it myself
If i could only work in Java full time *sigh*
conickAuthor Commented:
I got it! *bounce*

The problem was that i was adding the button to the screen after each paint()
so every time the paint method was called i was adding the button to the LineGraph
If I only add it once the flickering stops
now i only have to account for the resizing of the window (to place the button where I
want it) (i dont THINK that should be too tough)
Thanks for all your help sailwind
(you can junk the source code i sent you ... i know I would like too)

Congrats. Good luck on rest of the program.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.