2012-01-24 14 views
0

私はpaintComponentメソッドを使用して、毎回特定の遅延の後に異なる幾何学的オブジェクトを描画します。paintComponent:毎回遅れて異なる幾何学的図形をペイントします

私はSystem.currentTimeMillis()関数で答えを見ました。Timerクラスを使用したいと思います。手伝って頂けますか?

+0

「あなたは私を助けることができる?」* *たぶん、あなたは実際にどのような問題が発生している。ここで

は、完全なサンプルですか? –

+1

一般的なアプローチは、Timerクラスを使用して描画する幾何学的オブジェクトの変更をトリガーし、次にrepaint()を呼び出すことだと思います。 Timerクラスに関するウェブページを見つけたり、見たことがありますか? – user949300

答えて

1

Javaのスレッドについて学ぶことができます。ここでは、サンプルにsun tutorials

のサンプルを変更しました(下のAnimator)は0.5秒ごとにペインタの変数を更新し、パネルに再描画を呼び出します。

package com.swing.examples; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

import com.swing.examples.BullsEyePanel.Animator; 

/* 
*************************************************************** 
* Silly Sample program which demonstrates the basic paint 
* mechanism for Swing components. 
*************************************************************** 
*/ 
public class SwingPaintDemo { 
    public static void main(String[] args) { 
     JFrame f = new JFrame("Aim For the Center"); 
     f.addWindowListener(new WindowAdapter() { 
      public void windowClosing(WindowEvent e) { 
       System.exit(0); 
      } 
     }); 
     BullsEyePanel panel = new BullsEyePanel(); 
     panel.add(new JLabel("BullsEye!", SwingConstants.CENTER), BorderLayout.CENTER); 
     f.getContentPane().add(panel, BorderLayout.CENTER); 
     f.pack(); 
     f.show(); 
     Animator animator = panel.new Animator(panel); 
     animator.start(); 
    } 
} 

/** 
* A Swing container that renders a bullseye background 
* where the area around the bullseye is transparent. 
*/ 
class BullsEyePanel extends JPanel { 
    private int i = 0; 
    public BullsEyePanel() { 
     super(); 
     setOpaque(false); // we don't paint all our bits 
     setLayout(new BorderLayout()); 
     setBorder(BorderFactory.createLineBorder(Color.black)); 
    } 

    public Dimension getPreferredSize() { 
     // Figure out what the layout manager needs and 
     // then add 100 to the largest of the dimensions 
     // in order to enforce a 'round' bullseye 
     Dimension layoutSize = super.getPreferredSize(); 
     int max = Math.max(layoutSize.width,layoutSize.height); 
     return new Dimension(max+100,max+100); 
    } 

    protected void paintComponent(Graphics g) { 
     Dimension size = getSize(); 
     int x = 0; 
     int y = 0; 

     while(x < size.width && y < size.height) { 
      g.setColor(i%2==0? Color.red : Color.white); 
      g.fillOval(x,y,size.width-(2*x),size.height-(2*y)); 
      x+=10; y+=10; i++; 
     } 
    } 
    class Animator extends Thread{ 
     private BullsEyePanel painter; 
     private int sleepTime = 500; //half a second 
     public Animator(BullsEyePanel painter){ 
      this.painter = painter; 
     } 
     public void run(){ 
      while(true){ 
       painter.i++; 
       painter.repaint(); 
       try{ 
        Thread.sleep(sleepTime); 
       } 
       catch(InterruptedException e){ 
        e.printStackTrace(); 
       } 
      }  
     } 
    } 
} 
関連する問題