AWT-EventQueue-0 is running all the time and program become very slow in java

1.6k Views Asked by At

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;
    }
}
2

There are 2 best solutions below

3
On

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.

0
On

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);
}



}