-1

I am writing an gui. In that gui, there are lots of shapes ( around 200 ). I used paint method to draw them.

In one situation, I have to make them blink (switching between two color). In a for loop I am changing their colors and then fram.repaint();

However, When I clicked some buttons, after a while program becomes very slow. I checked via Profile (I am using Netbeans). I saw that AWT-Event-Queue is starting to run all the time after a while.

So, I can have two solution:

Is there a way to split AWT-EventQueue of add another AWT-EventQueue? or Is there a better way to make 200 shapes to blink?

Thank you

note: in detail, I saw that pumpEvents, pumpEventsForHierarchy, pumpEventsForFilter, pumpOneEventFilters, ...

Here is paint method:

@Override
public void paint(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(bs_3);
    g2d.setColor(currentcolor);
    g2d.draw(line);;

}

Here is thread:

paintTimer = new Timer(1000, new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            mframe.repaint();

                for (CircuitPanel cp : mframe.cppL){
                    cp.onOff();
                }
});

paintTimer.start();

Here is color changer method:

@Override
public void onOff() {
    if(currentcolor.equals(offcolor)){
        currentcolor=oncolor;
    }else{
        currentcolor=offcolor;
    }
}
Emre
  • 79
  • 1
  • 11
  • 1
    If you are already using the profiler, why don't you check exactly what is hogging the CPU in that thread? That's what the profiler is for. – Marko Topolnik May 17 '12 at 12:34
  • 1
    Your problem is likely not what you think it is, and so "splitting the queue" is a poor kludge when the real issue is to fix the bug I'm betting you have, and that we have no way of guessing where it is since we can't see code. Could you be adding timers or listeners somehow and thereby bogging the system down? Only your code holds the answers. – Hovercraft Full Of Eels May 17 '12 at 12:49
  • Can you paste your `paint(Graphics g)` method code? – Hakan Serce May 17 '12 at 13:27
  • You shouldn't draw in a paint method but rather in a JComponent's (such as a JPanel) `paintComponent(...)` method. This will give you double buffering by default. I see your paintTimer object, but where do you create it? – Hovercraft Full Of Eels May 18 '12 at 14:47

2 Answers2

1

This example shows one approach. It marks time on another thread maintained by javax.swing.Timer in order to pace the flashing. To profile on your target platform, the example can be scaled easily by changing N and the timer's initial period, 1000 ms. Because instances ofjavax.swing.Timer use a shared thread, each component can have it own timer, as discussed here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Sorry for missing codes. now I added to my question. And now I will make necessary changes you explained me. Thanks for help – Emre May 18 '12 at 09:37
  • I don't see your code to `start()` the `Timer`; please edit your question to include an [sscce](http://sscce.org/). – trashgod May 18 '12 at 16:16
  • Your example is far from complete; you might start with the example I cited. Note that "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://java.sun.com/products/jfc/tsc/articles/painting/index.html#callbacks). – trashgod May 21 '12 at 14:10
0

I think I found the reason. I had added an bean in Netbeans which is a small panel. When I remove it from frame, It seems the problem is solved. (I am using frame.repaint() method as you can see below)

Thanks for your helps and comments. I am improving my codes with the help of your comments.

Here I am posting the code of MyPanel (that bean):

package beans;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;


public class MyParallelPanel extends javax.swing.JPanel {
int x1=300;
int x2=400;
int h=110;
Path2D path = new Path2D.Double();
Line2D line1 = new Line2D.Double(0, h, (x2-x1)/2, 0);
Line2D line2 = new Line2D.Double((x2-x1)/2, 0, x2-((x2-x1)/2), 0);
Line2D line3 = new Line2D.Double(x2-((x2-x1)/2), 0, x2, h);
Line2D line4 = new Line2D.Double(x2, h, 0, h);
Color color = new Color(237, 236, 235);
/** Creates new form MyParallelPanel */
public MyParallelPanel(int x1, int x2, int h,Color color) {
    this.x1=x1;
    this.x2=x2;
    this.h=h;
    this.color=color;
    setSize(x2,h);
    setPreferredSize(new Dimension(x2, h));

}

public void setColor(Color color) {
    this.color = color;
}

public void setH(int h) {
    this.h = h;
}

public void setX1(int x1) {
    this.x1 = x1;
}

public void setX2(int x2) {
    this.x2 = x2;
}
public MyParallelPanel() {

    initComponents();
}

/** This method is called from within the constructor to
 * initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is
 * always regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
    this.setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );
}// </editor-fold>                        
// Variables declaration - do not modify                     
// End of variables declaration                   
@Override
protected void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g; 

path.append(line1,true);
path.append(line2,true);
path.append(line3,true);
path.append(line4,true);

g2d.setColor(color);
g2d.fill(path);
}



}
Emre
  • 79
  • 1
  • 11