本帖最后由 郑冬 于 2012-6-10 16:32 编辑
代码如下
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class ScApplet extends Applet
{
final String testStr = "Java真好玩,学习很有趣!";
private int nextX;
private int nextY;
private Color nextColor;
private Font nextFont;
java.util.Random rand = new java.util.Random(System.currentTimeMillis());
public void init()
{
Timer timer = new Timer(200 , new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
nextX = rand.nextInt(400);
nextY = rand.nextInt(300);
nextColor = new Color(rand.nextInt(255) , rand.nextInt(255) , rand.nextInt(255));
nextFont = new Font("Times" , Font.BOLD , rand.nextInt(60));
repaint();
}
});
timer.start();
}
public void paint(Graphics g)
{
g.setColor(nextColor);
g.setFont(nextFont);
g.drawString(testStr, nextX, nextY);
}
public static void main(String[] args)
{
JFrame jf = new JFrame();
ScApplet panel = new ScApplet();
panel.setPreferredSize(new Dimension(400, 300));
panel.init();
panel.start();
jf.add(panel);
jf.pack();
jf.setVisible(true);
}
}
运行结果如下:
[img]file:///C:/Users/think/AppData/Roaming/Tencent/Users/915458516/QQ/WinTemp/RichOle/MUMT9YG[W%7DH0EQPSG%7BTBU_G.jpg[/img]
程序中重写print方法时绘制了一个字符串,但运行结果上出现了很多的字符串,是为什么?
|
|