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;
}
}
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 changingN
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.