これは私の問題の単純なバージョンです。私は3つのクラスを持っています: public class TopographyFrame extends JFrame - JPAnel とボタンを備えたシンプルな JFrame public class TopograpyPanel extends JPanel - JPanel to fill Rectangles public class Siec - 計算を実行し、JPAnale で repaint を呼び出すクラス
JPanel で、paintComponent() メソッドをオーバーライドしました
public void paintComponent (Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
System.out.println(rectangles.length);
for(int i = 0 ; i < rectangles.length ; i++){
g2.setPaint(neurony[i].winner);
g2.fillRect((int)rectangles[i].x,(int)rectangles[i].y,(int)rectangles[i].width, (int)rectangles[i].height);
}
}
neurony - フィールド public Color を持つオブジェクトの配列
クラスSiecでは、クラスJFrameで再描画するためにJPanelへの参照があります。プライベートアクションリスナーを持つボタンがあります。
class MyListener implements ActionListener{
Siec s;
public MyListener(Siec s){
this.s = s;
}
public void actionPerformed(ActionEvent arg0) {
try {
s.forPaint();
} catch (Exception e) {
e.printStackTrace();
}
}
Siec の forPaint() メソッドは次のようになります。
public void forPaint(){
setTopography();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setTopography();
}
public void setTopography() {
for (int i = 0; i < vector.colors.length; i++) {
neurony[i].winner = vector.colors[(int)(random() * 900 % vector.colors.length)];
}
panel.repaint();
}
vector.color は色の配列です
だから私の問題は、ボタンをクリックするとすぐにJPanelを再描画し、3秒後にもう一度再描画したいということです。Insted JPanel は、3 秒の遅延後に 1 回だけ再描画します。}