Topic: 请问关于applet的paint方法 |
Print this page |
1.请问关于applet的paint方法 | Copy to clipboard |
Posted by: ifmirror Posted on: 2005-03-24 22:13 我想要试着实现一个字符串在界面上运动的简单applet。 开始的代码是 import javax.swing.JApplet; import java.awt.*; public class MovingString extends JApplet { int x = 15; int addX = 0; //字符串横坐标增量 public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; //字符串横坐标变化 addX += 3; x += addX; //保证字符串在当前行往复运动 g2d.drawString("I am moving!", x % 1000, y); //控制延时 try { Thread.sleep(200); } catch (InterruptedException e) { System.out.println(e.getMessage()); } repaint(); } } 实现的结果是:无数的字符串相互覆盖着被绘制在界面上,并且全部可见,最后是一行黑条。 后来把程序修改为 import javax.swing.JApplet; import java.awt.*; public class MovingString extends JApplet { int x = 15; int addX = 0; public void paint(Graphics g) { Graphics2D g2d = (Graphics2D)g; g2d.setColor(Color.WHITE); g2d.drawString("I am moving!", x % 1000, y % 1000); addX += 3; x += addX; g2d.setColor(Color.BLACK); g2d.drawString("I am moving!", x % 1000, y % 1000); try { Thread.sleep(200); } catch (InterruptedException e) { System.out.println(e.getMessage()); } repaint(); } } 这是从书上找来的代码,我理解是每次重新绘制时都要用背景色的字符串将上一次绘制的字符串覆盖,之后再在新的位置绘制新的字符串。 难道repaint方法调用paint的效果不是全面地重新绘制整个界面,而只是追加式地么? 恳请赐教。 |
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 |