⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 marble.java

📁 java语言与面向对象程序设计源程序
💻 JAVA
字号:
import java.awt.*;

public class Marble extends Thread
{  private int xdir = 2*(1-2*(int)Math.round(Math.random()));
   private int ydir = 2*(1-2*(int)Math.round(Math.random()));
   private boolean running = false;
   private Table table = null;
   protected int x,y;
   public Marble(Table _table, int _x, int _y)
   { table = _table;
     x = _x;
     y = _y;
     start();
   }

   public void start()
   { running = true;
     super.start();
   }
  
   public void halt()
   {
     running = false;
   }
    
   public void run()
   {
    while (running)
     {
       move();
       try 
       { sleep(20);}
       catch (InterruptedException ie)
       { System.err.println("Thread interrupted");}
       table.repaint();
       //this.draw();
      }
    }

    private void move()
    {
      x+=xdir;
      y+=ydir;
      if (x > table.getSize().width) 
         { x = table.getSize().width;
           xdir *= (-1);
         }
      if  (x < 0)
          xdir *= (-1);

      if (y > table.getSize().height)
         {
          y =table.getSize().height;
          ydir *= (-1);
         }   
         if (y < 0)
         ydir *= (-1);
     }

     public void draw(Graphics g)
     {
       g.setColor(Color.red);
       g.fillOval(x, y, 30, 30);
     }
  
}
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -