?? die.java
字號(hào):
/** 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); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -