本帖最后由 陈圳 于 2013-4-20 20:50 编辑
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.util.Random;
- import java.util.concurrent.TimeUnit;
- import javax.swing.JFrame;
- class Example39 extends JFrame{
- Example39(){
- super("满天星");
- setBackground(Color.BLACK);
- new MyThread().start();
- setResizable(false);
- }
- @Override
- public void paint(Graphics g) {
- g.clearRect(0, 0, 500, 400);//清除矩阵
- Font font = new Font("宋体", Font.BOLD, 20);
- g.setFont(font);
- g.setColor(Color.WHITE);
- for (int i = 0; i < 50; i++) {
- Random random = new Random();
- int x = random.nextInt(500);
- int y = random.nextInt(500);
- g.drawString("*", x, y);
- }
- }
- class MyThread extends Thread{
- public void run(){
- while(true){
- try {
- TimeUnit.SECONDS.sleep(1);
- repaint();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public static void main(String[] args){
- SwingConsole.run(new Example39(), 400, 300);
- }
- }
复制代码 repaint方法只是重复执行paint方法,并不清空区域,如果想要清清,在paint方法内,使用Graphics的clearRect方法.
程序我修改了下.你想法不错.定时器,我一点印象都没有...什么时候学的呀? |