我想实现的是 文字滚动的画面,可是图像总是残留,一堆一堆的,用了缓存就一片黑了?什么原因?
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_drawpublic void paint(Graphics g)
{
if(m_ready)
g.drawImage(m_image,0,0,320,320,this);
mb_draw();
}
}
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_drawpublic void paint(Graphics g)
{
if(m_ready)
g.drawImage(m_image,0,0,320,320,this);
mb_draw();
}
}
你把这条语句加上看看
g.clearRect(0,0,1024,1024);
你这样每次绘制的时候都是在同一个Image上绘制,再把这同一个Image画到界面上这样的感觉就是你在同一张纸上重复画,当然最后什么也不像了
两种解决的方法
1:每次都new一个Image。(不建议)
2:每次都在image的Graphics上fillRect一下,也就是说用背景色把这个image全部刷一下。
BTW:你这种实现不好,为什么不用直接在paint里面做呢?mb_draw();方法放到g.drawImage(m_image,0,0,320,320,this);后面的意思是什么?