Topic: 实在是要疯了,这段代码到底问题出在哪里?大家帮忙

  Print this page

1.实在是要疯了,这段代码到底问题出在哪里?大家帮忙 Copy to clipboard
Posted by: krint
Posted on: 2004-08-16 12:50

import java.awt.*;
import java.applet.*;
import java.lang.*;

public class menu1 extends Applet implements Runnable
{
Image pic;
Image pic2;
Thread runner;
private boolean running=false;
Graphics signal;
Image offsignal;
int xpos;

public void init()
{
pic = getImage(getCodeBase(),"samba.png");
pic2 = getImage(getCodeBase(), "samba.png");
offsignal = createImage(this.size().width, this.size().height);
signal = offsignal.getGraphics();
}


public void start()
  {
    if (runner==null)
    {
      runner=new Thread(this);
      runner.start();
    }
  }
  
  public void stop()
  {
    if (runner != null)
    {
      runner.stop();
      runner=null;
    }
  }
  
  public void run()
  {
   while(true)
   {
   for (xpos = 148; xpos <= 392; xpos+=4)
   {
    repaint();
    try { Thread.sleep(100); }
    catch (InterruptedException e) { }
   }
    xpos = 148;
   }
  }
  
  public void paint(Graphics g)
{
signal.drawImage(pic, 20, 50,this);
signal.drawImage(pic2, 400,50, this);
signal.setColor(Color.black);
signal.drawLine(148,100,400,100);
signal.setColor(Color.blue);
signal.fillOval(xpos,98,8,8);

g.drawImage(offsignal,0,0,this);

}

  
  public void update(Graphics g)
  {
    paint(g);
  }
  
  public void destroy()
  {
  signal.dispose();
  }

}

本来应该是两副图片之间有一个蓝色的小圆球从一头到另一头,到达另一头以后,再从开端重新出发,就好像两台电脑之前的信息的传输。应该说每一帧画面都会被下一帧画面所替换,但是我的这段程序,做出的来的结果是前一帧的还是留在那里,没有被去掉,结果小圆球就变成了一段不断增长的圆柱,难道最后的signal.dispose()没有起作用?忘高手帮我查看一下,这段代码问题出在哪里?谢谢。

2.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: kingwzb
Posted on: 2004-08-18 09:19

你在paint函数里面先把窗口内容抹掉,再画小球。

3.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: krint
Posted on: 2004-08-18 11:03

我在paint函数里面就算是把其他都去掉不写,只画小球还是一样的结果,不知道楼上说得方法到底指什么?

4.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: bwpc
Posted on: 2004-08-18 17:35

在paint函数的最开头加入下面一行代码就ok了

signal.clearRect(0,0,this.getWidth(),this.getHeight());

5.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: bsspirit
Posted on: 2004-08-18 18:19

不行啊
NOTE: .....uses or override a deprecated API
NOTE: Recomple with -deprecation details

好像用了过时的关键字吧??!!

6.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: zy_ball
Posted on: 2004-08-18 18:45

public void paint(Graphics g)
{
//add the two lines
signal.setColor( Color.white );//使用你想要的背景颜色
signal.fillRect(0, 0, this.size().width, this.size().height);

signal.drawImage(pic, 20, 50,this);
signal.drawImage(pic2, 400,50, this);
signal.setColor(Color.black);
signal.drawLine(148,100,400,100);
signal.setColor(Color.blue);
signal.fillOval(xpos,98,8,8);

g.drawImage(offsignal,0,0,this);

}

7.Re:实在是要疯了,这段代码到底问题出在哪里?大家帮忙 [Re: krint] Copy to clipboard
Posted by: krint
Posted on: 2004-08-19 00:10

太感谢各位了,上述两种方法都可以实现,谢谢大家了。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923