rectangularcake.java
来自「Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套」· Java 代码 · 共 45 行
JAVA
45 行
// Figure 9.9import java.awt.*;public class RectangularCake extends Cake { private Rectangle theCake; /** post: getX() == x and getY() == y * and getWidth() == w and getHeight() == h * and getBackground == Color.yellow */ public RectangularCake( int x, int y, int w, int h ) { super(x, y, w, h); setBackground( Color.yellow ); } /** post: the cake is drawn as a rectangle filling the bounding rectangle * and the color of the cake is getBackground() */ protected void paintCake( Graphics g ) { g.setColor( getBackground() ); g.fillRect(0, 0, getWidth(), getHeight()); } /** post: two black lines (one vertical and one horizontal) divide * the cake into fourths * and getWidth() > getHeight() implies the cake is separated by * two more vertical lines making 8 equal-sized pieces. * and getWidth() <= getHeight() implies the cake is separated by * two more horizontal lines making 8 equal-sized pieces. */ protected void paintEighthCuts( Graphics g ) { g.setColor( Color.black ); g.drawLine(0, getHeight()/2, getWidth(), getHeight()/2); g.drawLine(getWidth()/2, 0, getWidth()/2, getHeight()); if ( getWidth() > getHeight() ) { g.drawLine(getWidth()/4, 0, getWidth()/4, getHeight()); g.drawLine(getWidth()*3/4, 0, getWidth()*3/4, getHeight()); } else { g.drawLine(0, getHeight()/4, getWidth(), getHeight()/4); g.drawLine(0, getHeight()*3/4, getWidth(), getHeight()*3/4); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?