做文字动画的时候,如何让图像不残留阿?急!急!
我想实现的是 文字滚动的画面,可是图像总是残留,一堆一堆的,用了缓存就一片黑了?什么原因?
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();
}
}