Solved

not the everyday flickering

Posted on 1998-08-12
11
268 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
  • 6
  • 5
11 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 120 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

705 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

20 Experts available now in Live!

Get 1:1 Help Now