Solved

Panel  getGraphics problem

Posted on 2001-08-21
20
765 Views
Last Modified: 2012-06-21

Hi,

I have a tree with many nodes which represent
unused volume on the diskdrives

for example

root
--40 gb
--60 gb

I want a pie chart which will represent
the unused space on the disk


I do this
 Graphics g = panel.getGraphics();


right now I have put the code to draw piechart
in the valueChanged event of the tree because the pie chart will change as this event is fired

 public void valueChanged(TreeSelectionEvent e)
    {
      gui_drawPieChart();
    }

The pie chart is displayed correctly
but if I do anything like resizing/minimizing
the panel becomes empty I mean I cannot see the
chart if I resize the panel

I am sure this is some sort of paint problem

Is there any way to draw  pie chart before the
panel is displayed and redraw as the value changed event is fired




Any help?
0
Comment
Question by:sanjay_thakur
  • 10
  • 4
  • 4
  • +1
20 Comments
 
LVL 2

Expert Comment

by:Tom7
ID: 6426133
You have to override the panel's paintComponent() method, then the chart will be there constantly! That means you must use a subclass (for example of JPanel). Something like:

class ChartPanel extends JPanel {
  float unusedDiskSpace;
  float totalDiskSpace;
  public void paintComponent(Graphics g) {
    if (totalDiskSpace == 0.0F) return;
    // do your drawing here:
  }
}


Tom
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6430325
Hi, I did that but the problem is it looks bit
distorted the pie chart.I mean it looks real rough at the edges.

Is ther any way I can make it look better
without distortions.
0
 
LVL 2

Expert Comment

by:Tom7
ID: 6432670
So how did you draw the chart? Maybe you could post some code.

Tom
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6438910
Hi, here is the code




