标签:
1 import java.applet.*; 2 import java.awt.*; 3 import javax.swing.*; 4 5 public class MyBall { 6 public static void main(String[] args) { 7 JFrame w = new JFrame(); 8 w.setSize(300, 400); 9 MyPanel mp = new MyPanel(); 10 w.add(mp); 11 Thread b1 = new Thread(mp, "b1"); 12 Thread b2 = new Thread(mp, "b2"); 13 b1.start(); 14 b2.start(); 15 w.show(); 16 } 17 } 18 19 class MyPanel extends JPanel implements Runnable { 20 int gx = 40, gy = 30, tx = 60, ty = 30; 21 String str1 = "b1", str2 = "b2"; 22 23 public void run() { 24 boolean stop = false; 25 while (!stop) { 26 String s = Thread.currentThread().getName().toString(); 27 if (s.equals(str1)) { 28 gy++; 29 try { 30 Thread.sleep(60); 31 } catch (Exception e) { 32 } 33 if (gy >= 200) 34 stop = true; 35 } else { 36 ty++; 37 try { 38 Thread.sleep(30); 39 } catch (Exception e) { 40 } 41 if (ty >= 200) 42 stop = true; 43 } 44 repaint(); 45 } 46 } 47 48 public void paint(Graphics g) { 49 super.paint(g); 50 g.fillOval(gx, gy, 20, 20); 51 g.fillOval(tx, ty, 20, 20); 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/zuferj115/p/5001847.html