Движение текста без использования потоков - Applet - Java
Формулировка задачи:
Здраствуйте,хочу чтобы текст без использовние потоков двигался.У меня вроде получилось.Но как сделать чтобы текст пропадал ,а то он накладываеться один на другой.
public class JavaApplet extends Applet implements MouseListener{
String msg=new String("first");
int x=0;
int y=20;
int clickCounter=0;//we will increment this shit when user will do double click
void setString()
{
if(this.clickCounter%2==0)
{
this.msg="first";
}
else
{
this.msg="second";
}
}
public void init() {
setBackground(Color.yellow);
setForeground(Color.black);
addMouseListener( this );//
}
public void start(){
}
public void paint(Graphics g){
while(true){
try {
Thread.sleep(1000);
update( g );
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setForeground(Color.yellow);
}}
public void mouseClicked( MouseEvent e )
{ this.clickCounter++;
this.setString();
setForeground(Color.yellow);
repaint();
e.consume();
}
public void update( Graphics g ) {
x +=10;
g.drawString(msg,x,y);
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}Решение задачи: «Движение текста без использования потоков - Applet»
textual
Листинг программы
public void paint(Graphics g) {
while (true) {
try {
Thread.sleep(1000);
g.clearRect(0, 0, this.getWidth(), this.getHeight());
update(g);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void update(Graphics g) {
x += 10;
if (x > this.getWidth()) {
x = 0;
}
g.drawString(msg, x, y);
}