📄 histogramframe.java
字号:
/**
* file name : Histogram.java
* data : 2006-12-2
*
* abstract : a class that can show a histogram of a bmp file
* the main data is percent of bmp file, dram the histogram by
* the percent data.
*/
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
public class HistogramFrame extends Frame {
private float[] percent;
private double entropy;
final int GRAPHICS_WIDTH = 300;
final int GRAPHICS_HEIGHT = 300 * 20;
final int G_X0 = 20;
final int G_Y0 = 250;
final int WINDOW_WIDTH = 300;
final int WINDOW_HEIGHT = 300;
public HistogramFrame() {
setSize(WINDOW_WIDTH,WINDOW_HEIGHT);
setTitle("位图直方图");
this.setLocation(500,170);
// Add window listener.
this.addWindowListener
(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
HistogramFrame.this.windowClosed();
}
}
);
}
/**
* Shutdown procedure when run as an application.
*/
protected void windowClosed() {
this.removeAll();
this.dispose();
}
public void setPercent(float[] aPercent) {
percent = aPercent;
}
public void setEntropy(double AnEntropy) {
entropy = AnEntropy;
}
DecimalFormat format = new DecimalFormat("#0");
DecimalFormat formatEntropy = new DecimalFormat("0.00000");
public void paint(Graphics g) {
if (percent == null) {
return ;
}
int x = G_X0;
int y = 0;
int space = 0;
for (int i=0; i<percent.length; i++) {
y = (int)(percent[i] * GRAPHICS_HEIGHT);
g.setColor(Color.BLACK);
g.drawLine(x,G_Y0,x,G_Y0-y);
if (space > 15 && percent[i] > 5E-4) {
g.setColor(Color.RED);
g.drawString(format.format(percent[i]*1000),x-9,40);
g.drawString(Integer.toHexString(i+1),x-5,G_Y0 + 20);
g.drawLine(x,G_Y0,x,G_Y0-y);
space = 0;
}
x ++;
space ++;
}
g.setColor(Color.BLACK);
g.drawString("单位:1/1000",GRAPHICS_WIDTH-70,50);
g.drawString("位图的熵 : "+formatEntropy.format(entropy),GRAPHICS_WIDTH/2 - 50,G_Y0 + 40);
}
public float[] getPercent() {
float[] percent = new float[256];
for (int i=0; i<percent.length; i++) {
percent[i] = (float) Math.random();
}
return percent;
}
public static void main(String[] args) {
HistogramFrame f = new HistogramFrame();
f.setPercent(f.getPercent());
f.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -