1 package com.java7.myballfalldown.main; 2 3 import java.awt.Graphics; 4 5 import javax.swing.JFrame; 6 import javax.swing.JPanel; 7 8 public class MyBallFallDown { 9 public static void main(String[] args) { 10 JFrame w = new JFrame(); 11 w.setSize(300, 400); 12 13 MyPanel mp = new MyPanel(); 14 w.add(mp); 15 16 Thread t = new Thread(mp); 17 t.start(); 18 19 w.setVisible(true); 20 } 21 } 22 23 class MyPanel extends JPanel implements Runnable { 24 int x = 30; 25 int y = 30; 26 27 public void paint(Graphics g) { 28 super.paint(g); 29 g.fillOval(x, y, 20, 20); 30 } 31 public void run() { 32 while(true) { 33 y++; 34 if(y > 400) { 35 y = 0; 36 } 37 try { 38 Thread.sleep(20); 39 } catch(Exception e) { 40 41 } 42 repaint(); 43 } 44 } 45 }
时间: 2024-11-04 03:24:26