⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 die.java

📁 Java程序设计(美) David D. Riley著 机械工业出版社 书籍配套 代码
💻 JAVA
字号:
/** class invariant: *      This class maintains a single die. *      and  dieRect.getWidth() == dieRect.getHeight() == 70 *      and  1 <= spotCount <= 6 *      and  spotCountSameAsDotsAdded() */import java.awt.Color;public class Die {		private int numberOfSpots;		private Rectangle dieRect;		private Oval topLeftSpot, midLeftSpot, botLeftSpot, middleSpot,                 topRightSpot, midRightSpot, botRightSpot;    		/** post:	spotCount() == 1		 *         	and  dieRect.getBackground() == Color.black		 */		public Die() {			dieRect = new Rectangle(0, 0, 70, 70);			topLeftSpot = newSpot(10, 10);			midLeftSpot = newSpot(10, 30);			botLeftSpot = newSpot(10, 50);			middleSpot = newSpot(30, 30);			topRightSpot = newSpot(50, 10);			midRightSpot = newSpot(50, 30);			botRightSpot = newSpot(50, 50);			setSpotCount(1);			setDieColor(Color.black);		}    		/** pre:		1 <= j <= 6		 *  post:	dieRect has j spots added to it		 */		public void setSpotCount(int j) {			assert 1 <= j && j <= 6 : "Invalid number of die spots";			numberOfSpots = j;			dieRect.removeAll();			if (numberOfSpots == 1) {				dieRect.add( middleSpot, 0 );			} else if (numberOfSpots == 2) {				dieRect.add( topLeftSpot, 0 );				dieRect.add( botRightSpot, 0 );			} else if (numberOfSpots == 3) {				dieRect.add(  topLeftSpot, 0 );				dieRect.add( middleSpot, 0 );				dieRect.add( botRightSpot, 0 );			} else if (numberOfSpots == 4) {				dieRect.add( topLeftSpot, 0 );				dieRect.add( botLeftSpot, 0 );				dieRect.add( topRightSpot, 0 );				dieRect.add( botRightSpot, 0 );			} else if (numberOfSpots == 5) {				dieRect.add( topLeftSpot, 0 );				dieRect.add( botLeftSpot, 0 );				dieRect.add( middleSpot, 0 );				dieRect.add( topRightSpot, 0 );				dieRect.add( botRightSpot, 0 );			} else {				dieRect.add( topLeftSpot, 0 );				dieRect.add( midLeftSpot, 0 );				dieRect.add( botLeftSpot, 0 );				dieRect.add( topRightSpot, 0 );				dieRect.add( midRightSpot, 0 );				dieRect.add( botRightSpot, 0 );			}			dieRect.repaint();			assert spotCountSameAsDotsAdded()				: "Number of spots does not match spotCount()";		}    		/** post:	result == numberOfSpots */		public int spotCount()  {			return numberOfSpots;		}		/** post:		dieRect.getBackground() == c		 *			and  (dieRect.getBackground() == Color.white  		 *				implies  all spots are colored black)		 *			and  (dieRect.getBackground() != Color.white  		 *				implies  all spots are colored white)		 */		public void setDieColor(Color c)  {			dieRect.setBackground( c );			if (c == Color.white) {				setAllSpotColors(Color.black);			} else {				setAllSpotColors(Color.white);			}			dieRect.repaint();			assert	implies( c == Color.white, 						middleSpot.getBackground() == Color.black )					&& implies( c != Color.white,						middleSpot.getBackground() == Color.white )				: "Color of die background inconsistent with spot color";		}    		/** post:	result == dieRect */		public Rectangle getDieRect() {			return dieRect;		}        		/** post:	result is a newly instantiated Oval		 *			and  result.getX() == x  and  result.getY() == y		 *			and  result.getWidth == result.getHeight == 10		 *			and  result.getColor == Color.white		 */		private Oval newSpot(int x, int y)  {			Oval result;			result = new Oval(x, y, 10, 10);			result.setBackground( Color.white );			return result;		}    		/** post:   result == p implies q */		private boolean implies(boolean p, boolean q) {			return !p || q;		}    		/** post:	topLeftSpot.getBackground() == c		 *			and  midLeftSpot.getBackground() == c  		 *			and  botLeftSpot.getBackground() == c		 *			and  middleSpot.getBackground() == c		 *			and  topRightSpot.getBackground() == c		 *			and  midRightSpot.getBackground() == c		 *			and  botRightSpot.getBackground() == c		 */		private void setAllSpotColors(Color c ) {			topLeftSpot.setBackground(c);			midLeftSpot.setBackground(c);			botLeftSpot.setBackground(c);			middleSpot.setBackground(c);			topRightSpot.setBackground(c);			midRightSpot.setBackground(c);			botRightSpot.setBackground(c);		}    		/** post:	numberOfSpots == count of spots added to dieRect */		private boolean spotCountSameAsDotsAdded()  {			int dotsAddedCount = 0;			if (topLeftSpot.getParent() != null)				dotsAddedCount++;			if (midLeftSpot.getParent() != null)				dotsAddedCount++;			if (botLeftSpot.getParent() != null)				dotsAddedCount++;			if (middleSpot.getParent() != null)				dotsAddedCount++;			if (topRightSpot.getParent() != null)				dotsAddedCount++;			if (midRightSpot.getParent() != null)				dotsAddedCount++;			if (botRightSpot.getParent() != null)				dotsAddedCount++;			return (dotsAddedCount == numberOfSpots); 		}}

⌨️ 快捷键说明

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