?
Solved

not the everyday flickering

Posted on 1998-08-12
11
Medium Priority
?
276 Views
Last Modified: 2010-04-16

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)

Thanks

0
Comment
Question by:conick
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 480 total points
ID: 1232473

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
complete.
it with the current displayed image
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1232474
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.
0
 
LVL 7

Author Comment

by:conick
ID: 1232475
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)  {
                              isTotalShowing=false;
                              totalButton.setLabel("Show Totals");
                              LineGraph.this.repaint();
                        }
                        else if (!isTotalShowing)  {
                              isTotalShowing=true;
                              totalButton.setLabel("No Totals");
                              LineGraph.this.repaint();
                        }
                  }
            });
      //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.setColor(Color.lightGray);
            offGraphics.setFont(labelFont);
            offGraphics.fill3DRect(0,0, d.width, d.height,true);
            offGraphics.setColor(Color.black);
            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)  {
                    isNotFirst=false;
                    startY= (int) (xaxis-(scale*totalVal[0]));
                    startX= XMIN;
                    offGraphics.setColor(colors[colors.length-1]);
                    if (totalVal[0] != 0.0)
                          offGraphics.fillRect(startX-2,startY-2,4,4);
                    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)
                                    offGraphics.drawLine(startX,startY,endX,endY);
                                offGraphics.fillRect(endX-2,endY-2,4,4);
                                startY=endY;
                                startX=endX;
                                isNotFirst=true;
                          }
                    }
              }
              //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)
            this.setLayout(null);
            totalButton.setBounds(XIMG+d.width-80,YIMG+5,75,25);
            this.add(totalButton);
              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
            }
        }
    }

0
Independent Software Vendors: 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 7

Author Comment

by:conick
ID: 1232476
that text area didnt format that very well
Sorry.. let me know if you have any questions

0
 
LVL 7

Author Comment

by:conick
ID: 1232477
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

0
 
LVL 3

Expert Comment

by:sailwind
ID: 1232478
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

sailwind9999@hotmail.com (dummy address for safety reasons)




0
 
LVL 7

Author Comment

by:conick
ID: 1232479
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

conick
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1232480
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!
0
 
LVL 7

Author Comment

by:conick
ID: 1232481
No problem

I havent had time to work on it myself
If i could only work in Java full time *sigh*
0
 
LVL 7

Author Comment

by:conick
ID: 1232482
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
Component...
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)

0
 
LVL 3

Expert Comment

by:sailwind
ID: 1232483
Congrats. Good luck on rest of the program.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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