Java Applets - 重绘

时间:2013-04-28 22:10:13

标签: java applet repaint

我有以下代码:

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秒?

2 个答案:

答案 0 :(得分:0)

在paint方法的开头添加:

super.paint(g);

答案 1 :(得分:0)

答案在您的标题中,请尝试拨打repaint() method。您可以使用TimerTask每3秒执行repaint()方法/

示例:

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