shadowedreddot.java

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

JAVA
55
字号
// Figure 6.18import java.awt.Color;import javax.swing.JFrame;/**	CLass Invariant *		A ShadowedRedDot object... *			is a filled circular region colored red  *			and is centered its Container  *			and has a shadow that is the same size as dot and colored black *			and the shadow is delta units left & above dot upon the window *					(delta is 10% of the diameter of dot)  */public class ShadowedRedDot  {	private Oval dot, shadow;	/**	post:	dot.getWidth() == dot.getHeight() == 200   */	public ShadowedRedDot(JFrame w )  {		dot = new Oval(w.getWidth()/2-100, w.getHeight()/2-100, 200, 200);		dot.setBackground( Color.red );		shadow = new Oval(dot.getX()+20, dot.getY()-20, 200, 200);		shadow.setBackground( Color.black );		w.add(shadow, 0);		w.add(dot, 0);		w.repaint();	}	/**	post:	dot.getWidth() == dot.getWidth()@pre * (100 + d)/100	 *			and dot.getHeight() == dot.getHeight()@pre * (100 + d)/100  	 */	public void zoom(double d)   {		dot.setLocation( (int)(-d/200*dot.getWidth()) + dot.getX(), 							(int)(-d/200*dot.getHeight()) + dot.getY() );		dot.setSize( (int)(dot.getWidth() * (1.0+d/100)), 						(int)(dot.getHeight() * (1.0+d/100)) );		shadow.setSize( dot.getWidth(), dot.getHeight() );		shadow.setLocation( dot.getX()+(int)(dot.getWidth()*0.1),							   dot.getY()-(int)(dot.getHeight()*0.1) );		dot.repaint();		shadow.repaint();	}	/**	post:	dot.getWidth() == dot.getHeight == 200   */	public void reset()   {		int dotCenterX, dotCenterY;		dotCenterX = dot.getX() + dot.getWidth()/2;		dotCenterY = dot.getY() + dot.getHeight()/2;		dot.setSize( 200, 200 );		dot.setLocation( dotCenterX-100, dotCenterY-100 );		shadow.setSize( 200, 200 );		shadow.setLocation( dot.getX()+20, dot.getY()-20 );		dot.repaint();		shadow.repaint();	}}

⌨️ 快捷键说明

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