📄 histogram.java
字号:
// Histogram.java: Display a histogram in a panel to show the// occurrence of the lettersimport javax.swing.*;import java.awt.*;public class Histogram extends JPanel { // Count the occurrence of 26 letters private int count[]; /**Set the count and display histogram*/ public void showHistogram(int[] count) { this.count = count; repaint(); } /**Paint the histogram*/ public void paintComponent(Graphics g) { if (count == null) return; // No display if count is null super.paintComponent(g); // Find the panel size and bar width and interval dynamically int width = getSize().width; int height = getSize().height; int interval = (width-40)/count.length; int individualWidth = (int)(((width-40)/24)*0.60); // Find the maximum count. The maximum count has the highest bar int maxCount = 0; for (int i=0; i<count.length; i++) { if (maxCount < count[i]) maxCount = count[i]; } // x is the start position for the first bar in the histogram int x = 30; // Draw a horizontal base line g.drawLine(10, height-45, width-10, height-45); for (int i=0; i<count.length; i++) { // Find the bar height int barHeight = (int)(((double)count[i]/(double)maxCount)*(height-55)); // Display a bar (i.e. rectangle) g.drawRect(x, height-45-barHeight, individualWidth, barHeight); // Display a letter under the base line g.drawString((char)(65+i)+"", x, height-30); // Move x for displaying the next character x += interval; } } /**Override getPreferredSize*/ public Dimension getPreferredSize() { return new Dimension(300, 300); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -