📄 driver.java
字号:
import java.awt.Color;/** Author: David D. Riley * Date: Jan, 2005 * Histogram Program (Figure 5.27) */ public class Driver extends ThreeButtons{ private ThreeButtonFrame histoWin; private Rectangle leftBar, midBar, rightBar; private int leftClickCount, midClickCount, rightClickCount; private final int maxBarHeight = 400; /** post: A window is displayed that contains a histogram of the * number of times that each window button is clicked * as a percentage of the total number of such clicks. */ public Driver() { histoWin = new ThreeButtonFrame("Button Click Histogram"); histoWin.setLayout(null); leftClickCount = 0; midClickCount = 0; rightClickCount = 0; leftBar = newBar(100); histoWin.add(leftBar, 0); midBar = newBar(250); histoWin.add(midBar, 0); rightBar = newBar(400); histoWin.add(rightBar, 0); histoWin.repaint(); } /** pre: leftBar, midBar and rightBar are constructed * post: leftClickCount == leftClickCount@pre + 1 * and all bars of the histogram are properly updated */ public void leftAction() { leftClickCount++; redrawHistogram(); } /** pre: leftBar, midBar and rightBar are constructed * post: midClickCount == midClickCount@pre + 1 * and all bars of the histogram are properly updated */ public void midAction() { midClickCount++; redrawHistogram(); } /** pre: leftBar, midBar and rightBar are constructed * post: rightClickCount == rightClickCount@pre + 1 * and all bars of the histogram are properly updated */ public void rightAction() { rightClickCount++; redrawHistogram(); } /** post: result is a filled blue rectangle * and result.getWidth() == 100 and result.getX() == j */ private Rectangle newBar(int j) { Rectangle tmp = new Rectangle(j, 0, 100, 0); tmp.setBackground(Color.blue); return tmp; } /** pre: leftBar, midBar and rightBar are constructed * post: all bars of the histogram are properly updated */ private void redrawHistogram() { redrawOneBar(leftBar, leftClickCount); redrawOneBar(midBar, midClickCount); redrawOneBar(rightBar, rightClickCount); } /** pre: r is constructed * post: the y and height attributes of r are adjusted to a new height of * maxBarHeight * (c/(leftClickCount+midClickCount+rightClickCount)) */ private void redrawOneBar(Rectangle r, int c) { int totalClicks, newHeight; totalClicks = leftClickCount + midClickCount + rightClickCount; newHeight = (int)((double)c / totalClicks * maxBarHeight); r.setLocation( r.getX(), 430-newHeight ); r.setSize( r.getWidth(), newHeight ); r.repaint(); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -