Posted on 2009-02-12 14:46
希 阅读(596)
评论(0) 编辑 收藏
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import cilent.ScreenMessage;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
public class ScrollText extends JComponent
{
private BufferedImage image;
private Dimension imageSize;
private volatile int currOffset;
private Thread internalThread;
private volatile boolean noStopRequested;
private String scrollText;
public ScrollText(String text)
{
ScreenMessage m = new ScreenMessage();
currOffset=0;
buildImage("今天天气不错!over");
this.setMinimumSize(imageSize);
this.setPreferredSize(imageSize);
this.setMaximumSize(imageSize);
this.setSize(imageSize);
noStopRequested=true;
Runnable r=new Runnable()
{
public void run()
{
try
{
runWork();
}
catch(Exception e)
{
e.printStackTrace();
}
}
};
internalThread=new Thread(r,"ScrollText");
internalThread.start();
}
private void buildImage(String text)
{
//请求绘制时,打开反锯齿,并运用高质量选项
RenderingHints renderHints=new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
renderHints.put(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY);
//创建一幅临时图像,用于判断文件的大小
BufferedImage scratchImage=new BufferedImage(1,1,BufferedImage.TYPE_INT_RGB);
Graphics2D scratchG2=scratchImage.createGraphics();
scratchG2.setRenderingHints(renderHints);
Font font=new Font("宋体",Font.BOLD|Font.ITALIC,24);//Serif
FontRenderContext frc=scratchG2.getFontRenderContext();
TextLayout t1=new TextLayout(text,font,frc);
Rectangle2D textBounds=t1.getBounds();
int textWidth=(int)Math.ceil(textBounds.getWidth()); //设置滚动区的大小是字符串的大小
//int textWidth=ScreenMessage.width; //设置滚动区的大小
int textHeight=(int)Math.ceil(textBounds.getHeight());
// int textHeight = ScreenMessage.height/10;
int horizontalPad=0;
int verticalPad=6;
imageSize=new Dimension(textWidth+horizontalPad,textHeight+verticalPad);
//创建适当大小的图像
image=new BufferedImage(imageSize.width,imageSize.height,BufferedImage.TYPE_INT_RGB);
Graphics2D g2=image.createGraphics();
g2.setRenderingHints(renderHints);
int baselineOffset=(verticalPad/2)-((int)textBounds.getY());
g2.setColor(Color.white);
g2.fillRect(0,0,imageSize.width,imageSize.height);
g2.setColor(Color.black);
t1.draw(g2,0,baselineOffset);
//立即释放资源,但保持动画显示图像
scratchG2.dispose();
scratchImage.flush();
g2.dispose();
}
public void paint(Graphics g)
{
//不论当前大小,确保裁减边缘
g.setClip(0,0,imageSize.width,imageSize.height);
int localOffset=currOffset; //以防改变
g.drawImage(image,-localOffset,0,this);
g.drawImage(image,imageSize.width-localOffset,0,this);
/*//绘制轮廓
g.setColor(Color.black);
g.drawRect(0,0,imageSize.width-1,imageSize.height-1); */
}
private void runWork()
{
while(noStopRequested)
{
try
{
Thread.sleep(50); //每秒10帧
//调整滚动位置
currOffset=(currOffset+1)%imageSize.width;
//通知事件线程调用paint()
repaint();
}
catch(InterruptedException e)
{
Thread.currentThread().interrupt();
}
}
}
public void stopRequest()
{
noStopRequested=false;
internalThread.interrupt();
}
public boolean isAlive()
{
return internalThread.isAlive();
}
public static void main(String args[])
{
ScrollText st=new ScrollText("Java can do animation!");
JPanel p=new JPanel();
p.setLayout(new FlowLayout());
p.add(st);
JFrame f=new JFrame("ScrollText Demo");
f.setContentPane(p);
}
public String getScrollText() {
return scrollText;
}
public void setScrollText(String scrollText) {
this.scrollText = scrollText;
}
}