做文字动画的时候,如何让图像不残留阿?急!急!

merryland2 2006-12-20 08:53:16
我想实现的是 文字滚动的画面,可是图像总是残留,一堆一堆的,用了缓存就一片黑了?什么原因?
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.Timer;
import javax.swing.JApplet;
import java.awt.image.*;


public class animation extends JApplet implements ActionListener
{

String text="欢迎来公司上班";
Color C;
int m_frame=-1;
Timer m_timer;
boolean m_frozen=false;
int x=20;
int y=20;
boolean m_ready=true;
BufferedImage m_image=new BufferedImage(1024,1024,BufferedImage.TYPE_INT_RGB);

public void init()
{
setBackground(Color.blue);
int delay=20;
m_timer=new Timer(delay,this);
m_timer.setInitialDelay(0);
m_timer.setCoalesce(true);

getContentPane( ).addMouseListener(new MouseAdapter( )
{
public void mousePressed(MouseEvent e)
{
m_frozen = !m_frozen;
if (m_frozen)
mb_stopAnimation( );
else mb_startAnimation( );
}
});
} // End of method: init

public void start( )
{
mb_startAnimation( );
} // End of method: start

public void stop( )
{
mb_stopAnimation( );
} // End of method: stop

public void actionPerformed(ActionEvent e)
{
repaint( );
} // End of method: actionPerformed

public void mb_startAnimation( )
{
if (!m_frozen && !m_timer.isRunning( ))
m_timer.start( );
} // End of method: mb_startAnimation

public void mb_stopAnimation( )
{
if (m_timer.isRunning( ))
m_timer.stop( );
} // End of method: mb_stopAnimation


public void mb_draw()
{

if(!m_ready)
return;
m_ready=false;
Graphics2D g=m_image.createGraphics();
C=new Color(255,255,255,255);
g.setColor(C);
Font F=new Font(text,Font.BOLD+Font.ITALIC,24);
g.setFont(F);
g.drawString(text,0,100);
C=new Color(255,0,0,128);
g.setColor(C);
g.drawString(text,x,y);
x=x+10;
x %= 300;
y %= 400;
try
{
Thread.sleep(400);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
} //end of mb_draw

public void paint(Graphics g)
{
if(m_ready)
g.drawImage(m_image,0,0,320,320,this);
mb_draw();
}
}
...全文
159 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
merryland2 2006-12-22
  • 打赏
  • 举报
回复
呵呵~~解决了
fool_leave 2006-12-22
  • 打赏
  • 举报
回复
当然会有残留了


你这样每次绘制的时候都是在同一个Image上绘制,再把这同一个Image画到界面上

这样的感觉就是你在同一张纸上重复画,当然最后什么也不像了


两种解决的方法
1:每次都new一个Image。(不建议)
2:每次都在image的Graphics上fillRect一下,也就是说用背景色把这个image全部刷一下。


BTW:你这种实现不好,为什么不用直接在paint里面做呢?mb_draw();方法放到g.drawImage(m_image,0,0,320,320,this);后面的意思是什么?
博傻狂徒 2006-12-22
  • 打赏
  • 举报
回复
使用双缓冲技术就不会残留 .
merryland2 2006-12-21
  • 打赏
  • 举报
回复
试过了,没什么改变。
chinareny2k 2006-12-20
  • 打赏
  • 举报
回复
简单的看了一下你作了一个图像缓冲区m_image,然后你在上面不停的drawString 但是你没有对缓冲区作清屏的操作所以得到的图像是连续的阿!!
你把这条语句加上看看
g.clearRect(0,0,1024,1024);
merryland2 2006-12-20
  • 打赏
  • 举报
回复
自己先顶!

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