0

我有以下代码:

import java.awt.*;
import java.applet.*;

public class DrawExample extends Applet 
{
    Font bigFont;

     Color redColor;
     Color weirdColor;
     Color bgColor;

     public void init() 
     {
          bigFont = new Font("Arial",Font.BOLD,16);
          redColor = Color.red;
          weirdColor = new Color(60,60,122);
          bgColor = Color.yellow;
      setBackground(bgColor);
     }

     public void stop()
     {
     }

     public void paint(Graphics g) 
     {
      g.setFont(bigFont);
      g.drawString("TESTING",80,20);
      g.setColor(Color.black);
      g.drawRect(100,100,100,100);
      g.fillRect(110,110,80,80);
      g.setColor(weirdColor);
      g.fillArc(120,120,60,60,0,360);
      g.setColor(Color.yellow);
      g.drawLine(140,140,160,160);
      g.setColor(Color.black);
     }
}

我不知道如何“重新绘制”我得到的输出。假设我希望小程序启动,3 秒后我想重新绘制“图片”,以便背景颜色变为红色(或其他)。我怎么做?如何每 3 秒更改一次输出?

4

1 回答 1

0

答案就在您的标题中,请尝试调用repaint() 方法。您可以使用TimerTaskrepaint()每 3 秒执行一次该方法/

例子:

boolean colourIsRed = false;
int delay = 1000; //1 second
int period = 3000; //3 seconds

Timer timer = new Timer();

timer.scheduleAtFixedRate(new TimerTask() {
  public void run() {
    if (colourIsRed) {
      setBackground(Color.blue);
      colourIsRed = false;
   }
   else {
      setBackground(Color.red);
      colourIsRed = true;
   }         

   repaint();
  }
}, delay, period);
于 2013-04-28T23:27:27.410 回答