circularcake.java

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

JAVA
40
字号
// Figure 9.10import java.awt.*;public class CircularCake  extends Cake  {		/**	post:	getX() == x  and   getY() == y	 *			and  getWidth() == getHeight() == d	 *			and   getBackground() == Color.pink  	 */    public CircularCake( int x, int y, int d )  {    	super(x, y, d, d);		setBackground( Color.pink );	}	/**	post:	the cake is drawm as an oval filling the bounding rectangle	 *	   		and the color of the cake is getBackground() 	 */	protected void paintCake( Graphics g )   {		g.setColor( getBackground() );		g.fillOval(0, 0, getWidth(), getHeight());	}	/**	post:   four black lines are drawn through the cake center to form	 *          eight wedge-shaped pieces of equal size.  	 */	protected void paintEighthCuts( Graphics g )   {		int ds, dl;		g.setColor( Color.black );		// draw horizontal and vertical lines, respectively		g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2);		g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight() );        		// draw diagonal lines		ds = (int)( (getWidth() - getWidth()/Math.sqrt(2.0)) / 2.0 );		dl = (int)( getWidth() / Math.sqrt(2.0) ) + ds;		g.drawLine( ds, ds, dl, dl );		g.drawLine( ds, dl, dl, ds );	}}

⌨️ 快捷键说明

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