growingdot.java

来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 62 行

JAVA
62
字号
// see FIgure 15.12import java.awt.Color;import java.awt.event.*;import javax.swing.*;/**	Class Invariant:	 *		redDot.getWidth() == redDot.getHeight() *		and  redDot.getX() == 100 - redDot.getWidth()/2 *		and  redDot.getY() == 100 - redDot.getHeight()/2  */public class GrowingDot  extends JApplet 		implements ActionListener {	private Oval redDot;	private Timer clock;	private ButtonFrame startBtn;	private int darkness;	/**	post:	redDot is added on this 	 *			and startBtn != null	 *			and this is colored black	 *			and clock is scheduled for 0.1 sec. repeated events  	 */	public GrowingDot()  {		setLayout(null);		redDot = new Oval(98, 98, 4, 4);		redDot.setBackground( Color.red );		add(redDot);		clock = new Timer(100, this);		clock.setRepeats(true);		startBtn = new ButtonFrame(clock);		darkness = 0;		setBackground(new Color(darkness, darkness, darkness));	}	/**	pre:	redDot != null  <br>	 *	post:	(redDot@pre.getWidth() == 200) implies	 *					redDot is resized to 4 by 4 and this recolored to black	 *			and  (redDot@pre.getWidth() != 200) implies	 *					( redDot resize to 2 by 2 larger than redDot@pre	 *					and this is recolored whiter ) 	 */	public void actionPerformed(ActionEvent e) {		if (redDot.getWidth() == 200)  {			redDot.setSize(4, 4);			redDot.setLocation(98, 98);			darkness = 0;			setBackground(new Color(darkness, darkness, darkness));			clock.stop();		} else {			redDot.setSize(redDot.getWidth()+2, redDot.getHeight()+2);			redDot.setLocation(100-redDot.getWidth()/2, 100-redDot.getHeight()/2);			darkness = darkness + 5;			if (darkness > 255)				darkness = 255;			setBackground( new Color(darkness, darkness, darkness) );		}		redDot.repaint();		repaint();	}}

⌨️ 快捷键说明

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