This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Constant paintComponent calling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Constant paintComponent calling" Watch "Constant paintComponent calling" New topic
Author

Constant paintComponent calling

Edu Cobi
Greenhorn

Joined: Jan 24, 2004
Posts: 9
Hello,
I have developed a JFrame in swing. I have the problem that the paintComponent function is called more than 50 times a second without a single repaint() anywhere.
Please help me to find why it is repainting like crazy.
Thanks.
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
Need more info, Post your code
Edu Cobi
Greenhorn

Joined: Jan 24, 2004
Posts: 9
Hi Matt,
thanks for answering.
the code is 400 lines long. I will cut it:
public class Chart extends JPanel{
DataCollector dataCollect;
BarArray bars1,bars2;...
...
public Chart(BarArray bars1,BarArray bars2,OrderProcessor orderProc, DataCollector dataCollect){
this.config = (Config)Config.getConfig();
this.bars1 = bars1;
...
public void paintComponent(Graphics g){
System.out.println("paintComponent called at "+sdf.format(new Date()));
//this is how I see it is called 50 times/second
...
public void main() {
JFrame win = new JFrame("Edgar & Eduardo Automated Trader: Chart display software");
Image image = new ImageIcon( "bars.gif" ).getImage();
win.setIconImage(image);
Container frameContent=win.getContentPane();
JButton exitB=new JButton("EXIT");
JButton backtestB=new JButton("Hist.data");
JButton performB=new JButton("Perform");
final OnOffIcon OnOffI= new OnOffIcon();
final JButton OnOffB = new JButton(OnOffI);
JLabel placeOrdersL = new JLabel("Orders:");
final JButton placeOrdersB = new JButton(orderProc.getPlaceOrders()==true?"True":"False");
ButtonGroup barCandleGroup=new ButtonGroup();
JRadioButton CandleRB=new JRadioButton("CandleStick",true);
barCandleGroup.add(barRB);
barCandleGroup.add(CandleRB);
JButton resetB=new JButton("Reset");
JButton ConB=new JButton("-><-");
// ConB.setIcon(new ImageIcon("rofl.gif"));
JButton ExpB=new JButton("<-->");
JPanel top = new JPanel();
top.add(exitB);
top.add(backtestB);
top.add(performB);
top.add(OnOffB);
top.add(placeOrdersL);
top.add(placeOrdersB);
top.add(barRB);
top.add(CandleRB);
top.add(resetB);
top.add(ConB);
top.add(ExpB);
// RBExpConPanel.setBorder(new TitledBorder("Chart types:"));
frameContent.add(top,BorderLayout.NORTH);
...
//listeners
resetB.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(JOptionPane.showConfirmDialog(
Chart.this,"Are you sure yo want to reset?",
"Confirm resetting", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
bars1.reset();
bars2.reset();
((StrategyAnalyzer)config.get("analyzer")).reset();
repaint();
}
}
});
...
...
win.pack();
win.setVisible(true);
repaint();
}
PLEASE HELP! THANKS.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
I was suspicious of the repaint call in the code below but now it doesn't look so bad after I formatted it. You might try putting a System.out.println inside this and other event handlers to see if they are being called too much.

If you do things right there should not be a need to call repaint at the end (after win.pack). I would go thru your code and comment out every call to repaint in an attempt to stop the repainting. And make sure you don't have a repaint inside any paint or paintComponent methods.
[ January 24, 2004: Message edited by: Craig Wood ]
Edu Cobi
Greenhorn

Joined: Jan 24, 2004
Posts: 9
Thank you very much for your help.
IT HAS SOLVED THE PROBLEM!!!
I have commented out all the repaint() calls and now there is only repaint when I hide and show back the window.
I think it was this final repaint call after win.pack; I will check it now by uncommenting the actionlistener repaint calls.
It has also solved the problem of my button being not visible.
NOW EVERYTHING IS PERFECT.
THANKS!!!
ps: now the problem is going to be how to refresh when new data comes. ha ha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constant paintComponent calling
 
Similar Threads
GUI display(rendering) problem
refresh canvas, designer problem?
repaint() method problem
Jbutton on top of fillRect
does repaint() call paintComponent() ?