import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DanBai
{ public static void main(String[] args)
{ JFrame f = new DFrame("DanBai");
f.show();
}
}
class DFrame extends JFrame
{ public DFrame(String title)
{ super(title);
setSize(300, 200);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent evt)
{ System.exit(0); }
});
getContentPane().add(new DPanel());
}
}
class DPanel extends JPanel
implements TimerListener
{ public DPanel()
{ Timer t = new Timer(1000);
t.addTimerListener(this);
}
public void timeElapsed(TimerEvent evt)
{ Graphics g = getGraphics();
int x1, y1, x2, y2 = 0, r = 160, t = 5;
double b = 30, a = this.getWidth();
x2 = (int)(a / 2);
x1 = (int)(x2 - Math.sin(b * Math.PI / 180) * r);
y1 = (int)(Math.cos(b * Math.PI / 180) * r);
g.drawLine(x1, y1, x2, y2);
g.fillOval(x1 - t, y1 - t, 2 * t, 2 * t);
if(b >= 30) b--;
else if (b <= 30) b++;
g.dispose();
}
}
interface TimerListener extends EventListener
{ public void timeElapsed(TimerEvent evt);
}
class Timer extends Component implements Runnable
{ public Timer(int i)
{ interval = i;
Thread t = new Thread(this);
t.start();
evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
enableEvents(0);
}
public void addTimerListener(TimerListener l)
{ listener = l;
}
public void run()
{ while (true)
{ try { Thread.sleep(interval); }
catch(InterruptedException e) {}
TimerEvent te = new TimerEvent(this);
evtq.postEvent(te);
}
}
public void processEvent(AWTEvent evt)
{ if (evt instanceof TimerEvent)
{ if (listener != null)
listener.timeElapsed((TimerEvent)evt);
}
else super.processEvent(evt);
}
private int interval;
private TimerListener listener;
private static EventQueue evtq;
}
class TimerEvent extends AWTEvent
{ public TimerEvent(Timer t) { super(t, TIMER_EVENT); }
public static final int TIMER_EVENT
= AWTEvent.RESERVED_ID_MAX + 5555;
}
程序后半段是抄书的,哪位可以帮我解释一下吗??
[此贴子已经被作者于2005-12-30 16:17:55编辑过]