请问如何让一个图形在一运行程序开始就一直存在。例如时钟的图形。
------解决方案--------------------
// 程序:电子小时钟
// 范例文件:Clock.java
import java.awt.*;
import java.util.*; //为了使用其中的GregorianCalendar类
import java.applet.*;
public class Clock extends Applet implements Runnable
{
//=====资料成员==================================================
int hour,minute,second,ImageWidth,ImageHeight,X,Y,
width,height;
Image digit[],Background,OffScreen;
Thread newThread;
boolean showSeparator;
Graphics drawOffScreen;
MediaTracker MT;
GregorianCalendar time; //可用来获得时间与日期
//=====函数成员==================================================
public void init()
{
X = 50; //时间数字被绘制的起始位置
Y = 110;
MT = new MediaTracker(this);
digit = new Image[11];
width = 210; //重绘区域的大小
height = 135;
//使用MediaTracker追踪图像
Background = getImage(getDocumentBase(), "Images/clock.jpg ");
MT.addImage(Background,0);
for(int i=0;i <11;i++)
{
digit[i] = getImage(getDocumentBase(), "Images/ " + i + ".jpg ");
MT.addImage(digit[i],0);
}
try
{
showStatus( "图像加载中(Loading Images)... ");
MT.waitForAll();
}
catch(InterruptedException E){ }
//获得图像的宽度与高度
ImageWidth = digit[0].getWidth(this);
ImageHeight = digit[0].getHeight(this);
//建立次画面
OffScreen = createImage(300,300);
drawOffScreen = OffScreen.getGraphics();
showSeparator = true; //显示时间分隔符
}
public void start() //start()函数
{
newThread = new Thread(this); //建立与启动新线程
newThread.start();
}
public void stop() //stop()函数
{
newThread = null; //将线程设为null
}
public void paint(Graphics g)
{
//只清除此部分区域的图像
drawOffScreen.clearRect(X,Y,width,height);
//绘制背景图像
drawOffScreen.drawImage(Background,0,0,this);
if(hour < 10) //绘制小时
{
drawOffScreen.drawImage(digit[0],X,Y,this);
drawOffScreen.drawImage(digit[hour],X+ImageWidth,Y,this);
}
else
{
drawOffScreen.drawImage(digit[hour/10],X,Y,this);
drawOffScreen.drawImage(digit[hour%10],X+ImageWidth,Y,this);
}
if(minute < 10) //绘制分钟
{
drawOffScreen.drawImage(digit[0],X+ImageWidth*2+10,Y,this);
drawOffScreen.drawImage(digit[minute],X+ImageWidth*3+10,Y,this);
}
else
{
drawOffScreen.drawImage(digit[minute/10],X+ImageWidth*2+10,Y,this);
drawOffScreen.drawImage(digit[minute%10],X+ImageWidth*3+10,Y,this);
}
if(second < 10) //绘制秒数
{
drawOffScreen.drawImage(digit[0],X+ImageWidth*4+15,
Y+ImageHeight-30,15,30,this);
drawOffScreen.drawImage(digit[second],X+ImageWidth*4+30,
Y+ImageHeight-30,15,30,this);
}
else
{
drawOffScreen.drawImage(digit[second/10],X+ImageWidth*4+15,
Y+ImageHeight-30,15,30,this);
drawOffScreen.drawImage(digit[second%10],X+ImageWidth*4+30,