A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 罗正荣 中级黑马   /  2013-4-20 16:14  /  9203 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 罗正荣 于 2013-4-21 12:33 编辑

  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.util.Random;
  5. import java.util.Timer;
  6. import java.util.TimerTask;

  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;

  9. public class Demo extends JPanel {
  10. public static void main(String[] args) {
  11. JFrame frame=new JFrame("满天星");
  12. Demo demo=new Demo();
  13. frame.setLayout(null);
  14. frame.setSize(600,600);
  15. frame.setLocation(50, 50);

  16. frame.setLocationRelativeTo(null);
  17. frame.setBackground(Color.BLUE);
  18. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. demo.setSize(500, 500);
  20. demo.setLocation(20,30);
  21. frame.add(demo);
  22. frame.setVisible(true);
  23. demo.go();
  24. }
  25. @Override
  26. public void paint(Graphics g) {
  27. setBackground(Color.BLACK);
  28. Font font=new Font("宋体",Font.BOLD,20);
  29. g.setFont(font);
  30. g.setColor(Color.WHITE);
  31. for(int i=0;i<50;i++){
  32. Random random=new Random();
  33. int x=random.nextInt(500);
  34. int y=random.nextInt(500);
  35. g.drawString("*", x, y);
  36. }

  37. }
  38. public void go(){
  39. Timer timer=new Timer();
  40. timer.schedule(new TimerTask(){
  41. public void run() {
  42. repaint();
  43. }
  44. }, 500, 500);
  45. }
  46. }
复制代码
帮忙看看为什么repaint();没有覆盖之前的画而是不断的添加呢?
预期效果是次在面板上画出50个*,但是这个的结果是每次加了50个。。。。。

评分

参与人数 1技术分 +1 收起 理由
黄玉昆 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 陈圳 于 2013-4-20 20:50 编辑
  1. import java.awt.Color;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.util.Random;
  5. import java.util.concurrent.TimeUnit;

  6. import javax.swing.JFrame;
  7. class Example39 extends JFrame{
  8.         Example39(){
  9.                 super("满天星");
  10.                 setBackground(Color.BLACK);
  11.                 new MyThread().start();
  12.                 setResizable(false);
  13.         }
  14.         @Override
  15.         public void paint(Graphics g) {
  16.                 g.clearRect(0, 0, 500, 400);//清除矩阵
  17.                 Font font = new Font("宋体", Font.BOLD, 20);
  18.                 g.setFont(font);
  19.                 g.setColor(Color.WHITE);
  20.                 for (int i = 0; i < 50; i++) {
  21.                         Random random = new Random();
  22.                         int x = random.nextInt(500);
  23.                         int y = random.nextInt(500);
  24.                         g.drawString("*", x, y);
  25.                 }
  26.         }
  27.         class MyThread extends Thread{
  28.                 public void run(){
  29.                         while(true){
  30.                                 try {
  31.                                         TimeUnit.SECONDS.sleep(1);
  32.                                         repaint();
  33.                                 } catch (InterruptedException e) {
  34.                                         e.printStackTrace();
  35.                                 }
  36.                         }
  37.                 }
  38.         }
  39.         public static void main(String[] args){
  40.                 SwingConsole.run(new Example39(), 400, 300);
  41.         }
  42. }
复制代码
repaint方法只是重复执行paint方法,并不清空区域,如果想要清清,在paint方法内,使用Graphics的clearRect方法.
程序我修改了下.你想法不错.定时器,我一点印象都没有...什么时候学的呀?
回复 使用道具 举报
陈圳 发表于 2013-4-20 20:48
repaint方法只是重复执行paint方法,并不清空区域,如果想要清清,在paint方法内,使用Graphics的clearRect方法 ...

repaint 应该是重绘啊,应该是重新绘制而不是重复绘制,贪吃蛇里就是直接使用repaint啊,不需要清除矩阵,我写的其他的也是能够重绘的,郁闷
回复 使用道具 举报
这里你要系统的了解repaint的执行过程,repaint/update/paint三者的关系.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马