public void paintComponent(Graphics g){

    gui_customize();

    int  x=0;
    int  y=0;
    int width=0,height=0;
    int ax=0,ay=0;          //For Drawing Black line from center to Peripherial
    int px=0,py=0;          //For Writing Percentage
    int radius=0;
    //width=height=Math.min((this.getSize().width - 100),(this.getSize().height - 100));
    width=height=Math.min((this.getSize().width - 15),(this.getSize().height - 15));
    //width =height = 100;

    x=y=50;

    if ( this.getSize().width > width ){
        x = (this.getSize().width - width ) /2 ;
    }


     if ( this.getSize().height > height ){
        y = (this.getSize().height - height ) /2 ;
    }



    cx = x + width/2;
    cy = y + height/2;
    radius = width/2;

    // Draw the Title of the Chart on Top of the Applet

    strWidth=fontMetrics.stringWidth(title);
    Font fnt = new java.awt.Font("Sanserif", Font.BOLD, 12);
    g.setFont(fnt);
    g.setColor(Color.red);
    //g.drawString(title,((detP.getSize().width - strWidth )/2),15);
    //g.drawString(title,((this.getSize().width - strWidth )/2),15);

    g.setFont(font);
    int initAngle=90;
    int sweepAngle=0;
    int incSweepAngle=0;
    int incLabelAngle= (int) (angle[0]/2);

    for (int i=0; i < columns; i++) {
        sweepAngle = (int) Math.round(angle[i]);
        g.setColor((Color)colors[i]);

        if (i==(columns-1)){
            sweepAngle = 360 - incSweepAngle;
            g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
            g.setColor(Color.black);
            g.drawArc(x,y,width,height,initAngle,(-sweepAngle));

            if (showLabel){
                lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                adjustLabel(i);
                g.drawString((String)labels[i],lx,ly);
            }
            if (showPercent){
                px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
                py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
                g.drawString(String.valueOf(Math.round(percent[i]))+"%",px,py);
            }
            break;
        }

        g.fillArc(x,y,width,height,initAngle,(-sweepAngle));
        g.setColor(Color.black);
        g.drawArc(x,y,width,height,initAngle,(-sweepAngle));
        incSweepAngle +=sweepAngle;

        ax = (int) (cx + ( radius * Math.cos((incSweepAngle * 3.14f/180) - 3.14f/2)));
        ay = (int) (cy + ( radius * Math.sin((incSweepAngle * 3.14f/180) - 3.14f/2)));
        g.drawLine(cx,cy,ax,ay);

        if (showLabel){
            lx = (int) (cx + ( radius * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
            ly = (int) (cy + ( radius * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
            adjustLabel(i);
            g.drawString((String)labels[i],lx,ly);
        }
        if (showPercent){
            px = (int) (cx + ((radius*2/3) * Math.cos((incLabelAngle * 3.14f/180) - 3.14f/2)));
            py = (int) (cy + ((radius*2/3) * Math.sin((incLabelAngle * 3.14f/180) - 3.14f/2)));
            strWidth = fontMetrics.stringWidth(Math.round(percent[i])+"%");
            g.drawString(String.valueOf(Math.round(percent[i]))+"%",(px - strWidth/2),py);
        }

        incLabelAngle = incLabelAngle + (int) (angle[i]/2 + angle[i+1]/2);
        initAngle += (-sweepAngle);
    }
    g.setColor(Color.black);
    g.drawLine(cx,cy,cx,cy-radius);
  }
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6514906
Hi

I get some junk values in the panel in which I have the
Pie chart.

If I minimize the screen and load it again
then it looks fine

I know its repaint() problem
If I try to put this code in PaintComponent()
method the mouse keeps flickering continuously

this.invalidate();
this.validate();
this.repaint();


where should I do to solve this repaint problem
 


0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6515104
repaint() sets a flag to tell the gui to redraw itself at the next available opportunity.

invalidate() sets a flag to tell the gui to redo the component's layout.

validate() redoes the layout.

You shouldn't need to call either of invalidate or validate if your component neither has children nor changes its own size.  It's likely the call to validate() that is causing the flicker, unless you are calling repaint() from within the paintComponent method, in which case that'd probably cause flicker.

What values in the panel are "junk"?
0
 
LVL 92

Expert Comment

by:objects
ID: 6515569
Sounds like your doing painting outside of the paintComponent method.
You should limit your paint calls to within the paintComponent() method.
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6517104
Actually

I have a JSplitpane witch two panel components,horizontal divider.
The top component of the splitpane has
Jtree and textfield

The bottom component has this pie chart panel

When the pie chart gets displayed it shows some components of the top panel.

I mean I can see the tree and the textfield of
the top component in the background of
the pie chart(no the entire tree just a part of it)

If I minimize the screen everything looks fine.

thanks.





0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6520484
Hi I get the same problem when I select any of the nodes
of the tree in the top panel.

The pie chart has to change according to the
selected node.

It changes fine but displays some part of the tree in the background.

If I minimize and get it back again then it looks fine

0
 
LVL 92

Expert Comment

by:objects
ID: 6521632
Sounds like your doing painting outside of the paintComponent method.
You should limit your paint calls to within the paintComponent() method.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6521682
HI

Please let me know any solution to this.
it's bit urgent for me

Thanks in advance
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6521690
for objects

I have removed

this.invalidate();
this.validate();
this.repaint();

from the paintComponent method.

It is same problem.

Where should I call invalidate so that the panel will
paint itself before it is displayed on the screen?


0
 
LVL 92

Expert Comment

by:objects
ID: 6521708
I didn't say anything about invalidate.
It has nothing really to do with painting.
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6521792
Hi

My problem is that as the node selected in the tree
changes I have to change the values
in the pie chart.

To do this

I set the values and then I call
piechart.invalidate()
piechart.validate()
piechart.repaint()

This redraws the pie chart with proper values
but shows the same junk in the background





0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6521839
Well, I don't see in your paint routine anywhere you clear or otherwise paint the background.

Try a rectangular fill of the background to a background color prior to drawing the rest of your chart.
0
 
LVL 92

Expert Comment

by:objects
ID: 6521860
Still doesn't explain why your calling invalidate() and validate(). Though I wouldn't think they'd create a problem.
0
 
LVL 3

Expert Comment

by:stimpyjcat
ID: 6521875
The repeated calls to validate() might induce cursor flicker, since it potentially recalculates the layout and updates the cursor.
0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6521931
Hi filling the rectangle solves the problem
But is there any way to make this rectangle invisible

I am using windows look and feel hence
I either need to fill it with the default color
of the windows since that is the color for the
entire application or I have to make the
rectangle invisible.

Please let me know.

currently
this is what I am doing in paint component

 g.setColor(Color.lightGray);
    g.fillRect(0, 0 , this.getSize().width , this.getSize().height);

How do I get the system default color
or make the filled rectangle invisible




 

0
 
LVL 3

Accepted Solution

by:
stimpyjcat earned 50 total points
ID: 6521936
You can get the background color with

g.setColor(getBackground())

This should work even on OS X, which uses a textured stripe pattern as the default background "color".

0
 
LVL 4

Author Comment

by:sanjay_thakur
ID: 6524220
Thanks a ton!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

747 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

10 Experts available now in Live!

Get 1:1 Help Now